2020-07-26 17:09:05 +08:00
|
|
|
package syncx
|
|
|
|
|
|
|
|
import "sync/atomic"
|
|
|
|
|
2021-05-10 00:09:00 +08:00
|
|
|
// A OnceGuard is used to make sure a resource can be taken once.
|
2020-07-26 17:09:05 +08:00
|
|
|
type OnceGuard struct {
|
|
|
|
done uint32
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:16:22 +08:00
|
|
|
// Taken checks if the resource is taken.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (og *OnceGuard) Taken() bool {
|
|
|
|
return atomic.LoadUint32(&og.done) == 1
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:16:22 +08:00
|
|
|
// Take takes the resource, returns true on success, false for otherwise.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (og *OnceGuard) Take() bool {
|
|
|
|
return atomic.CompareAndSwapUint32(&og.done, 0, 1)
|
|
|
|
}
|