-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_queues.go
82 lines (60 loc) · 1.21 KB
/
list_queues.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
package eventbus
import (
"sync"
)
type ListQueues struct {
*Semaphore
queues []*QueueCallbacks
len int
mtx *sync.Mutex
}
func NewListQueues(nrQueues int, limitRunner int) *ListQueues {
if nrQueues <= 0 {
panic("NewListQueues(): number of queues must be greater than 0")
}
queues := make([]*QueueCallbacks, nrQueues)
semaphore := NewSemaphore(nrQueues)
for i := range queues {
queues[i] = NewQueue(limitRunner)
}
return &ListQueues{
queues: queues,
Semaphore: semaphore,
mtx: &sync.Mutex{},
len: nrQueues,
}
}
func (c *ListQueues) Push(x *QueueCallbacks) {
c.mtx.Lock()
c.len++
for i := 0; i < c.len; i++ {
if c.queues[i] == nil {
c.queues[i] = x
break
} else if x.WaitingCallbacks() < c.queues[i].WaitingCallbacks() {
for j := i; j < c.len; j++ {
if c.queues[j] == nil {
c.queues[j] = x
break
} else {
c.queues[i], x = x, c.queues[i]
}
}
break
}
}
c.mtx.Unlock()
}
func (c *ListQueues) Pop() *QueueCallbacks {
var (
x *QueueCallbacks
)
c.mtx.Lock()
c.queues[0], x = x, c.queues[0]
for i := 1; i < c.len; i++ {
c.queues[i-1], c.queues[i] = c.queues[i], c.queues[i-1]
}
c.len--
c.mtx.Unlock()
return x
}