-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondlist.go
More file actions
41 lines (35 loc) · 769 Bytes
/
Copy pathcondlist.go
File metadata and controls
41 lines (35 loc) · 769 Bytes
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
package queue
import (
"container/list"
"sync"
)
// An implementation of Queue using a double linked list protected by
// a condition variable.
type CondListQueue struct {
// Condition Variable protecting access this `q`.
cond *sync.Cond
// Elements in the queue.
q *list.List
}
func NewCondListQueue() *CondListQueue {
return &CondListQueue{
cond: &sync.Cond{L: &sync.Mutex{}},
q: list.New(),
}
}
func (this *CondListQueue) PopFront() interface{} {
this.cond.L.Lock()
defer this.cond.L.Unlock()
for this.q.Len() == 0 {
this.cond.Wait()
}
e := this.q.Front()
defer this.q.Remove(e)
return e.Value
}
func (this *CondListQueue) PushBack(v interface{}) {
this.cond.L.Lock()
defer this.cond.L.Unlock()
this.q.PushBack(v)
this.cond.Signal()
}