1
0
mirror of https://github.com/golang/go synced 2024-09-29 14:34:30 -06:00

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 <mpratt@google.com>
Reviewed-by: Austin Clements <austin@google.com>
Trust: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Michael Anthony Knyszek 2022-02-14 19:28:20 +00:00 committed by Michael Knyszek
parent 12acf9b0f0
commit f1b5c048d7

View File

@ -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
}