Newer
Older
// Synchronization aid that lets goroutines wait for some
// job to finish. Similar to sync.Once.
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
type waitingroom struct {
initer sync.Once
cond *sync.Cond
released bool
}
// Wait for anyone to call Release.
func (wr *waitingroom) Wait() {
wr.initialize()
wr.cond.L.Lock()
defer wr.cond.L.Unlock()
for !wr.released {
wr.cond.Wait()
}
}
// Release all goroutines currently stuck in Wait. Also
// results in all later calls to Wait to simply not wait
// at all.
func (wr *waitingroom) Release() {
wr.initialize()
wr.cond.L.Lock()
defer wr.cond.L.Unlock()
wr.released = true
wr.cond.Broadcast()
}
// Initialize if necessary.
func (wr *waitingroom) initialize() {
wr.initer.Do(func() {
wr.cond = sync.NewCond(&sync.Mutex{})
wr.released = false
})
}