fix: add future stateful lock (#36332)

issue: #36323

Signed-off-by: chyezh <chyezh@outlook.com>
pull/36266/head
Zhen Ye 2024-09-18 20:15:11 +08:00 committed by GitHub
parent a03397ba70
commit 47da9023a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 136 additions and 48 deletions

View File

@ -91,13 +91,12 @@ func Async(ctx context.Context, f CGOAsyncFunction, opts ...Opt) Future {
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
future := &futureImpl{ future := &futureImpl{
closure: f, closure: f,
ctx: ctx, ctx: ctx,
ctxCancel: cancel, ctxCancel: cancel,
releaserOnce: sync.Once{}, future: cFuturePtr,
future: cFuturePtr, opts: options,
opts: options, state: newFutureState(),
state: newFutureState(),
} }
// register the future to do timeout notification. // register the future to do timeout notification.
@ -106,29 +105,33 @@ func Async(ctx context.Context, f CGOAsyncFunction, opts ...Opt) Future {
} }
type futureImpl struct { type futureImpl struct {
ctx context.Context ctx context.Context
ctxCancel context.CancelFunc ctxCancel context.CancelFunc
future *C.CFuture future *C.CFuture
closure CGOAsyncFunction closure CGOAsyncFunction
opts *options opts *options
state futureState state futureState
releaserOnce sync.Once
} }
// Context return the context of the future.
func (f *futureImpl) Context() context.Context { func (f *futureImpl) Context() context.Context {
return f.ctx return f.ctx
} }
// BlockUntilReady block until the future is ready or canceled.
func (f *futureImpl) BlockUntilReady() { func (f *futureImpl) BlockUntilReady() {
f.blockUntilReady() f.blockUntilReady()
} }
// BlockAndLeakyGet block until the future is ready or canceled, and return the leaky result.
func (f *futureImpl) BlockAndLeakyGet() (unsafe.Pointer, error) { func (f *futureImpl) BlockAndLeakyGet() (unsafe.Pointer, error) {
f.blockUntilReady() f.blockUntilReady()
if !f.state.intoConsumed() { guard := f.state.LockForConsume()
if guard == nil {
return nil, ErrConsumed return nil, ErrConsumed
} }
defer guard.Unlock()
var ptr unsafe.Pointer var ptr unsafe.Pointer
var status C.CStatus var status C.CStatus
@ -144,21 +147,31 @@ func (f *futureImpl) BlockAndLeakyGet() (unsafe.Pointer, error) {
return ptr, err return ptr, err
} }
// Release the resource of the future.
func (f *futureImpl) Release() { func (f *futureImpl) Release() {
// block until ready to release the future. // block until ready to release the future.
f.blockUntilReady() f.blockUntilReady()
guard := f.state.LockForRelease()
if guard == nil {
return
}
defer guard.Unlock()
// release the future. // release the future.
getCGOCaller().call("future_destroy", func() { getCGOCaller().call("future_destroy", func() {
C.future_destroy(f.future) C.future_destroy(f.future)
}) })
} }
// cancel the future with error.
func (f *futureImpl) cancel(err error) { func (f *futureImpl) cancel(err error) {
if !f.state.checkUnready() { // only unready future can be canceled.
// only unready future can be canceled. guard := f.state.LockForCancel()
// a ready future' cancel make no sense. if guard == nil {
return return
} }
defer guard.Unlock()
if errors.IsAny(err, context.DeadlineExceeded, context.Canceled) { if errors.IsAny(err, context.DeadlineExceeded, context.Canceled) {
getCGOCaller().call("future_cancel", func() { getCGOCaller().call("future_cancel", func() {
@ -169,8 +182,9 @@ func (f *futureImpl) cancel(err error) {
panic("unreachable: invalid cancel error type") panic("unreachable: invalid cancel error type")
} }
// blockUntilReady block until the future is ready or canceled.
func (f *futureImpl) blockUntilReady() { func (f *futureImpl) blockUntilReady() {
if !f.state.checkUnready() { if !f.state.CheckUnready() {
// only unready future should be block until ready. // only unready future should be block until ready.
return return
} }
@ -183,10 +197,7 @@ func (f *futureImpl) blockUntilReady() {
mu.Lock() mu.Lock()
// mark the future as ready at go side to avoid more cgo calls. // mark the future as ready at go side to avoid more cgo calls.
f.state.intoReady() f.state.IntoReady()
// notify the future manager that the future is ready. // notify the future manager that the future is ready.
f.ctxCancel() f.ctxCancel()
if f.opts.releaser != nil {
f.releaserOnce.Do(f.opts.releaser)
}
} }

View File

@ -25,6 +25,32 @@ func TestMain(m *testing.M) {
} }
} }
func TestFutureWithConcurrentReleaseAndCancel(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i < 20; i++ {
future := createFutureWithTestCase(context.Background(), testCase{
interval: 100 * time.Millisecond,
loopCnt: 10,
caseNo: 100,
})
wg.Add(3)
// Double release should be ok.
go func() {
defer wg.Done()
future.Release()
}()
go func() {
defer wg.Done()
future.Release()
}()
go func() {
defer wg.Done()
future.cancel(context.DeadlineExceeded)
}()
}
wg.Wait()
}
func TestFutureWithSuccessCase(t *testing.T) { func TestFutureWithSuccessCase(t *testing.T) {
// Test success case. // Test success case.
future := createFutureWithTestCase(context.Background(), testCase{ future := createFutureWithTestCase(context.Background(), testCase{

View File

@ -2,27 +2,17 @@ package cgo
func getDefaultOpt() *options { func getDefaultOpt() *options {
return &options{ return &options{
name: "unknown", name: "unknown",
releaser: nil,
} }
} }
type options struct { type options struct {
name string name string
releaser func()
} }
// Opt is the option type for future. // Opt is the option type for future.
type Opt func(*options) type Opt func(*options)
// WithReleaser sets the releaser function.
// When a future is ready, the releaser function will be called once.
func WithReleaser(releaser func()) Opt {
return func(o *options) {
o.releaser = releaser
}
}
// WithName sets the name of the future. // WithName sets the name of the future.
// Only used for metrics. // Only used for metrics.
func WithName(name string) Opt { func WithName(name string) Opt {

View File

@ -1,38 +1,99 @@
package cgo package cgo
import "go.uber.org/atomic" import (
"sync"
)
const ( const (
stateUnready int32 = iota stateUnready state = iota
stateReady stateReady
stateConsumed stateConsumed
stateDestoryed
) )
// newFutureState creates a new futureState. // newFutureState creates a new futureState.
func newFutureState() futureState { func newFutureState() futureState {
return futureState{ return futureState{
inner: atomic.NewInt32(stateUnready), mu: sync.Mutex{},
inner: stateUnready,
} }
} }
type state int32
// futureState is a state machine for future. // futureState is a state machine for future.
// unready --BlockUntilReady--> ready --BlockAndLeakyGet--> consumed // unready --BlockUntilReady--> ready --BlockAndLeakyGet--> consumed
type futureState struct { type futureState struct {
inner *atomic.Int32 mu sync.Mutex
inner state
} }
// intoReady sets the state to ready. // LockForCancel locks the state for cancel.
func (s *futureState) intoReady() { func (s *futureState) LockForCancel() *lockGuard {
s.inner.CompareAndSwap(stateUnready, stateReady) s.mu.Lock()
// only unready future can be canceled.
// cancel on a ready future make no sense.
if s.inner != stateUnready {
s.mu.Unlock()
return nil
}
return &lockGuard{
locker: s,
target: stateUnready,
}
} }
// intoConsumed sets the state to consumed. // LockForConsume locks the state for consume.
// if the state is not ready, it does nothing and returns false. func (s *futureState) LockForConsume() *lockGuard {
func (s *futureState) intoConsumed() bool { s.mu.Lock()
return s.inner.CompareAndSwap(stateReady, stateConsumed) if s.inner != stateReady {
s.mu.Unlock()
return nil
}
return &lockGuard{
locker: s,
target: stateConsumed,
}
}
// LockForRelease locks the state for release.
func (s *futureState) LockForRelease() *lockGuard {
s.mu.Lock()
if s.inner != stateReady && s.inner != stateConsumed {
s.mu.Unlock()
return nil
}
return &lockGuard{
locker: s,
target: stateDestoryed,
}
} }
// checkUnready checks if the state is unready. // checkUnready checks if the state is unready.
func (s *futureState) checkUnready() bool { func (s *futureState) CheckUnready() bool {
return s.inner.Load() == stateUnready s.mu.Lock()
defer s.mu.Unlock()
return s.inner == stateUnready
}
// IntoReady changes the state to ready.
func (s *futureState) IntoReady() {
s.mu.Lock()
if s.inner == stateUnready {
s.inner = stateReady
}
s.mu.Unlock()
}
// lockGuard is a guard for futureState.
type lockGuard struct {
locker *futureState
target state
}
// Unlock unlocks the state.
func (lg *lockGuard) Unlock() {
lg.locker.inner = lg.target
lg.locker.mu.Unlock()
} }