summaryrefslogtreecommitdiff
path: root/src/util/atomicbool.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-12 12:56:17 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-12 12:56:17 +0900
commitcd847affb79ea6438c9721635724efc6f58e2215 (patch)
treed1e631e3dca8832ee4c495924789f6697c3629cf /src/util/atomicbool.go
parent7a2bc2cada971c7a390d09b0afda34780ff56fb6 (diff)
downloadfzf-cd847affb79ea6438c9721635724efc6f58e2215.tar.gz
Reorganize source code
Diffstat (limited to 'src/util/atomicbool.go')
-rw-r--r--src/util/atomicbool.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/util/atomicbool.go b/src/util/atomicbool.go
new file mode 100644
index 00000000..9e1bdc8f
--- /dev/null
+++ b/src/util/atomicbool.go
@@ -0,0 +1,32 @@
+package util
+
+import "sync"
+
+// AtomicBool is a boxed-class that provides synchronized access to the
+// underlying boolean value
+type AtomicBool struct {
+ mutex sync.Mutex
+ state bool
+}
+
+// NewAtomicBool returns a new AtomicBool
+func NewAtomicBool(initialState bool) *AtomicBool {
+ return &AtomicBool{
+ mutex: sync.Mutex{},
+ state: initialState}
+}
+
+// Get returns the current boolean value synchronously
+func (a *AtomicBool) Get() bool {
+ a.mutex.Lock()
+ defer a.mutex.Unlock()
+ return a.state
+}
+
+// Set updates the boolean value synchronously
+func (a *AtomicBool) Set(newState bool) bool {
+ a.mutex.Lock()
+ defer a.mutex.Unlock()
+ a.state = newState
+ return a.state
+}