once_go122.go 382 B

1234567891011121314151617181920212223242526
  1. //go:build go1.22
  2. package once
  3. import (
  4. "sync"
  5. "sync/atomic"
  6. "unsafe"
  7. )
  8. type Once struct {
  9. done atomic.Uint32
  10. m sync.Mutex
  11. }
  12. func Done(once *sync.Once) bool {
  13. // atomic visit sync.Once.done
  14. return (*atomic.Uint32)(unsafe.Pointer(once)).Load() == 1
  15. }
  16. func Reset(once *sync.Once) {
  17. o := (*Once)(unsafe.Pointer(once))
  18. o.m.Lock()
  19. defer o.m.Unlock()
  20. o.done.Store(0)
  21. }