inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/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 //go:build !amd64 && !arm64 16 // +build !amd64,!arm64 17 18 package atomicbitops 19 20 import ( 21 "sync/atomic" 22 ) 23 24 //go:nosplit 25 func AndUint32(addr *uint32, val uint32) { 26 for { 27 o := atomic.LoadUint32(addr) 28 n := o & val 29 if atomic.CompareAndSwapUint32(addr, o, n) { 30 break 31 } 32 } 33 } 34 35 //go:nosplit 36 func OrUint32(addr *uint32, val uint32) { 37 for { 38 o := atomic.LoadUint32(addr) 39 n := o | val 40 if atomic.CompareAndSwapUint32(addr, o, n) { 41 break 42 } 43 } 44 } 45 46 //go:nosplit 47 func XorUint32(addr *uint32, val uint32) { 48 for { 49 o := atomic.LoadUint32(addr) 50 n := o ^ val 51 if atomic.CompareAndSwapUint32(addr, o, n) { 52 break 53 } 54 } 55 } 56 57 //go:nosplit 58 func CompareAndSwapUint32(addr *uint32, old, new uint32) (prev uint32) { 59 for { 60 prev = atomic.LoadUint32(addr) 61 if prev != old { 62 return 63 } 64 if atomic.CompareAndSwapUint32(addr, old, new) { 65 return 66 } 67 } 68 } 69 70 //go:nosplit 71 func AndUint64(addr *uint64, val uint64) { 72 for { 73 o := atomic.LoadUint64(addr) 74 n := o & val 75 if atomic.CompareAndSwapUint64(addr, o, n) { 76 break 77 } 78 } 79 } 80 81 //go:nosplit 82 func OrUint64(addr *uint64, val uint64) { 83 for { 84 o := atomic.LoadUint64(addr) 85 n := o | val 86 if atomic.CompareAndSwapUint64(addr, o, n) { 87 break 88 } 89 } 90 } 91 92 //go:nosplit 93 func XorUint64(addr *uint64, val uint64) { 94 for { 95 o := atomic.LoadUint64(addr) 96 n := o ^ val 97 if atomic.CompareAndSwapUint64(addr, o, n) { 98 break 99 } 100 } 101 } 102 103 //go:nosplit 104 func CompareAndSwapUint64(addr *uint64, old, new uint64) (prev uint64) { 105 for { 106 prev = atomic.LoadUint64(addr) 107 if prev != old { 108 return 109 } 110 if atomic.CompareAndSwapUint64(addr, old, new) { 111 return 112 } 113 } 114 }