-
Notifications
You must be signed in to change notification settings - Fork 7
/
sync_flag.go
39 lines (32 loc) · 853 Bytes
/
sync_flag.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
package nsync
import (
"sync"
"sync/atomic"
)
// SyncFlag implements a boolean flag that can be set or unset atomically.
// During set/unset SyncFlag locks the mutex, so if anything needs to prevent
// a flag from being set/unset should acquire a lock.
type SyncFlag struct {
sync.Mutex
flag int32
}
// Set locks the mutex, sets the flag and unlocks the mutex.
func (bf *SyncFlag) Set() {
bf.Lock()
atomic.StoreInt32(&bf.flag, 1)
bf.Unlock()
}
// Set locks the mutex, reset the flag and unlocks the mutex.
func (bf *SyncFlag) Unset() {
bf.Lock()
atomic.StoreInt32(&bf.flag, 0)
bf.Unlock()
}
// IsSet atomically checks if flag is set.
func (bf *SyncFlag) IsSet() bool {
return atomic.LoadInt32(&bf.flag) > 0
}
// IsUnset atomically checks if flag is unset.
func (bf *SyncFlag) IsUnset() bool {
return atomic.LoadInt32(&bf.flag) == 0
}