-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48ed296
commit 7a7b23c
Showing
2 changed files
with
63 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,50 @@ | ||
package atomic | ||
|
||
import "sync/atomic" | ||
import ( | ||
"sync" | ||
) | ||
|
||
// Flag is an atomic flag | ||
type Flag struct { | ||
value uint32 | ||
mut sync.RWMutex | ||
value bool | ||
} | ||
|
||
// SetReturningPrevious sets flag and returns its previous value | ||
func (flag *Flag) SetReturningPrevious() bool { | ||
previousValue := atomic.SwapUint32(&flag.value, 1) | ||
return previousValue == 1 | ||
flag.mut.Lock() | ||
previousValue := flag.value | ||
flag.value = true | ||
flag.mut.Unlock() | ||
|
||
return previousValue | ||
} | ||
|
||
// Reset resets the flag, putting it in off position | ||
func (flag *Flag) Reset() { | ||
atomic.StoreUint32(&flag.value, 0) | ||
flag.mut.Lock() | ||
flag.value = false | ||
flag.mut.Unlock() | ||
} | ||
|
||
// IsSet checks whether flag is set | ||
func (flag *Flag) IsSet() bool { | ||
value := atomic.LoadUint32(&flag.value) | ||
return value == 1 | ||
flag.mut.RLock() | ||
defer flag.mut.RUnlock() | ||
|
||
return flag.value | ||
} | ||
|
||
// Toggle toggles the flag | ||
func (flag *Flag) Toggle(set bool) { | ||
if set { | ||
_ = flag.SetReturningPrevious() | ||
} else { | ||
flag.Reset() | ||
} | ||
func (flag *Flag) Toggle() { | ||
flag.mut.Lock() | ||
flag.value = !flag.value | ||
flag.mut.Unlock() | ||
} | ||
|
||
// SetValue sets the new value in the flag | ||
func (flag *Flag) SetValue(newValue bool) { | ||
flag.mut.Lock() | ||
flag.value = newValue | ||
flag.mut.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters