diff --git a/libdevcore/Worker.h b/libdevcore/Worker.h index 2d07b5592..8f0baaf60 100644 --- a/libdevcore/Worker.h +++ b/libdevcore/Worker.h @@ -31,8 +31,18 @@ namespace dev class Worker { protected: - Worker(std::string const& _name): m_name(_name) {} + Worker(std::string const& _name = "anon"): m_name(_name) {} + + /// Move-constructor. + Worker(Worker&& _m) { std::swap(m_name, _m.m_name); } + + /// Move-assignment. + Worker& operator=(Worker&& _m) { std::swap(m_name, _m.m_name); return *this; } + virtual ~Worker() { stopWorking(); } + + void setName(std::string _n) { if (!isWorking()) m_name = _n; } + void startWorking(); void stopWorking(); bool isWorking() const { Guard l(x_work); return !!m_work; } diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 53222f343..6527d1eb1 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -63,7 +63,7 @@ Client::Client(p2p::Host* _extNet, std::string const& _dbPath, bool _forceClean, { m_host = _extNet->registerCapability(new EthereumHost(m_bc, m_tq, m_bq, _networkId)); -// setMiningThreads(); + setMiningThreads(); if (_dbPath.size()) Defaults::setDBPath(_dbPath); m_vc.setOk(); diff --git a/libethereum/Miner.cpp b/libethereum/Miner.cpp index 95789889f..5020a72c9 100644 --- a/libethereum/Miner.cpp +++ b/libethereum/Miner.cpp @@ -27,42 +27,12 @@ using namespace dev; using namespace dev::eth; Miner::Miner(MinerHost* _host, unsigned _id): - m_host(_host), - m_id(_id) + Worker("miner-" + toString(_id)), + m_host(_host) { } -void Miner::start() -{ - if (!m_host) - return; - - Guard l(x_work); - if (!m_work) - { - m_stop = false; - m_work.reset(new thread([&]() - { - setThreadName(("miner-" + toString(m_id)).c_str()); - m_miningStatus = Preparing; - while (!m_stop) - work(); - })); - } -} - -void Miner::stop() -{ - Guard l(x_work); - if (m_work) - { - m_stop = true; - m_work->join(); - m_work.reset(nullptr); - } -} - -void Miner::work() +void Miner::doWork() { // Do some mining. if ((m_pendingCount || m_host->force()) && m_miningStatus != Mined) diff --git a/libethereum/Miner.h b/libethereum/Miner.h index d8ae20cc1..bee4d70e4 100644 --- a/libethereum/Miner.h +++ b/libethereum/Miner.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include "State.h" @@ -74,38 +75,38 @@ public: * @threadsafe * @todo Signal Miner to restart once with condition variables. */ -class Miner +class Miner: Worker { public: /// Null constructor. - Miner(): m_host(nullptr), m_id(0) {} + Miner(): m_host(nullptr) {} /// Constructor. Miner(MinerHost* _host, unsigned _id = 0); /// Move-constructor. - Miner(Miner&& _m) { std::swap(m_host, _m.m_host); std::swap(m_id, _m.m_id); } + Miner(Miner&& _m): Worker((Worker&&)_m) { std::swap(m_host, _m.m_host); } /// Move-assignment. - Miner& operator=(Miner&& _m) { std::swap(m_host, _m.m_host); std::swap(m_id, _m.m_id); return *this; } + Miner& operator=(Miner&& _m) { Worker::operator=((Worker&&)_m); std::swap(m_host, _m.m_host); return *this; } /// Destructor. Stops miner. ~Miner() { stop(); } /// Setup its basics. - void setup(MinerHost* _host, unsigned _id = 0) { m_host = _host; m_id = _id; } + void setup(MinerHost* _host, unsigned _id = 0) { m_host = _host; setName("miner-" + toString(_id)); } /// Start mining. - void start(); + void start() { startWorking(); } /// Stop mining. - void stop(); + void stop() { stopWorking(); } /// Call to notify Miner of a state change. void noteStateChange() { m_miningStatus = Preparing; } /// @returns true iff the mining has been start()ed. It may still not be actually mining, depending on the host's turbo() & force(). - bool isRunning() { return !!m_work; } + bool isRunning() { return isWorking(); } /// @returns true if mining is complete. bool isComplete() const { return m_miningStatus == Mined; } @@ -121,14 +122,9 @@ public: private: /// Do some work on the mining. - void work(); + virtual void doWork(); MinerHost* m_host = nullptr; ///< Our host. - unsigned m_id = 0; ///< Our identity. - - std::mutex x_work; ///< Mutex protecting the creation of the work thread. - std::unique_ptr m_work; ///< The work thread. - bool m_stop = false; ///< Stop working? enum MiningStatus { Preparing, Mining, Mined, Stopping, Stopped }; MiningStatus m_miningStatus = Preparing;///< TODO: consider mutex/atomic variable. diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index 3b3be3fec..20c42a9b8 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -42,6 +42,7 @@ void mine(Client& c, int numBlocks) void connectClients(Client& c1, Client& c2) { + // TODO: Move to WebThree. eth::Client no longer handles networking. #if 0 short c1Port = 20000; short c2Port = 21000;