summaryrefslogtreecommitdiffhomepage
path: root/device/misc.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 04:04:41 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 05:00:40 +0100
commit69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03 (patch)
tree1ef86da3242afde462dcadb7241bb09f499d5bd7 /device/misc.go
parentd435be35cac49af9367b2005d831d55e570c4b1b (diff)
global: begin modularization
Diffstat (limited to 'device/misc.go')
-rw-r--r--device/misc.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/device/misc.go b/device/misc.go
new file mode 100644
index 0000000..a38d1c1
--- /dev/null
+++ b/device/misc.go
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "sync/atomic"
+)
+
+/* Atomic Boolean */
+
+const (
+ AtomicFalse = int32(iota)
+ AtomicTrue
+)
+
+type AtomicBool struct {
+ int32
+}
+
+func (a *AtomicBool) Get() bool {
+ return atomic.LoadInt32(&a.int32) == AtomicTrue
+}
+
+func (a *AtomicBool) Swap(val bool) bool {
+ flag := AtomicFalse
+ if val {
+ flag = AtomicTrue
+ }
+ return atomic.SwapInt32(&a.int32, flag) == AtomicTrue
+}
+
+func (a *AtomicBool) Set(val bool) {
+ flag := AtomicFalse
+ if val {
+ flag = AtomicTrue
+ }
+ atomic.StoreInt32(&a.int32, flag)
+}
+
+func min(a, b uint) uint {
+ if a > b {
+ return b
+ }
+ return a
+}