aboutsummaryrefslogblamecommitdiffstats
path: root/misc.go
blob: 6786cb5633cc95cd19644e58f59d9aa1494fb48b (plain) (tree)
1
2
3
4
5
6
7
8
9
                               
  
                                                              

   

            
        
                     

 

                    
       
                                 


                  
                        
             


                                 
                                                       

 




                                          
                                                             

 




                                    
                                         

 
                          




                        
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
 */

package main

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
}