github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/atomicbitops/bool.go (about) 1 // Copyright 2022 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 package atomicbitops 16 17 import "sync/atomic" 18 19 // Bool is an atomic Boolean. 20 // 21 // It is implemented by a Uint32, with value 0 indicating false, and 1 22 // indicating true. 23 // 24 // +stateify savable 25 type Bool struct { 26 Uint32 27 } 28 29 // FromBool returns an Bool initialized to value val. 30 // 31 //go:nosplit 32 func FromBool(val bool) Bool { 33 var u uint32 34 if val { 35 u = 1 36 } 37 return Bool{ 38 Uint32{ 39 value: u, 40 }, 41 } 42 } 43 44 // Load is analogous to atomic.LoadBool, if such a thing existed. 45 // 46 //go:nosplit 47 func (b *Bool) Load() bool { 48 return atomic.LoadUint32(&b.value) == 1 49 } 50 51 // Store is analogous to atomic.StoreBool, if such a thing existed. 52 // 53 //go:nosplit 54 func (b *Bool) Store(val bool) { 55 var u uint32 56 if val { 57 u = 1 58 } 59 atomic.StoreUint32(&b.value, u) 60 } 61 62 // Swap is analogous to atomic.SwapBool, if such a thing existed. 63 // 64 //go:nosplit 65 func (b *Bool) Swap(val bool) bool { 66 var u uint32 67 if val { 68 u = 1 69 } 70 return atomic.SwapUint32(&b.value, u) == 1 71 }