forked from centrifugal/centrifuge-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
79 lines (71 loc) · 1.73 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
package centrifuge
import "sync"
// cbQueue allows to process callbacks in separate goroutine with
// preserved order.
// This queue implementation is a slightly modified code borrowed from
// https://github.com/nats-io/nats.go client released under Apache 2.0
// license: see https://github.com/nats-io/nats.go/blob/master/LICENSE.
type cbQueue struct {
mu sync.Mutex
cond *sync.Cond
head *asyncCB
tail *asyncCB
}
type asyncCB struct {
fn func()
next *asyncCB
}
// dispatch is responsible for calling async callbacks. Should be run
// in separate goroutine.
func (q *cbQueue) dispatch() {
for {
q.mu.Lock()
// Protect for spurious wake-ups. We should get out of the
// wait only if there is an element to pop from the list.
for q.head == nil {
q.cond.Wait()
}
curr := q.head
q.head = curr.next
if curr == q.tail {
q.tail = nil
}
q.mu.Unlock()
// This signals that the dispatcher has been closed and all
// previous callbacks have been dispatched.
if curr.fn == nil {
return
}
curr.fn()
}
}
// Push adds the given function to the tail of the list and
// signals the dispatcher.
func (q *cbQueue) push(f func()) {
q.pushOrClose(f, false)
}
// Close signals that async queue must be closed.
func (q *cbQueue) close() {
q.pushOrClose(nil, true)
}
func (q *cbQueue) pushOrClose(f func(), close bool) {
q.mu.Lock()
defer q.mu.Unlock()
// Make sure that library is not calling push with nil function,
// since this is used to notify the dispatcher that it must stop.
if !close && f == nil {
panic("pushing a nil callback with false close")
}
cb := &asyncCB{fn: f}
if q.tail != nil {
q.tail.next = cb
} else {
q.head = cb
}
q.tail = cb
if close {
q.cond.Broadcast()
} else {
q.cond.Signal()
}
}