-
Notifications
You must be signed in to change notification settings - Fork 19
/
flag.go
51 lines (44 loc) · 872 Bytes
/
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
40
41
42
43
44
45
46
47
48
49
50
51
package skipset
import "sync/atomic"
const (
fullyLinked = 1 << iota
marked
)
type bitflag struct {
data uint32
}
func (f *bitflag) SetTrue(flags uint32) {
for {
old := atomic.LoadUint32(&f.data)
if old&flags != flags {
// Flag is 0, need set it to 1.
n := old | flags
if atomic.CompareAndSwapUint32(&f.data, old, n) {
return
}
continue
}
return
}
}
func (f *bitflag) SetFalse(flags uint32) {
for {
old := atomic.LoadUint32(&f.data)
check := old & flags
if check != 0 {
// Flag is 1, need set it to 0.
n := old ^ check
if atomic.CompareAndSwapUint32(&f.data, old, n) {
return
}
continue
}
return
}
}
func (f *bitflag) Get(flag uint32) bool {
return (atomic.LoadUint32(&f.data) & flag) != 0
}
func (f *bitflag) MGet(check, expect uint32) bool {
return (atomic.LoadUint32(&f.data) & check) == expect
}