You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

task.go 456 B

12345678910111213141516171819202122232425262728
  1. package task
  2. import "sync"
  3. type TaskBody[TCtx any] interface {
  4. Execute(ctx TCtx, complete func(completing func()))
  5. }
  6. type Task[TCtx any] struct {
  7. body TaskBody[TCtx]
  8. isCompleted bool
  9. waiters []chan any
  10. waiterLock sync.Mutex
  11. }
  12. func (t *Task[TCtx]) Wait() {
  13. t.waiterLock.Lock()
  14. if t.isCompleted {
  15. t.waiterLock.Unlock()
  16. return
  17. }
  18. waiter := make(chan any)
  19. t.waiters = append(t.waiters, waiter)
  20. t.waiterLock.Unlock()
  21. <-waiter
  22. }

公共库

Contributors (1)