-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
98 lines (84 loc) · 1.87 KB
/
status.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
95
96
97
98
package orbyte
import (
"runtime"
"strconv"
"strings"
"sync/atomic"
)
const (
statusisbuffered = 1 << iota
statusdestroyed
statusinsyncop
)
type status uintptr
var destroyedstatus status
func init() {
destroyedstatus.setdestroyed(true)
}
func getGoroutineID() int64 {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(string(buf[:n]))[1]
id, err := strconv.ParseInt(idField, 10, 64)
if err != nil {
panic(err)
}
return id
}
func (c status) mask(v bool, typ uintptr) (news status) {
news = c
if v {
news |= status(typ)
} else {
news &= ^status(typ)
}
return
}
func (c *status) setbool(v bool, typ uintptr) {
olds := atomic.LoadUintptr((*uintptr)(c))
oldv := olds&typ != 0
if oldv == v {
return
}
news := status(olds).mask(v, typ)
for !atomic.CompareAndSwapUintptr((*uintptr)(c), olds, uintptr(news)) {
olds = atomic.LoadUintptr((*uintptr)(c))
news = status(olds).mask(v, typ)
}
}
// setboolunique return false on non-unique set
func (c *status) setboolunique(v bool, typ uintptr) bool {
olds := atomic.LoadUintptr((*uintptr)(c))
oldv := olds&typ != 0
if oldv == v {
return false
}
news := status(olds).mask(v, typ)
for !atomic.CompareAndSwapUintptr((*uintptr)(c), olds, uintptr(news)) {
olds = atomic.LoadUintptr((*uintptr)(c))
oldv = olds&typ != 0
if oldv == v {
return false
}
news = status(olds).mask(v, typ)
}
return true
}
func (c *status) loadbool(typ uintptr) bool {
return atomic.LoadUintptr((*uintptr)(c))&typ != 0
}
func (c *status) isbuffered() bool {
return c.loadbool(statusisbuffered)
}
func (c *status) setbuffered(v bool) {
c.setbool(v, statusisbuffered)
}
func (c *status) hasdestroyed() bool {
return c.loadbool(statusdestroyed)
}
func (c *status) setdestroyed(v bool) {
c.setbool(v, statusdestroyed)
}
func (c *status) setinsyncop(v bool) bool {
return c.setboolunique(v, statusinsyncop)
}