-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclicbarrier.go
94 lines (78 loc) · 1.97 KB
/
cyclicbarrier.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cyclicbarrier
// Package cyclicbarrier provides an implementation of Cyclic Barrier primitive.
import (
"sync"
)
// CyclicBarrier is a synchronizer that allows a set of goroutines to wait for each other
// to reach a common execution point, also called a barrier.
// CyclicBarriers are useful in programs involving a fixed sized party of goroutines
// that must occasionally wait for each other.
type CyclicBarrier struct {
lock sync.RWMutex
p int
n int
b chan struct{}
broken bool
}
// New initializes a new instance of the CyclicBarrier,
// specifying the number of parties
func New(parties int) *CyclicBarrier {
return &CyclicBarrier{
lock: sync.RWMutex{},
p: parties,
n: parties,
b: make(chan struct{}),
broken: false,
}
}
func (cb *CyclicBarrier) reset(broken bool) {
cb.lock.Lock()
defer cb.lock.Unlock()
cb.broken = broken
close(cb.b)
cb.b = make(chan struct{})
}
// Init reinit cyclic barrier for future use
func (cb *CyclicBarrier) Init() {
cb.lock.Lock()
defer cb.lock.Unlock()
cb.n = cb.p
close(cb.b)
cb.b = make(chan struct{})
}
// Await waits until all parties have invoked await on this barrier.
func (cb *CyclicBarrier) Await() {
cb.lock.Lock()
cb.n--
n := cb.n
b := cb.b
cb.lock.Unlock()
if n > 0 {
<-b
} else {
cb.reset(false)
}
}
// GetParties returns the itotal number of parties.
func (cb *CyclicBarrier) GetParties() int {
cb.lock.RLock()
defer cb.lock.RUnlock()
return cb.p
}
// GetWaiting returns the number of parties currently waiting at the barrier.
func (cb *CyclicBarrier) GetWaiting() int {
cb.lock.RLock()
defer cb.lock.RUnlock()
return cb.n
}
// IsBroken queries if this barrier is in a broken state.
func (cb *CyclicBarrier) IsBroken() bool {
cb.lock.RLock()
defer cb.lock.RUnlock()
return cb.broken
}
// BreakBarrier set barrier to broken state, all clients Await will be ended
// For broken state check IsBroken() result
func (cb *CyclicBarrier) BreakBarrier() {
cb.reset(true)
}