Skip to content

Commit d14c07d

Browse files
authored
accounts: use atomic type (#27857)
1 parent 8574767 commit d14c07d

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

accounts/keystore/keystore_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -397,19 +397,19 @@ func TestImportRace(t *testing.T) {
397397
t.Fatalf("failed to export account: %v", acc)
398398
}
399399
_, ks2 := tmpKeyStore(t, true)
400-
var atom uint32
400+
var atom atomic.Uint32
401401
var wg sync.WaitGroup
402402
wg.Add(2)
403403
for i := 0; i < 2; i++ {
404404
go func() {
405405
defer wg.Done()
406406
if _, err := ks2.Import(json, "new", "new"); err != nil {
407-
atomic.AddUint32(&atom, 1)
407+
atom.Add(1)
408408
}
409409
}()
410410
}
411411
wg.Wait()
412-
if atom != 1 {
412+
if atom.Load() != 1 {
413413
t.Errorf("Import is racy")
414414
}
415415
}

accounts/usbwallet/hub.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ type Hub struct {
6363
stateLock sync.RWMutex // Protects the internals of the hub from racey access
6464

6565
// TODO(karalabe): remove if hotplug lands on Windows
66-
commsPend int // Number of operations blocking enumeration
67-
commsLock sync.Mutex // Lock protecting the pending counter and enumeration
68-
enumFails uint32 // Number of times enumeration has failed
66+
commsPend int // Number of operations blocking enumeration
67+
commsLock sync.Mutex // Lock protecting the pending counter and enumeration
68+
enumFails atomic.Uint32 // Number of times enumeration has failed
6969
}
7070

7171
// NewLedgerHub creates a new hardware wallet manager for Ledger devices.
@@ -151,7 +151,7 @@ func (hub *Hub) refreshWallets() {
151151
return
152152
}
153153
// If USB enumeration is continually failing, don't keep trying indefinitely
154-
if atomic.LoadUint32(&hub.enumFails) > 2 {
154+
if hub.enumFails.Load() > 2 {
155155
return
156156
}
157157
// Retrieve the current list of USB wallet devices
@@ -172,7 +172,7 @@ func (hub *Hub) refreshWallets() {
172172
}
173173
infos, err := usb.Enumerate(hub.vendorID, 0)
174174
if err != nil {
175-
failcount := atomic.AddUint32(&hub.enumFails, 1)
175+
failcount := hub.enumFails.Add(1)
176176
if runtime.GOOS == "linux" {
177177
// See rationale before the enumeration why this is needed and only on Linux.
178178
hub.commsLock.Unlock()
@@ -181,7 +181,7 @@ func (hub *Hub) refreshWallets() {
181181
"vendor", hub.vendorID, "failcount", failcount, "err", err)
182182
return
183183
}
184-
atomic.StoreUint32(&hub.enumFails, 0)
184+
hub.enumFails.Store(0)
185185

186186
for _, info := range infos {
187187
for _, id := range hub.productIDs {

0 commit comments

Comments
 (0)