onceguard.go 246 B

123456789101112131415
  1. package syncx
  2. import "sync/atomic"
  3. type OnceGuard struct {
  4. done uint32
  5. }
  6. func (og *OnceGuard) Taken() bool {
  7. return atomic.LoadUint32(&og.done) == 1
  8. }
  9. func (og *OnceGuard) Take() bool {
  10. return atomic.CompareAndSwapUint32(&og.done, 0, 1)
  11. }