Files
wehub-resource-sync 498b235461
Build and test / Build and test AMD64 Ubuntu 22.04 (push) Failing after 0s
Publish Builder / amazonlinux2023 (push) Failing after 1s
Build and test / UT for Go (push) Has been skipped
Publish KRTE Images / KRTE (push) Failing after 1s
Build and test / Integration Test (push) Has been skipped
Build and test / Upload Code Coverage (push) Has been skipped
Publish Builder / rockylinux9 (push) Failing after 1s
Publish Builder / ubuntu22.04 (push) Failing after 0s
Publish Builder / ubuntu24.04 (push) Failing after 0s
Publish Gpu Builder / publish-gpu-builder (push) Failing after 1s
Publish Test Images / PyTest (push) Failing after 0s
Build and test / UT for Cpp (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:17 +08:00

51 lines
1.4 KiB
Go

package syncutil
import "context"
// NewAsyncTaskNotifier creates a new async task notifier.
func NewAsyncTaskNotifier[T any]() *AsyncTaskNotifier[T] {
ctx, cancel := context.WithCancel(context.Background()) //nolint:gosec // G118: cancel is stored in struct and called via Cancel()
return &AsyncTaskNotifier[T]{
ctx: ctx,
cancel: cancel,
future: NewFuture[T](),
}
}
// AsyncTaskNotifier is a notifier for async task.
type AsyncTaskNotifier[T any] struct {
ctx context.Context
cancel context.CancelFunc
future *Future[T]
}
// Context returns the context of the async task.
func (n *AsyncTaskNotifier[T]) Context() context.Context {
return n.ctx
}
// Cancel cancels the async task, the async task can receive the cancel signal from Context.
func (n *AsyncTaskNotifier[T]) Cancel() {
n.cancel()
}
// BlockAndGetResult returns the result of the async task.
func (n *AsyncTaskNotifier[T]) BlockAndGetResult() T {
return n.future.Get()
}
// BlockUntilFinish blocks until the async task is finished.
func (n *AsyncTaskNotifier[T]) BlockUntilFinish() {
<-n.future.Done()
}
// FinishChan returns a channel that will be closed when the async task is finished.
func (n *AsyncTaskNotifier[T]) FinishChan() <-chan struct{} {
return n.future.Done()
}
// Finish finishes the async task with a result.
func (n *AsyncTaskNotifier[T]) Finish(result T) {
n.future.Set(result)
}