-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
88 lines (79 loc) · 1.46 KB
/
event.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
package event
import (
"sync"
)
type Event struct {
WaitChan chan struct{}
waitGroup sync.WaitGroup
waitCount sync.WaitGroup
isSet bool
isClosed bool
lock sync.RWMutex
isClosedLock sync.RWMutex
}
func New(withChan ...bool) *Event {
e := &Event{}
e.Set()
if len(withChan) > 0 && withChan[0] {
e.WaitChan = make(chan struct{})
go e.handleWaitChan()
}
return e
}
// Set makes Wait() block
func (e *Event) Set() {
e.lock.Lock()
if !e.isSet {
// wait for all Event waiter to finish, because number of waitGroup waiter must be 0 when Adding
e.waitCount.Wait()
e.isSet = true
e.waitGroup.Add(1)
}
e.lock.Unlock()
}
// Clear makes Wait() not block
func (e *Event) Clear() {
e.lock.Lock()
if e.isSet {
e.isSet = false
e.waitGroup.Done()
}
e.lock.Unlock()
}
// Wait blocks until Clear() called
func (e *Event) Wait() {
// avoid racing with Set()
e.lock.RLock()
e.waitCount.Add(1)
e.lock.RUnlock()
e.waitGroup.Wait()
e.waitCount.Done()
}
func (e *Event) IsSet() bool {
e.lock.RLock()
isSet := e.isSet
e.lock.RUnlock()
return isSet
}
func (e *Event) Close() {
e.Clear()
e.isClosedLock.Lock()
e.isClosed = true
e.isClosedLock.Unlock()
if e.WaitChan != nil {
<-e.WaitChan
}
}
func (e *Event) handleWaitChan() {
for {
e.Wait()
// a struct must have be sent before breaking
e.WaitChan <- struct{}{}
e.isClosedLock.RLock()
isClosed := e.isClosed
e.isClosedLock.RUnlock()
if isClosed {
break
}
}
}