From: Michael Anthony Knyszek Date: Mon, 14 Feb 2022 19:28:20 +0000 (+0000) Subject: runtime/internal/atomic: add Bool X-Git-Tag: go1.19beta1~833 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f1b5c048d7b7589a5b3ae423114baf27b3456021;p=gostls13.git runtime/internal/atomic: add Bool This change adds the Bool type, a convenient wrapper around Uint8 for atomic bool values. Change-Id: I86127d6f213b730d6999db5718ca1a5af0c5b538 Reviewed-on: https://go-review.googlesource.com/c/go/+/393395 Reviewed-by: Michael Pratt Reviewed-by: Austin Clements Trust: Michael Knyszek --- diff --git a/src/runtime/internal/atomic/types.go b/src/runtime/internal/atomic/types.go index 1a240d7c91..d9cffbf88f 100644 --- a/src/runtime/internal/atomic/types.go +++ b/src/runtime/internal/atomic/types.go @@ -124,6 +124,28 @@ func (u *Uint8) Or(value uint8) { Or8(&u.value, value) } +// Bool is an atomically accessed bool value. +// +// A Bool must not be copied. +type Bool struct { + // Inherits noCopy from Uint8. + u Uint8 +} + +// Load accesses and returns the value atomically. +func (b *Bool) Load() bool { + return b.u.Load() != 0 +} + +// Store updates the value atomically. +func (b *Bool) Store(value bool) { + s := uint8(0) + if value { + s = 1 + } + b.u.Store(s) +} + // Uint32 is an atomically accessed uint32 value. // // A Uint32 must not be copied. @@ -326,6 +348,7 @@ func (u *Uintptr) Add(delta uintptr) uintptr { // // A Float64 must not be copied. type Float64 struct { + // Inherits noCopy from Uint64. u Uint64 }