]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/internal/atomic: add Bool
authorMichael Anthony Knyszek <mknyszek@google.com>
Mon, 14 Feb 2022 19:28:20 +0000 (19:28 +0000)
committerMichael Knyszek <mknyszek@google.com>
Thu, 31 Mar 2022 20:01:47 +0000 (20:01 +0000)
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>

src/runtime/internal/atomic/types.go

index 1a240d7c9180863a63ee51c68a84fef4ec6dd35f..d9cffbf88fa519cfddd9251617df5b56d9181047 100644 (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
 }