-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
48 lines (41 loc) · 777 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
package testplant
type Queue struct {
// Since messages are empty, we can just use an integer for the pending ones
pending uint
Send, Recv chan struct{}
}
func NewQueue() *Queue {
q := &Queue{
Send: make(chan struct{}),
Recv: make(chan struct{}),
}
// Consumer/producer/waiter
// FIXME to run.
go func() {
for {
// If there are values in queue, accept reads and writes
if q.pending > 0 {
select {
case _, ok := <-q.Send:
if !ok {
return // Finish
}
q.pending++
case q.Recv <- *new(struct{}):
q.pending--
}
} else {
// Wait until we've received
_, ok := <-q.Send
if !ok {
return
}
q.pending++
}
}
}()
return q
}
func (q *Queue) Add(messages uint) {
q.pending += messages
}