Skip to content

Commit

Permalink
refactor: use func to simplify methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Aug 2, 2024
1 parent fa67d0b commit 4153934
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions future.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Future[T any] struct {
func (s *state[T]) set(val T, err error) {
for {
st := atomic.LoadUint64(&s.state)
if ((st & maskState) >> 32) > stateFree {
if !isFree(st) {
panic("promise already satisfied")
}
if atomic.CompareAndSwapUint64(&s.state, st, st+stateDelta) {
Expand Down Expand Up @@ -90,12 +90,12 @@ func (s *state[T]) get() (T, error) {
}
for {
st := atomic.LoadUint64(&s.state)
if ((st & maskState) >> 32) == stateDone {
if isDone(st) {
return s.val, s.err
}
if atomic.CompareAndSwapUint64(&s.state, st, st+1) {
runtime_Semacquire(&s.sema)
if (atomic.LoadUint64(&s.state)&maskState)>>32 != stateDone {
if !isDone(atomic.LoadUint64(&s.state)) {
panic("sync: notified before state has done")
}
return s.val, s.err
Expand All @@ -108,7 +108,7 @@ func (s *state[T]) subscribe(cb func(T, error)) {
for {
oldCb := (*callback[T])(atomic.LoadPointer(&s.stack))

if ((atomic.LoadUint64(&s.state) & maskState) >> 32) == stateDone {
if isDone(atomic.LoadUint64(&s.state)) {
cb(s.val, s.err)
return
}
Expand All @@ -117,7 +117,7 @@ func (s *state[T]) subscribe(cb func(T, error)) {
if atomic.CompareAndSwapPointer(&s.stack, unsafe.Pointer(oldCb), unsafe.Pointer(newCb)) {
for {
// Double-check the state to ensure the callback is not missed
if ((atomic.LoadUint64(&s.state) & maskState) >> 32) == stateDone {
if isDone(atomic.LoadUint64(&s.state)) {
if atomic.CompareAndSwapPointer(&s.stack, unsafe.Pointer(newCb), unsafe.Pointer(newCb.next)) {
cb(s.val, s.err)
return
Expand Down Expand Up @@ -158,6 +158,14 @@ func (f *Future[T]) Subscribe(cb func(val T, err error)) {
f.state.subscribe(cb)
}

func isFree(st uint64) bool {
return ((st & maskState) >> 32) == stateFree
}

func isDone(st uint64) bool {
return ((st & maskState) >> 32) == stateDone
}

// noCopy may be embedded into structs which must not be copied
// after the first use.
//
Expand Down

0 comments on commit 4153934

Please sign in to comment.