11// Copyright (c) 2014-2016 The btcsuite developers
2- // Copyright (c) 2015-2022 The Decred developers
2+ // Copyright (c) 2015-2023 The Decred developers
33// Use of this source code is governed by an ISC
44// license that can be found in the LICENSE file.
55
5555// speedStats houses tracking information used to monitor the hashing speed of
5656// the CPU miner.
5757type speedStats struct {
58- totalHashes uint64 // atomic
59- elapsedMicros uint64 // atomic
58+ totalHashes atomic. Uint64
59+ elapsedMicros atomic. Uint64
6060}
6161
6262// Config is a descriptor containing the CPU miner configuration.
@@ -112,7 +112,7 @@ type Config struct {
112112// workers which means it will be idle. The number of worker goroutines for the
113113// normal mining mode can be set via the SetNumWorkers method.
114114type CPUMiner struct {
115- numWorkers uint32 // update atomically
115+ numWorkers atomic. Uint32
116116
117117 sync.Mutex
118118 g * mining.BgBlkTmplGenerator
165165 hashesPerSec = 0
166166 m .Lock ()
167167 for _ , stats := range m .speedStats {
168- totalHashes := atomic . SwapUint64 ( & stats .totalHashes , 0 )
169- elapsedMicros := atomic . SwapUint64 ( & stats .elapsedMicros , 0 )
168+ totalHashes := stats .totalHashes . Swap ( 0 )
169+ elapsedMicros := stats .elapsedMicros . Swap ( 0 )
170170 elapsedSecs := (elapsedMicros / 1000000 )
171171 if totalHashes == 0 || elapsedSecs == 0 {
172172 continue
@@ -285,9 +285,9 @@ func (m *CPUMiner) solveBlock(ctx context.Context, header *wire.BlockHeader, sta
285285 hashesCompleted := uint64 (0 )
286286 start := time .Now ()
287287 updateSpeedStats := func () {
288- atomic . AddUint64 ( & stats .totalHashes , hashesCompleted )
288+ stats .totalHashes . Add ( hashesCompleted )
289289 elapsedMicros := time .Since (start ).Microseconds ()
290- atomic . AddUint64 ( & stats .elapsedMicros , uint64 (elapsedMicros ))
290+ stats .elapsedMicros . Add ( uint64 (elapsedMicros ))
291291
292292 hashesCompleted = 0
293293 start = time .Now ()
552552 // Update the number of running workers.
553553 case <- m .updateNumWorkers :
554554 numRunning := uint32 (len (runningWorkers ))
555- numWorkers := atomic . LoadUint32 ( & m .numWorkers )
555+ numWorkers := m .numWorkers . Load ( )
556556
557557 // No change.
558558 if numWorkers == numRunning {
@@ -678,7 +678,7 @@ func (m *CPUMiner) SetNumWorkers(numWorkers int32) {
678678 } else if targetNumWorkers > MaxNumWorkers {
679679 targetNumWorkers = MaxNumWorkers
680680 }
681- atomic . StoreUint32 ( & m .numWorkers , targetNumWorkers )
681+ m .numWorkers . Store ( targetNumWorkers )
682682
683683 // Set the normal mining state accordingly.
684684 if targetNumWorkers != 0 {
@@ -699,7 +699,7 @@ func (m *CPUMiner) SetNumWorkers(numWorkers int32) {
699699//
700700// This function is safe for concurrent access.
701701func (m * CPUMiner ) NumWorkers () int32 {
702- return int32 (atomic . LoadUint32 ( & m .numWorkers ))
702+ return int32 (m .numWorkers . Load ( ))
703703}
704704
705705// GenerateNBlocks generates the requested number of blocks in the discrete
@@ -826,14 +826,15 @@ out:
826826//
827827// See the documentation for CPUMiner type for more details.
828828func New (cfg * Config ) * CPUMiner {
829- return & CPUMiner {
829+ miner := & CPUMiner {
830830 g : cfg .BgBlkTmplGenerator ,
831831 cfg : cfg ,
832- numWorkers : defaultNumWorkers ,
833832 updateNumWorkers : make (chan struct {}),
834833 queryHashesPerSec : make (chan float64 ),
835834 speedStats : make (map [uint64 ]* speedStats ),
836835 minedOnParents : make (map [chainhash.Hash ]uint8 ),
837836 quit : make (chan struct {}),
838837 }
838+ miner .numWorkers .Store (defaultNumWorkers )
839+ return miner
839840}
0 commit comments