onceguard.go 428 B

123456789101112131415161718
  1. package syncx
  2. import "sync/atomic"
  3. // A OnceGuard is used to make sure a resource can be taken once.
  4. type OnceGuard struct {
  5. done uint32
  6. }
  7. // Taken checks if the resource is taken.
  8. func (og *OnceGuard) Taken() bool {
  9. return atomic.LoadUint32(&og.done) == 1
  10. }
  11. // Take takes the resource, returns true on success, false for otherwise.
  12. func (og *OnceGuard) Take() bool {
  13. return atomic.CompareAndSwapUint32(&og.done, 0, 1)
  14. }