github.com/tailscale/wireguard-go@v0.0.20201119-0.20210522003738-46b531feb08a/device/misc.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package device
     7  
     8  import (
     9  	"sync/atomic"
    10  )
    11  
    12  /* Atomic Boolean */
    13  
    14  const (
    15  	AtomicFalse = int32(iota)
    16  	AtomicTrue
    17  )
    18  
    19  type AtomicBool struct {
    20  	int32
    21  }
    22  
    23  func (a *AtomicBool) Get() bool {
    24  	return atomic.LoadInt32(&a.int32) == AtomicTrue
    25  }
    26  
    27  func (a *AtomicBool) Swap(val bool) bool {
    28  	flag := AtomicFalse
    29  	if val {
    30  		flag = AtomicTrue
    31  	}
    32  	return atomic.SwapInt32(&a.int32, flag) == AtomicTrue
    33  }
    34  
    35  func (a *AtomicBool) Set(val bool) {
    36  	flag := AtomicFalse
    37  	if val {
    38  		flag = AtomicTrue
    39  	}
    40  	atomic.StoreInt32(&a.int32, flag)
    41  }
    42  
    43  func min(a, b uint) uint {
    44  	if a > b {
    45  		return b
    46  	}
    47  	return a
    48  }