-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
66 lines (59 loc) · 1.48 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
// Copyright 2023-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rosmar
import (
"container/list"
"sync"
)
// Thread-safe producer/consumer queue.
type queue[T any] struct {
list *list.List
cond *sync.Cond
}
// Initializes a queue struct
func (q *queue[T]) init() {
q.list = list.New()
q.cond = sync.NewCond(&sync.Mutex{})
}
// Pushes a value into the queue. (Never blocks: the queue has no size limit.)
// Returns false if the queue has been closed.
func (q *queue[T]) push(value T) (ok bool) {
q.cond.L.Lock()
if q.list != nil {
q.list.PushFront(value)
if q.list.Len() == 1 {
q.cond.Signal()
}
ok = true
}
q.cond.L.Unlock()
return ok
}
// Removes the last/oldest value from the queue; if the queue is empty, blocks.
// If the queue is closed while blocking, returns a default-initialized T.
func (q *queue[T]) pull() (result T) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
for q.list != nil && q.list.Len() == 0 {
q.cond.Wait()
}
if q.list != nil {
last := q.list.Back()
q.list.Remove(last)
result = last.Value.(T)
}
return
}
func (q *queue[T]) close() {
q.cond.L.Lock()
if q.list != nil {
q.list = nil
q.cond.Broadcast()
}
q.cond.L.Unlock()
}