Add global idler and other helpers #2

Merged
balki merged 5 commits from globalIdler into main 2023-09-11 20:45:52 -04:00
Showing only changes of commit 8ca82581fe - Show all commits

View File

@ -2,10 +2,32 @@
package idle package idle
import ( import (
"fmt"
"sync/atomic" "sync/atomic"
"time" "time"
) )
var (
gIdler atomic.Pointer[idler]
)
func Wait(timeout time.Duration) error {
i := CreateIdler(timeout).(*idler)
ok := gIdler.CompareAndSwap(nil, i)
if !ok {
return fmt.Errorf("idler already waiting")
}
i.Wait()
return nil
}
func Tick() {
i := gIdler.Load()
if i != nil {
i.Tick()
}
}
type Idler interface { type Idler interface {
Enter() Enter()
Exit() Exit()