]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rand: convert r.used to atomic type
authorcui fliter <imcusg@gmail.com>
Tue, 6 Sep 2022 13:17:25 +0000 (13:17 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Sat, 1 Oct 2022 08:32:46 +0000 (08:32 +0000)
For #53821

Change-Id: I1b5c62288eca20ff50f6d8d979cf82df24d4545b
GitHub-Last-Rev: 266148570a6465b8a43e04b39b1ebf85d80fcc76
GitHub-Pull-Request: golang/go#54884
Reviewed-on: https://go-review.googlesource.com/c/go/+/428477
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/crypto/rand/rand_unix.go

index 746e90cc919a4e1fa1041bf2fa09ee98ebf1580b..40fce36314adfae939ceb6a9841c7814219b37f2 100644 (file)
@@ -34,7 +34,7 @@ func init() {
 type reader struct {
        f    io.Reader
        mu   sync.Mutex
-       used uint32 // Atomic: 0 - never used, 1 - used, but f == nil, 2 - used, and f != nil
+       used atomic.Uint32 // Atomic: 0 - never used, 1 - used, but f == nil, 2 - used, and f != nil
 }
 
 // altGetRandom if non-nil specifies an OS-specific function to get
@@ -47,7 +47,7 @@ func warnBlocked() {
 
 func (r *reader) Read(b []byte) (n int, err error) {
        boring.Unreachable()
-       if atomic.CompareAndSwapUint32(&r.used, 0, 1) {
+       if r.used.CompareAndSwap(0, 1) {
                // First use of randomness. Start timer to warn about
                // being blocked on entropy not being available.
                t := time.AfterFunc(time.Minute, warnBlocked)
@@ -56,16 +56,16 @@ func (r *reader) Read(b []byte) (n int, err error) {
        if altGetRandom != nil && altGetRandom(b) == nil {
                return len(b), nil
        }
-       if atomic.LoadUint32(&r.used) != 2 {
+       if r.used.Load() != 2 {
                r.mu.Lock()
-               if atomic.LoadUint32(&r.used) != 2 {
+               if r.used.Load() != 2 {
                        f, err := os.Open(urandomDevice)
                        if err != nil {
                                r.mu.Unlock()
                                return 0, err
                        }
                        r.f = hideAgainReader{f}
-                       atomic.StoreUint32(&r.used, 2)
+                       r.used.Store(2)
                }
                r.mu.Unlock()
        }