-
Notifications
You must be signed in to change notification settings - Fork 175
/
barrier.go
37 lines (31 loc) · 835 Bytes
/
barrier.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
package main
import (
"sync"
)
// Direct import of https://github.com/pwaller/barrier/blob/master/barrier.go
// The zero of Barrier is a ready-to-use value
type Barrier struct {
channel chan struct{}
fall, initialize sync.Once
FallHook func()
}
func (b *Barrier) init() {
b.initialize.Do(func() { b.channel = make(chan struct{}) })
}
// `b.Fall()` can be called any number of times and causes the channel returned
// by `b.Barrier()` to become closed (permanently available for immediate reading)
func (b *Barrier) Fall() {
b.init()
b.fall.Do(func() {
if b.FallHook != nil {
b.FallHook()
}
close(b.channel)
})
}
// When `b.Fall()` is called, the channel returned by Barrier() is closed
// (and becomes always readable)
func (b *Barrier) Barrier() <-chan struct{} {
b.init()
return b.channel
}