File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ import (
2020 "errors"
2121 "math/big"
2222 "sync"
23+ "sync/atomic"
2324 "time"
2425
2526 "github.com/ethereum/ethash"
@@ -45,6 +46,8 @@ type RemoteAgent struct {
4546
4647 hashrateMu sync.RWMutex
4748 hashrate map [common.Hash ]hashrate
49+
50+ running int32 // running indicates whether the agent is active. Call atomically
4851}
4952
5053func NewRemoteAgent () * RemoteAgent {
@@ -70,18 +73,22 @@ func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) {
7073}
7174
7275func (a * RemoteAgent ) Start () {
76+ if ! atomic .CompareAndSwapInt32 (& a .running , 0 , 1 ) {
77+ return
78+ }
79+
7380 a .quit = make (chan struct {})
7481 a .workCh = make (chan * Work , 1 )
7582 go a .maintainLoop ()
7683}
7784
7885func (a * RemoteAgent ) Stop () {
79- if a .quit != nil {
80- close (a .quit )
81- }
82- if a .workCh != nil {
83- close (a .workCh )
86+ if ! atomic .CompareAndSwapInt32 (& a .running , 1 , 0 ) {
87+ return
8488 }
89+
90+ close (a .quit )
91+ close (a .workCh )
8592}
8693
8794// GetHashRate returns the accumulated hashrate of all identifier combined
You can’t perform that action at this time.
0 commit comments