summaryrefslogtreecommitdiffhomepage
path: root/misc.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-04 16:08:26 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2018-02-04 16:08:26 +0100
commita0f54cbe5ac2cd8b8296c2c57c30029dd349cff0 (patch)
tree64574090d79ff3899c5c18e5268e450028e4656b /misc.go
parent5871ec04deb8f4715cab37146940baa35c08cbee (diff)
Align with go library layout
Diffstat (limited to 'misc.go')
-rw-r--r--misc.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/misc.go b/misc.go
new file mode 100644
index 0000000..80e33f6
--- /dev/null
+++ b/misc.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "sync/atomic"
+)
+
+/* Atomic Boolean */
+
+const (
+ AtomicFalse = int32(iota)
+ AtomicTrue
+)
+
+type AtomicBool struct {
+ flag int32
+}
+
+func (a *AtomicBool) Get() bool {
+ return atomic.LoadInt32(&a.flag) == AtomicTrue
+}
+
+func (a *AtomicBool) Swap(val bool) bool {
+ flag := AtomicFalse
+ if val {
+ flag = AtomicTrue
+ }
+ return atomic.SwapInt32(&a.flag, flag) == AtomicTrue
+}
+
+func (a *AtomicBool) Set(val bool) {
+ flag := AtomicFalse
+ if val {
+ flag = AtomicTrue
+ }
+ atomic.StoreInt32(&a.flag, flag)
+}
+
+/* Integer manipulation */
+
+func toInt32(n uint32) int32 {
+ mask := uint32(1 << 31)
+ return int32(-(n & mask) + (n & ^mask))
+}
+
+func min(a uint, b uint) uint {
+ if a > b {
+ return b
+ }
+ return a
+}
+
+func minUint64(a uint64, b uint64) uint64 {
+ if a > b {
+ return b
+ }
+ return a
+}