github.com/lingyao2333/mo-zero@v1.4.1/core/syncx/onceguard.go (about)

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