Skip to content

Commit 6180658

Browse files
author
Bas van Kervel
committed
agent/miner Prevent the CpuAgent to be started multiple times
1 parent e2d7c1a commit 6180658

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

miner/agent.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/logger"
2424
"github.com/ethereum/go-ethereum/logger/glog"
2525
"github.com/ethereum/go-ethereum/pow"
26+
"sync/atomic"
2627
)
2728

2829
type CpuAgent struct {
@@ -35,6 +36,8 @@ type CpuAgent struct {
3536

3637
index int
3738
pow pow.PoW
39+
40+
isMining int32 // isMining indicates whether the agent is currently mining
3841
}
3942

4043
func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
@@ -58,15 +61,21 @@ func (self *CpuAgent) Stop() {
5861
}
5962

6063
func (self *CpuAgent) Start() {
61-
self.mu.Lock()
6264
defer self.mu.Unlock()
65+
self.mu.Lock()
66+
67+
if atomic.LoadInt32(&self.isMining) == 1 {
68+
return // agent already started
69+
}
6370

6471
self.quit = make(chan struct{})
6572
// creating current op ch makes sure we're not closing a nil ch
6673
// later on
6774
self.workCh = make(chan *Work, 1)
6875

6976
go self.update()
77+
78+
atomic.StoreInt32(&self.isMining, 1)
7079
}
7180

7281
func (self *CpuAgent) update() {
@@ -99,10 +108,11 @@ done:
99108
case <-self.workCh:
100109
default:
101110
close(self.workCh)
102-
103111
break done
104112
}
105113
}
114+
115+
atomic.StoreInt32(&self.isMining, 0)
106116
}
107117

108118
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {

0 commit comments

Comments
 (0)