Browse Source

Moved miner over to new Worker class.

cl-refactor
Gav Wood 11 years ago
parent
commit
4eb549cd80
  1. 12
      libdevcore/Worker.h
  2. 2
      libethereum/Client.cpp
  3. 36
      libethereum/Miner.cpp
  4. 24
      libethereum/Miner.h
  5. 1
      test/TestHelper.cpp

12
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; }

2
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();

36
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)

24
libethereum/Miner.h

@ -26,6 +26,7 @@
#include <list>
#include <atomic>
#include <libdevcore/Common.h>
#include <libdevcore/Worker.h>
#include <libethcore/CommonEth.h>
#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<std::thread> 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.

1
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;

Loading…
Cancel
Save