1
0
mirror of https://git.zx2c4.com/wireguard-go synced 2024-11-15 01:05:15 +01:00
wireguard-go/src/misc.go
Mathias Hall-Andersen fa399a91d5 Ported remaining netns.sh
- Ported remaining netns.sh tests
- Begin work on generic implementation of bind interface
2017-11-17 17:25:58 +01:00

87 lines
1.2 KiB
Go

package main
import (
"sync/atomic"
"time"
)
/* We use int32 as atomic bools
* (since booleans are not natively supported by sync/atomic)
*/
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)
}
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
}
func signalSend(c chan struct{}) {
select {
case c <- struct{}{}:
default:
}
}
func signalClear(c chan struct{}) {
select {
case <-c:
default:
}
}
func timerStop(timer *time.Timer) {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
}
func NewStoppedTimer() *time.Timer {
timer := time.NewTimer(time.Hour)
timerStop(timer)
return timer
}