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 (
20
20
"errors"
21
21
"math/big"
22
22
"sync"
23
+ "sync/atomic"
23
24
"time"
24
25
25
26
"github.com/ethereum/ethash"
@@ -45,6 +46,8 @@ type RemoteAgent struct {
45
46
46
47
hashrateMu sync.RWMutex
47
48
hashrate map [common.Hash ]hashrate
49
+
50
+ running int32 // running indicates whether the agent is active. Call atomically
48
51
}
49
52
50
53
func NewRemoteAgent () * RemoteAgent {
@@ -70,18 +73,22 @@ func (a *RemoteAgent) SetReturnCh(returnCh chan<- *Result) {
70
73
}
71
74
72
75
func (a * RemoteAgent ) Start () {
76
+ if ! atomic .CompareAndSwapInt32 (& a .running , 0 , 1 ) {
77
+ return
78
+ }
79
+
73
80
a .quit = make (chan struct {})
74
81
a .workCh = make (chan * Work , 1 )
75
82
go a .maintainLoop ()
76
83
}
77
84
78
85
func (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
84
88
}
89
+
90
+ close (a .quit )
91
+ close (a .workCh )
85
92
}
86
93
87
94
// GetHashRate returns the accumulated hashrate of all identifier combined
You can’t perform that action at this time.
0 commit comments