-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
92 lines (68 loc) · 1.59 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
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"sync"
"gitgud.io/softashell/comfy-translator/translator"
)
type Queue struct {
items []queueObject
lock *sync.Mutex
}
type queueObject struct {
req translator.Request
lock *sync.Mutex
count int
outChan chan string
}
func NewQueue() *Queue {
q := new(Queue)
q.lock = &sync.Mutex{}
return q
}
// Join adds a new item to queue or returns true and a channel if you need to wait
func (q *Queue) Join(req translator.Request) (chan string, bool) {
q.lock.Lock()
defer q.lock.Unlock()
if pos, found := q.findItem(req); found {
q.items[pos].lock.Lock()
q.items[pos].count++
q.items[pos].lock.Unlock()
return q.items[pos].outChan, true
}
i := queueObject{
req: req,
outChan: make(chan string),
lock: &sync.Mutex{},
}
q.addItem(i)
return nil, false
}
// Push sends output to all waiting threads and removes item from queue
func (q *Queue) Push(req translator.Request, response string) {
q.lock.Lock()
defer q.lock.Unlock()
if pos, found := q.findItem(req); found {
q.items[pos].lock.Lock()
// Sends response to all listeners
for i := 0; i < q.items[pos].count; i++ {
q.items[pos].outChan <- response
}
// Close unused channel
close(q.items[pos].outChan)
q.items[pos].lock.Unlock()
q.removeItem(pos)
}
}
func (q *Queue) findItem(req translator.Request) (int, bool) {
for i, t := range q.items {
if t.req == req {
return i, true
}
}
return -1, false
}
func (q *Queue) addItem(t queueObject) {
q.items = append(q.items, t)
}
func (q *Queue) removeItem(i int) {
q.items = append(q.items[:i], q.items[i+1:]...)
}