Commit 61806589 authored by Bas van Kervel's avatar Bas van Kervel

agent/miner Prevent the CpuAgent to be started multiple times

parent e2d7c1a5
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"sync/atomic"
) )
type CpuAgent struct { type CpuAgent struct {
...@@ -35,6 +36,8 @@ type CpuAgent struct { ...@@ -35,6 +36,8 @@ type CpuAgent struct {
index int index int
pow pow.PoW pow pow.PoW
isMining int32 // isMining indicates whether the agent is currently mining
} }
func NewCpuAgent(index int, pow pow.PoW) *CpuAgent { func NewCpuAgent(index int, pow pow.PoW) *CpuAgent {
...@@ -58,8 +61,12 @@ func (self *CpuAgent) Stop() { ...@@ -58,8 +61,12 @@ func (self *CpuAgent) Stop() {
} }
func (self *CpuAgent) Start() { func (self *CpuAgent) Start() {
self.mu.Lock()
defer self.mu.Unlock() defer self.mu.Unlock()
self.mu.Lock()
if atomic.LoadInt32(&self.isMining) == 1 {
return // agent already started
}
self.quit = make(chan struct{}) self.quit = make(chan struct{})
// creating current op ch makes sure we're not closing a nil ch // creating current op ch makes sure we're not closing a nil ch
...@@ -67,6 +74,8 @@ func (self *CpuAgent) Start() { ...@@ -67,6 +74,8 @@ func (self *CpuAgent) Start() {
self.workCh = make(chan *Work, 1) self.workCh = make(chan *Work, 1)
go self.update() go self.update()
atomic.StoreInt32(&self.isMining, 1)
} }
func (self *CpuAgent) update() { func (self *CpuAgent) update() {
...@@ -99,10 +108,11 @@ done: ...@@ -99,10 +108,11 @@ done:
case <-self.workCh: case <-self.workCh:
default: default:
close(self.workCh) close(self.workCh)
break done break done
} }
} }
atomic.StoreInt32(&self.isMining, 0)
} }
func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) { func (self *CpuAgent) mine(work *Work, stop <-chan struct{}) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment