From 4153934cd401edd654813c121861a788a28773fe Mon Sep 17 00:00:00 2001 From: jizhuozhi Date: Sat, 3 Aug 2024 03:04:20 +0800 Subject: [PATCH] refactor: use func to simplify methods --- future.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/future.go b/future.go index be0dbd9..8ae2a09 100644 --- a/future.go +++ b/future.go @@ -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) { @@ -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 @@ -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 } @@ -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 @@ -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. //