Source file src/vendor/golang.org/x/net/quic/atomic_bits.go
1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package quic 6 7 import "sync/atomic" 8 9 // atomicBits is an atomic uint32 that supports setting individual bits. 10 type atomicBits[T ~uint32] struct { 11 bits atomic.Uint32 12 } 13 14 // set sets the bits in mask to the corresponding bits in v. 15 // It returns the new value. 16 func (a *atomicBits[T]) set(v, mask T) T { 17 if v&^mask != 0 { 18 panic("BUG: bits in v are not in mask") 19 } 20 for { 21 o := a.bits.Load() 22 n := (o &^ uint32(mask)) | uint32(v) 23 if a.bits.CompareAndSwap(o, n) { 24 return T(n) 25 } 26 } 27 } 28 29 func (a *atomicBits[T]) load() T { 30 return T(a.bits.Load()) 31 } 32