-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
76 lines (66 loc) · 1.37 KB
/
Copy pathqueue.go
File metadata and controls
76 lines (66 loc) · 1.37 KB
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
package main
import "sync"
// PromptQueue serialises prompts so only one is active at a time.
type PromptQueue struct {
mu sync.Mutex
cond *sync.Cond
items []PromptItem
closed bool
onReady func(PromptItem)
}
type PromptItem struct {
Text string
Done chan struct{}
}
func NewPromptQueue(onReady func(PromptItem)) *PromptQueue {
q := &PromptQueue{onReady: onReady}
q.cond = sync.NewCond(&q.mu)
go q.run()
return q
}
func (q *PromptQueue) Enqueue(text string) {
q.mu.Lock()
defer q.mu.Unlock()
if q.closed {
return
}
item := PromptItem{Text: text, Done: make(chan struct{})}
q.items = append(q.items, item)
q.cond.Signal()
}
func (q *PromptQueue) Close() {
q.mu.Lock()
defer q.mu.Unlock()
q.closed = true
q.cond.Signal()
}
// QueueDepth returns the number of prompts waiting to be executed (not counting
// the one currently running, if any).
func (q *PromptQueue) QueueDepth() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.items)
}
// Clear discards all queued prompts that have not yet started executing.
func (q *PromptQueue) Clear() {
q.mu.Lock()
defer q.mu.Unlock()
q.items = q.items[:0]
}
func (q *PromptQueue) run() {
for {
q.mu.Lock()
for len(q.items) == 0 && !q.closed {
q.cond.Wait()
}
if q.closed {
q.mu.Unlock()
return
}
item := q.items[0]
q.items = q.items[1:]
q.mu.Unlock()
q.onReady(item)
<-item.Done
}
}