once_go120.go 396 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 uint32
  10. m sync.Mutex
  11. }
  12. func Done(once *sync.Once) bool {
  13. // atomic visit sync.Once.done
  14. return atomic.LoadUint32((*uint32)(unsafe.Pointer(once))) == 1
  15. }
  16. func Reset(once *sync.Once) {
  17. o := (*Once)(unsafe.Pointer(once))
  18. o.m.Lock()
  19. defer o.m.Unlock()
  20. atomic.StoreUint32(&o.done, 0)
  21. }