mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-11 21:51:18 +08:00
init
This commit is contained in:
59
status.go
Normal file
59
status.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package orbyte
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
const (
|
||||
statusisbuffered = 1 << iota
|
||||
statusdestroyed
|
||||
)
|
||||
|
||||
type status uintptr
|
||||
|
||||
var destroyedstatus status
|
||||
|
||||
func init() {
|
||||
destroyedstatus.setdestroyed(true)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user