/
MsvThreadPool.h
205 lines (164 loc) · 9.89 KB
/
MsvThreadPool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**************************************************************************************************//**
* @addtogroup MTHREADING
* @{
******************************************************************************************************/
/**************************************************************************************************//**
* @file
* @brief MarsTech Thread Pool Implementation
* @details Contains implementation @ref MsvThreadPool of @ref IMsvThreadPool interface.
* @author Martin Svoboda
* @date 26.11.2018
* @copyright GNU General Public License (GPLv3).
******************************************************************************************************/
/*
This file is part of MarsTech Threading.
MarsTech Dependency Injection 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.
MarsTech Promise Like Syntax 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 Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MARSTECH_THREADPOOL_H
#define MARSTECH_THREADPOOL_H
#include "IMsvThreadPool.h"
#include "IMsvUniqueWorker.h"
MSV_DISABLE_ALL_WARNINGS
#include <mutex>
#include <vector>
#include <queue>
MSV_ENABLE_WARNINGS
//forward declaration of MarsTech Thread Pool Dependency Injection Factory
class MsvThreadPool_Factory;
/**************************************************************************************************//**
* @brief MarsTech Thread Pool Implementation.
* @details Implementation of thread pool for easy threading.
* @see IMsvThreadPool
******************************************************************************************************/
class MsvThreadPool:
public IMsvThreadPool
{
public:
/**************************************************************************************************//**
* @brief Constructor.
* @param spFactory Shared pointer to dependency injection factory.
* @see MsvWorker_Factory
******************************************************************************************************/
MsvThreadPool(std::shared_ptr<MsvThreadPool_Factory> spFactory = nullptr);
/**************************************************************************************************//**
* @brief Virtual destructor.
******************************************************************************************************/
virtual ~MsvThreadPool();
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::AddTask(std::shared_ptr<IMsvTask> spTask)
******************************************************************************************************/
virtual void AddTask(std::shared_ptr<IMsvTask> spTask) override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::AddTask(std::function<void()>& task)
******************************************************************************************************/
virtual MsvErrorCode AddTask(std::function<void()>& task) override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::AddTask(std::function<void(void*)>& task, void* pContext)
******************************************************************************************************/
virtual MsvErrorCode AddTask(std::function<void(void*)>& task, void* pContext) override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::IsRunning()
******************************************************************************************************/
bool IsRunning() const override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::StartThreadPool(uint16_t threadCount)
******************************************************************************************************/
virtual MsvErrorCode StartThreadPool(uint16_t threadCount = 4) override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::StopThreadPool()
******************************************************************************************************/
virtual MsvErrorCode StopThreadPool() override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::StopAndWaitForThreadPoolStop(int32_t timeout)
******************************************************************************************************/
virtual MsvErrorCode StopAndWaitForThreadPoolStop(int32_t timeout = 30000000) override;
/**************************************************************************************************//**
* @copydoc IMsvThreadPool::WaitForThreadPoolStop(int32_t timeout)
******************************************************************************************************/
virtual MsvErrorCode WaitForThreadPoolStop(int32_t timeout = 30000000) override;
protected:
/**************************************************************************************************//**
* @brief Task execution function.
* @details This is callback inserted to each worker (instance of @ref IMsvUniqueWorker). It calls
* execute function of tasks.
* @see GetTask
******************************************************************************************************/
void ExecuteTask();
/**************************************************************************************************//**
* @brief Get task to execute.
* @details Returns current task in the queue @ref m_taskQueue which will be executed. The task
* is removed from the queue.
* @returns std::shared_ptr<IMsvTask>
* @see m_taskQueue
******************************************************************************************************/
virtual std::shared_ptr<IMsvTask> GetTask();
protected:
/**************************************************************************************************//**
* @brief Flag if thread pool is running (true) or not (false).
* @see IsRunning
******************************************************************************************************/
bool m_isRunning;
/**************************************************************************************************//**
* @brief Thread pool mutex.
* @details Locks this object for thread safety access.
******************************************************************************************************/
mutable std::recursive_mutex m_lock;
/**************************************************************************************************//**
* @brief Dependency injection factory.
* @details Contains get method for all injected objects.
* @see MsvThreadPool_Factory
******************************************************************************************************/
std::shared_ptr<MsvThreadPool_Factory> m_spFactory;
/**************************************************************************************************//**
* @brief Shared condition variable.
* @details It is used for worker thread synchronization (wake ups thread if new task is inserted).
* @see AddTask
* @see m_spSharedConditionMutex
* @see m_spSharedConditionPredicate
******************************************************************************************************/
std::shared_ptr<std::condition_variable> m_spSharedCondition;
/**************************************************************************************************//**
* @brief Shared condition variable mutex.
* @details Shared mutex for shared condition variable @ref m_spSharedCondition.
* @see AddTask
* @see m_spSharedCondition
* @see m_spSharedConditionPredicate
******************************************************************************************************/
std::shared_ptr<std::mutex> m_spSharedConditionMutex;
/**************************************************************************************************//**
* @brief Shared condition variable predicate.
* @details Shared predicate for shared condition variable @ref m_spSharedCondition.
* @see AddTask
* @see m_spSharedCondition
* @see m_spSharedConditionMutex
******************************************************************************************************/
std::shared_ptr<uint64_t> m_spSharedConditionPredicate;
/**************************************************************************************************//**
* @brief Flag if thread pool stop is requested (true) or not (false).
* @details When this flag is set (true), worker threads will be stopped.
* @see StopThreadPool
******************************************************************************************************/
bool m_stopRequested;
/**************************************************************************************************//**
* @brief Task queue.
* @details Contains all inserted tasks for execution.
* @see AddTask
******************************************************************************************************/
std::queue<std::shared_ptr<IMsvTask>> m_taskQueue;
/**************************************************************************************************//**
* @brief Worker threads.
* @details Contains all worker threads.
******************************************************************************************************/
std::vector<std::shared_ptr<IMsvUniqueWorker>> m_workers;
};
#endif // !MARSTECH_ITHREADPOOL_H
/** @} */ //End of group MTHREADING.