github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/atomicbitops/atomicbitops_noasm.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !amd64,!arm64
    16  
    17  package atomicbitops
    18  
    19  import (
    20  	"sync/atomic"
    21  )
    22  
    23  func AndUint32(addr *uint32, val uint32) {
    24  	for {
    25  		o := atomic.LoadUint32(addr)
    26  		n := o & val
    27  		if atomic.CompareAndSwapUint32(addr, o, n) {
    28  			break
    29  		}
    30  	}
    31  }
    32  
    33  func OrUint32(addr *uint32, val uint32) {
    34  	for {
    35  		o := atomic.LoadUint32(addr)
    36  		n := o | val
    37  		if atomic.CompareAndSwapUint32(addr, o, n) {
    38  			break
    39  		}
    40  	}
    41  }
    42  
    43  func XorUint32(addr *uint32, val uint32) {
    44  	for {
    45  		o := atomic.LoadUint32(addr)
    46  		n := o ^ val
    47  		if atomic.CompareAndSwapUint32(addr, o, n) {
    48  			break
    49  		}
    50  	}
    51  }
    52  
    53  func CompareAndSwapUint32(addr *uint32, old, new uint32) (prev uint32) {
    54  	for {
    55  		prev = atomic.LoadUint32(addr)
    56  		if prev != old {
    57  			return
    58  		}
    59  		if atomic.CompareAndSwapUint32(addr, old, new) {
    60  			return
    61  		}
    62  	}
    63  }
    64  
    65  func AndUint64(addr *uint64, val uint64) {
    66  	for {
    67  		o := atomic.LoadUint64(addr)
    68  		n := o & val
    69  		if atomic.CompareAndSwapUint64(addr, o, n) {
    70  			break
    71  		}
    72  	}
    73  }
    74  
    75  func OrUint64(addr *uint64, val uint64) {
    76  	for {
    77  		o := atomic.LoadUint64(addr)
    78  		n := o | val
    79  		if atomic.CompareAndSwapUint64(addr, o, n) {
    80  			break
    81  		}
    82  	}
    83  }
    84  
    85  func XorUint64(addr *uint64, val uint64) {
    86  	for {
    87  		o := atomic.LoadUint64(addr)
    88  		n := o ^ val
    89  		if atomic.CompareAndSwapUint64(addr, o, n) {
    90  			break
    91  		}
    92  	}
    93  }
    94  
    95  func CompareAndSwapUint64(addr *uint64, old, new uint64) (prev uint64) {
    96  	for {
    97  		prev = atomic.LoadUint64(addr)
    98  		if prev != old {
    99  			return
   100  		}
   101  		if atomic.CompareAndSwapUint64(addr, old, new) {
   102  			return
   103  		}
   104  	}
   105  }