Fabcoin Core  0.16.2
P2P Digital Currency
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 
94 private:
96  void terminate();
97 
98  std::string m_name;
99 
100  unsigned m_idleWaitMs = 0;
101 
102  mutable Mutex x_work;
103  std::unique_ptr<std::thread> m_work;
104  std::atomic<WorkerState> m_state = {WorkerState::Starting};
105 };
106 
107 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
virtual void startedWorking()
Called after thread is started from startWorking().
Definition: Worker.h:79
Worker & operator=(Worker &&_m)
Move-assignment.
Definition: Worker.h:57
assert(len-trim+(2 *lenIndices)<=WIDTH)
bool shouldStop() const
Definition: Worker.h:86
Mutex x_work
Lock for the network existance.
Definition: Worker.h:102
Worker(std::string const &_name="anon", unsigned _idleWaitMs=30)
Definition: Worker.h:51
WorkerState
Definition: Worker.h:39
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
bool isWorking() const
Returns if worker thread is present.
Definition: Worker.h:76
std::unique_ptr< std::thread > m_work
The network thread.
Definition: Worker.h:103
std::string m_name
Definition: Worker.h:98
void setName(std::string _n)
Allows changing worker name if work is stopped.
Definition: Worker.h:67
IfRunning
Definition: Worker.h:32
virtual ~Worker()
Definition: Worker.h:64
Worker(Worker &&_m)
Move-constructor.
Definition: Worker.h:54
std::mutex Mutex
Definition: Guards.h:37
virtual void doneWorking()
Called when is to be stopped, just prior to thread being joined.
Definition: Worker.h:89
virtual void doWork()
Called continuously following sleep for m_idleWaitMs.
Definition: Worker.h:82