-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqueue.go
63 lines (47 loc) · 784 Bytes
/
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
package kaburaya
import "sync"
type queue struct {
counter int
limit int
mu *sync.RWMutex
}
func newQueue(limit int) *queue {
return &queue{
limit: limit,
mu: &sync.RWMutex{},
}
}
func (q *queue) incrementLimit(n int) int {
defer q.mu.Unlock()
q.mu.Lock()
q.limit += n
if q.limit < 1 {
q.limit = 1
}
return q.limit
}
func (q *queue) full() bool {
defer q.mu.RUnlock()
q.mu.RLock()
return q.counter >= q.limit
}
func (q *queue) empty() bool {
defer q.mu.RUnlock()
q.mu.RLock()
return q.counter == 0
}
func (q *queue) enqueue() {
defer q.mu.Unlock()
q.mu.Lock()
q.counter++
}
func (q *queue) dequeue() {
defer q.mu.Unlock()
q.mu.Lock()
q.counter--
}
func (q *queue) count() int {
defer q.mu.RUnlock()
q.mu.RLock()
return q.counter
}