2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2019-01-02 00:55:51 +00:00
|
|
|
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
package main
|
|
|
|
|
2017-07-01 21:29:22 +00:00
|
|
|
import (
|
2017-08-11 14:18:20 +00:00
|
|
|
"sync/atomic"
|
2017-07-01 21:29:22 +00:00
|
|
|
)
|
|
|
|
|
2017-12-01 22:37:26 +00:00
|
|
|
/* Atomic Boolean */
|
|
|
|
|
2017-07-08 21:51:26 +00:00
|
|
|
const (
|
2017-08-11 14:18:20 +00:00
|
|
|
AtomicFalse = int32(iota)
|
2017-07-08 21:51:26 +00:00
|
|
|
AtomicTrue
|
|
|
|
)
|
|
|
|
|
2017-08-11 14:18:20 +00:00
|
|
|
type AtomicBool struct {
|
|
|
|
flag int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AtomicBool) Get() bool {
|
|
|
|
return atomic.LoadInt32(&a.flag) == AtomicTrue
|
|
|
|
}
|
|
|
|
|
2017-11-17 16:25:45 +00:00
|
|
|
func (a *AtomicBool) Swap(val bool) bool {
|
|
|
|
flag := AtomicFalse
|
|
|
|
if val {
|
|
|
|
flag = AtomicTrue
|
|
|
|
}
|
|
|
|
return atomic.SwapInt32(&a.flag, flag) == AtomicTrue
|
|
|
|
}
|
|
|
|
|
2017-08-11 14:18:20 +00:00
|
|
|
func (a *AtomicBool) Set(val bool) {
|
|
|
|
flag := AtomicFalse
|
|
|
|
if val {
|
|
|
|
flag = AtomicTrue
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&a.flag, flag)
|
|
|
|
}
|
|
|
|
|
2018-05-13 22:28:30 +00:00
|
|
|
func min(a, b uint) uint {
|
2017-05-30 20:36:49 +00:00
|
|
|
if a > b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|