|
@ -36,6 +36,15 @@ enum class IfRunning |
|
|
Detach |
|
|
Detach |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
enum class WorkerState |
|
|
|
|
|
{ |
|
|
|
|
|
Starting, |
|
|
|
|
|
Started, |
|
|
|
|
|
Stopping, |
|
|
|
|
|
Stopped, |
|
|
|
|
|
Killing |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
class Worker |
|
|
class Worker |
|
|
{ |
|
|
{ |
|
|
protected: |
|
|
protected: |
|
@ -47,19 +56,19 @@ protected: |
|
|
/// Move-assignment.
|
|
|
/// Move-assignment.
|
|
|
Worker& operator=(Worker&& _m) { std::swap(m_name, _m.m_name); return *this; } |
|
|
Worker& operator=(Worker&& _m) { std::swap(m_name, _m.m_name); return *this; } |
|
|
|
|
|
|
|
|
virtual ~Worker() { stopWorking(); } |
|
|
virtual ~Worker() { terminate(); } |
|
|
|
|
|
|
|
|
/// Allows changing worker name if work is stopped.
|
|
|
/// Allows changing worker name if work is stopped.
|
|
|
void setName(std::string _n) { if (!isWorking()) m_name = _n; } |
|
|
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(IfRunning _ir = IfRunning::Fail); |
|
|
void startWorking(); |
|
|
|
|
|
|
|
|
/// Stop worker thread; causes call to stopWorking().
|
|
|
/// Stop worker thread; causes call to stopWorking().
|
|
|
void stopWorking(); |
|
|
void stopWorking(); |
|
|
|
|
|
|
|
|
/// Returns if worker thread is present.
|
|
|
/// Returns if worker thread is present.
|
|
|
bool isWorking() const { Guard l(x_work); return !!m_work && m_work->joinable(); } |
|
|
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() {} |
|
@ -69,22 +78,25 @@ protected: |
|
|
|
|
|
|
|
|
/// Overrides doWork(); should call shouldStop() often and exit when true.
|
|
|
/// Overrides doWork(); should call shouldStop() often and exit when true.
|
|
|
virtual void workLoop(); |
|
|
virtual void workLoop(); |
|
|
bool shouldStop() const { return m_stop; } |
|
|
bool shouldStop() const { return m_state != WorkerState::Started; } |
|
|
|
|
|
|
|
|
/// 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.
|
|
|
/// Blocks caller into worker thread has finished.
|
|
|
void join() const { Guard l(x_work); try { if (m_work) m_work->join(); } catch (...) {} } |
|
|
// 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; |
|
|
|
|
|
|
|
|
mutable Mutex x_work; ///< Lock for the network existance.
|
|
|
mutable Mutex x_work; ///< Lock for the network existance.
|
|
|
std::unique_ptr<std::thread> m_work; ///< The network thread.
|
|
|
std::unique_ptr<std::thread> m_work; ///< The network thread.
|
|
|
std::atomic<bool> m_stop = {false}; |
|
|
std::atomic<WorkerState> m_state = {WorkerState::Starting}; |
|
|
std::atomic<bool> m_stopped = {false}; |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|