Ethereum  PoC-8
The C++ Implementation of Ethereum
Worker.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <string>
25 #include <thread>
26 #include <atomic>
27 #include "Guards.h"
28 
29 namespace dev
30 {
31 
32 enum class IfRunning
33 {
34  Fail,
35  Join,
36  Detach
37 };
38 
39 enum class WorkerState
40 {
41  Starting,
42  Started,
43  Stopping,
44  Stopped,
45  Killing
46 };
47 
48 class Worker
49 {
50 protected:
51  Worker(std::string const& _name = "anon", unsigned _idleWaitMs = 30): m_name(_name), m_idleWaitMs(_idleWaitMs) {}
52 
54  Worker(Worker&& _m) { std::swap(m_name, _m.m_name); }
55 
58  {
59  assert(&_m != this);
60  std::swap(m_name, _m.m_name);
61  return *this;
62  }
63 
64  virtual ~Worker() { terminate(); }
65 
67  void setName(std::string _n) { if (!isWorking()) m_name = _n; }
68 
70  void startWorking();
71 
73  void stopWorking();
74 
76  bool isWorking() const { Guard l(x_work); return m_state == WorkerState::Started; }
77 
79  virtual void startedWorking() {}
80 
82  virtual void doWork() {}
83 
85  virtual void workLoop();
86  bool shouldStop() const { return m_state != WorkerState::Started; }
87 
89  virtual void doneWorking() {}
90 
92 // void join() const { Guard l(x_work); try { if (m_work) m_work->join(); } catch (...) {} }
93 
97  void terminate();
98 
99 private:
100 
101  std::string m_name;
102 
103  unsigned m_idleWaitMs = 0;
104 
105  mutable Mutex x_work;
106  std::unique_ptr<std::thread> m_work;
107  mutable std::condition_variable m_state_notifier; //< Notification when m_state changes.
108  std::atomic<WorkerState> m_state = {WorkerState::Starting};
109 };
110 
111 }
dev::WorkerState::Starting
@ Starting
dev::Worker::setName
void setName(std::string _n)
Allows changing worker name if work is stopped.
Definition: Worker.h:67
dev::Worker::stopWorking
void stopWorking()
Stop worker thread; causes call to stopWorking().
Definition: Worker.cpp:100
dev::Worker::operator=
Worker & operator=(Worker &&_m)
Move-assignment.
Definition: Worker.h:57
dev::Guard
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
std::swap
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:282
dev::Worker::shouldStop
bool shouldStop() const
Definition: Worker.h:86
dev::IfRunning
IfRunning
Definition: Worker.h:33
dev::Worker::~Worker
virtual ~Worker()
Definition: Worker.h:64
dev::Worker::startedWorking
virtual void startedWorking()
Called after thread is started from startWorking().
Definition: Worker.h:79
dev::Worker::workLoop
virtual void workLoop()
Overrides doWork(); should call shouldStop() often and exit when true.
Definition: Worker.cpp:134
dev::IfRunning::Detach
@ Detach
dev::Worker::doWork
virtual void doWork()
Called continuously following sleep for m_idleWaitMs.
Definition: Worker.h:82
dev::IfRunning::Join
@ Join
dev::WorkerState::Started
@ Started
dev::Worker::Worker
Worker(Worker &&_m)
Move-constructor.
Definition: Worker.h:54
dev::IfRunning::Fail
@ Fail
dev::Mutex
std::mutex Mutex
Definition: Guards.h:37
dev::WorkerState::Stopping
@ Stopping
dev::Worker::doneWorking
virtual void doneWorking()
Called when is to be stopped, just prior to thread being joined.
Definition: Worker.h:89
dev::Worker::startWorking
void startWorking()
Starts worker thread; causes startedWorking() to be called.
Definition: Worker.cpp:30
dev::Worker::Worker
Worker(std::string const &_name="anon", unsigned _idleWaitMs=30)
Definition: Worker.h:51
dev::WorkerState::Killing
@ Killing
dev::Worker::isWorking
bool isWorking() const
Returns if worker thread is present.
Definition: Worker.h:76
dev::WorkerState::Stopped
@ Stopped
dev
Definition: Address.cpp:21
dev::Worker::terminate
void terminate()
Blocks caller into worker thread has finished.
Definition: Worker.cpp:116
Guards.h
dev::WorkerState
WorkerState
Definition: Worker.h:40
dev::Worker
Definition: Worker.h:49