-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
57 lines (44 loc) · 825 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
package gofherd
import (
"sync"
"sync/atomic"
)
type atomicBool struct {
val *int64
}
func (a *atomicBool) setTrue() {
atomic.StoreInt64(a.val, 1)
}
func (a *atomicBool) setFalse() {
atomic.StoreInt64(a.val, 0)
}
func (a *atomicBool) value() bool {
return atomic.LoadInt64(a.val) == 1
}
type queue struct {
hose chan Work
mu sync.Mutex
num uint64
close atomicBool
}
func newQueue() queue {
return queue{hose: make(chan Work), close: atomicBool{val: new(int64)}}
}
func (q *queue) increment() {
atomic.AddUint64(&(q.num), 1)
}
func (q *queue) setClosedTrue() {
q.close.setTrue()
}
func (q *queue) closed() bool {
return q.close.value()
}
func (q *queue) count() uint64 {
return atomic.LoadUint64(&(q.num))
}
func (q *queue) lock() {
q.mu.Lock()
}
func (q *queue) unlock() {
q.mu.Unlock()
}