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