1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
Dušan Borovčanin 8b94199785
Update dependencies (#1564)
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2022-02-12 19:07:48 +01:00

36 lines
641 B
Go

package sync
import (
"sync"
"sync/atomic"
)
// Once performs exactly one action.
// See sync.Once for more details.
type Once struct {
m sync.Mutex
done uint32
}
// Done returns true after Try executes succesfully.
func (o *Once) Done() bool {
return atomic.LoadUint32(&o.done) == 1
}
// Try executes the function f exactly once for this instance of Once.
// If the function f returns false, it enables further execution attempts.
func (o *Once) Try(f func() bool) {
if atomic.LoadUint32(&o.done) == 1 {
return
}
o.m.Lock()
defer o.m.Unlock()
if o.done == 1 {
return
}
if f() {
atomic.StoreUint32(&o.done, 1)
}
}