forked from johnlauer/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 101
/
queue.go
164 lines (140 loc) · 3.61 KB
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// queue.go
//
// Created by Hicham Bouabdallah
// Copyright (c) 2012 SimpleRocket LLC
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
package main
import (
"strings"
"sync"
)
type queuenode struct {
data string
id string
next *queuenode
}
// A go-routine safe FIFO (first in first out) data stucture.
type Queue struct {
head *queuenode
tail *queuenode
count int
lock *sync.Mutex
lenOfCmds int
}
// Creates a new pointer to a new queue.
func NewQueue() *Queue {
q := &Queue{}
q.lock = &sync.Mutex{}
return q
}
// Returns the number of elements in the queue (i.e. size/length)
// go-routine safe.
func (q *Queue) Len() int {
q.lock.Lock()
defer q.lock.Unlock()
return q.count
}
// Returns the length of the data (gcode cmd) in the queue (i.e. size/length)
// go-routine safe.
func (q *Queue) LenOfCmds() int {
q.lock.Lock()
defer q.lock.Unlock()
return q.lenOfCmds
}
// Pushes/inserts a value at the end/tail of the queue.
// Note: this function does mutate the queue.
// go-routine safe.
func (q *Queue) Push(item string, id string) {
q.lock.Lock()
defer q.lock.Unlock()
n := &queuenode{data: item, id: id}
if q.tail == nil {
q.tail = n
q.head = n
} else {
q.tail.next = n
q.tail = n
}
q.count++
q.lenOfCmds += len(item)
}
// Returns the value at the front of the queue.
// i.e. the oldest value in the queue.
// Note: this function does mutate the queue.
// go-routine safe.
func (q *Queue) Poll() (string, string) {
q.lock.Lock()
defer q.lock.Unlock()
if q.head == nil {
return "", ""
}
n := q.head
q.head = n.next
if q.head == nil {
q.tail = nil
}
q.count--
q.lenOfCmds -= len(n.data)
return n.data, n.id
}
// Returns a read value at the front of the queue.
// i.e. the oldest value in the queue.
// Note: this function does NOT mutate the queue.
// go-routine safe.
func (q *Queue) Peek() (string, string) {
q.lock.Lock()
defer q.lock.Unlock()
n := q.head
if n == nil || n.data == "" {
return "", ""
}
return n.data, n.id
}
// Returns a read value at the front of the queue.
// i.e. the oldest value in the queue.
// Note: this function does NOT mutate the queue.
// go-routine safe.
func (q *Queue) Delete() {
q.lock.Lock()
defer q.lock.Unlock()
q.head = nil
q.tail = nil
q.count = 0
q.lenOfCmds = 0
}
func (q *Queue) DebugStr() string {
q.lock.Lock()
defer q.lock.Unlock()
str := ""
if q.head != nil {
str += q.head.id + ":" + strings.Replace(q.head.data, "\n", "\\n", -1)
n := q.head.next
for n != nil {
str += ", " + n.id + ":" + strings.Replace(n.data, "\n", "\\n", -1)
n = n.next
}
}
return str
}