1
0
mirror of https://github.com/fumiama/orbyte.git synced 2026-06-05 02:00:30 +08:00
Files
orbyte/status.go
2025-05-12 23:59:04 +09:00

108 lines
2.0 KiB
Go

package orbyte
import (
"runtime"
"strconv"
"strings"
"sync/atomic"
)
const (
statusisbuffered = 1 << iota
statusdestroyed
statusinsyncop
statushasignored
)
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)
}
func (c *status) hasignored() bool {
return c.loadbool(statushasignored)
}
func (c *status) setignored(v bool) {
c.setbool(v, statushasignored)
}