Browse Source

Merge pull request #67 from ethereum-mining/worker

Refactor and cleanup Worker
cl-refactor
Paweł Bylica 7 years ago
committed by GitHub
parent
commit
7f9778c602
  1. 2
      libdevcore/CMakeLists.txt
  2. 29
      libdevcore/Guards.cpp
  3. 3
      libdevcore/Guards.h
  4. 3
      libdevcore/Worker.cpp
  5. 33
      libdevcore/Worker.h

2
libdevcore/CMakeLists.txt

@ -1,5 +1,5 @@
file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h") file(GLOB HEADERS "*.h")
file(GLOB SOURCES "*.cpp")
find_package(Threads) find_package(Threads)

29
libdevcore/Guards.cpp

@ -1,29 +0,0 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Guards.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "Guards.h"
using namespace std;
using namespace dev;
namespace dev
{
}

3
libdevcore/Guards.h

@ -29,11 +29,8 @@ namespace dev
{ {
using Mutex = std::mutex; using Mutex = std::mutex;
using RecursiveMutex = std::recursive_mutex;
using Guard = std::lock_guard<std::mutex>; using Guard = std::lock_guard<std::mutex>;
using UniqueGuard = std::unique_lock<std::mutex>; using UniqueGuard = std::unique_lock<std::mutex>;
using RecursiveGuard = std::lock_guard<std::recursive_mutex>;
template <class GuardType, class MutexType> template <class GuardType, class MutexType>
struct GenericGuardBool: GuardType struct GenericGuardBool: GuardType

3
libdevcore/Worker.cpp

@ -92,9 +92,8 @@ void Worker::stopWorking()
} }
} }
void Worker::terminate() Worker::~Worker()
{ {
// cnote << "stopWorking for thread" << m_name;
DEV_GUARDED(x_work) DEV_GUARDED(x_work)
if (m_work) if (m_work)
{ {

33
libdevcore/Worker.h

@ -30,13 +30,6 @@
namespace dev namespace dev
{ {
enum class IfRunning
{
Fail,
Join,
Detach
};
enum class WorkerState enum class WorkerState
{ {
Starting, Starting,
@ -51,21 +44,10 @@ class Worker
protected: protected:
Worker(std::string const& _name = "anon", unsigned _idleWaitMs = 30): m_name(_name), m_idleWaitMs(_idleWaitMs) {} Worker(std::string const& _name = "anon", unsigned _idleWaitMs = 30): m_name(_name), m_idleWaitMs(_idleWaitMs) {}
/// Move-constructor. Worker(Worker const&) = delete;
Worker(Worker&& _m) { std::swap(m_name, _m.m_name); } Worker& operator=(Worker const&) = delete;
/// Move-assignment.
Worker& operator=(Worker&& _m)
{
assert(&_m != this);
std::swap(m_name, _m.m_name);
return *this;
}
virtual ~Worker() { terminate(); }
/// Allows changing worker name if work is stopped. virtual ~Worker();
void setName(std::string _n) { if (!isWorking()) m_name = _n; }
/// Starts worker thread; causes startedWorking() to be called. /// Starts worker thread; causes startedWorking() to be called.
void startWorking(); void startWorking();
@ -73,9 +55,6 @@ protected:
/// Stop worker thread; causes call to stopWorking(). /// Stop worker thread; causes call to stopWorking().
void stopWorking(); void stopWorking();
/// Returns if worker thread is present.
bool isWorking() const { Guard l(x_work); return m_state == WorkerState::Started; }
/// Called after thread is started from startWorking(). /// Called after thread is started from startWorking().
virtual void startedWorking() {} virtual void startedWorking() {}
@ -89,13 +68,7 @@ protected:
/// Called when is to be stopped, just prior to thread being joined. /// Called when is to be stopped, just prior to thread being joined.
virtual void doneWorking() {} virtual void doneWorking() {}
/// Blocks caller into worker thread has finished.
// void join() const { Guard l(x_work); try { if (m_work) m_work->join(); } catch (...) {} }
private: private:
/// Stop and never start again.
void terminate();
std::string m_name; std::string m_name;
unsigned m_idleWaitMs = 0; unsigned m_idleWaitMs = 0;

Loading…
Cancel
Save