Repository navigation
Expand file tree
/
Copy pathThreadCommunication.h
More file actions
247 lines (219 loc) · 4.75 KB
/
Copy pathThreadCommunication.h
File metadata and controls
247 lines (219 loc) · 4.75 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#pragma once
#include <functional>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#ifdef _WINDOWS
void SetThreadName(const char* threadName);
const char *CleanName(const char *name);
#endif
template<class T>
class Signal
{
public:
Signal() : signaled(false)
{
}
Signal(const Signal &other) = default;
void Set(T t)
{
std::lock_guard<std::mutex> guard(mut);
signaled = true;
data = t;
cond.notify_one();
}
T Wait()
{
std::unique_lock<std::mutex> lock(mut);
cond.wait(lock, [&] {return signaled; });
signaled = false;
return data;
}
private:
std::condition_variable cond;
std::mutex mut;
bool signaled;
T data;
};
class ThreadManager;
class ThreadInterface
{
public:
ThreadInterface();
virtual ~ThreadInterface();
virtual void Run(Signal<bool> *init_signal, Signal<bool> *run_signal) = 0;
virtual void ExitThread() = 0;
virtual void WaitThread() = 0;
protected:
friend class ThreadManager;
};
template<class T>
using AsyncFunction = std::function<void(T *)>;
template<class T>
struct MessageQueue
{
enum Reason { ReasonNone, ReasonData, ReasonExit };
MessageQueue() : wakeup_reason(ReasonNone), should_exit(false)
{
}
void Add(AsyncFunction<T> &&func)
{
std::lock_guard<std::mutex> guard(mut);
function_queue.push(std::move(func));
wakeup_reason = ReasonData;
cond.notify_one();
}
void Exit()
{
std::lock_guard<std::mutex> guard(mut);
should_exit = true;
wakeup_reason = ReasonExit;
cond.notify_one();
}
Reason Wait(AsyncFunction<T> &func)
{
std::unique_lock<std::mutex> lock(mut);
if (should_exit)
return ReasonExit;
if (!function_queue.empty())
{
func.swap(function_queue.front());
function_queue.pop();
return ReasonData;
}
// buddifix 03.09.2018
do
{
cond.wait(lock);
} while (wakeup_reason == ReasonNone);
if (should_exit)
return ReasonExit;
if (!function_queue.empty())
{
func.swap(function_queue.front());
function_queue.pop();
return ReasonData;
}
return ReasonNone;
}
size_t Size()
{
std::unique_lock<std::mutex> lock(mut);
return function_queue.size();
}
Reason wakeup_reason;
bool should_exit;
std::mutex mut;
std::condition_variable cond;
std::queue<AsyncFunction<T>> function_queue;
};
template<class T, class QueueType = MessageQueue<T>>
class BaseThread : public ThreadInterface
{
public:
typedef QueueType Queue;
template<typename F, typename ...Args>
static void RPC(F f, Args&& ...args)
{
queue.Add(std::bind(f, std::placeholders::_1, std::forward<Args>(args)...));
}
static size_t QueueSize()
{
return queue.Size();
}
virtual bool OnInit()
{
return true;
}
virtual void OnStart()
{
}
virtual void OnExit()
{
}
virtual void OnOtherEvent(typename Queue::Reason)
{
}
std::thread::id GetThreadID()
{
return thread.get_id();
}
static std::thread::id GetFirstThreadID()
{
return first_thread_id;
}
private:
void Run(Signal<bool> *init_signal, Signal<bool> *run_signal) override
{
thread = std::thread(&BaseThread<T, QueueType>::ThreadFunc, this, init_signal, run_signal);
}
void ExitThread() override
{
queue.Exit();
}
void WaitThread() override
{
thread.join();
}
void ThreadFunc(Signal<bool> *init_signal, Signal<bool> *run_signal)
{
{
#ifdef _WINDOWS
#ifdef _DEBUG
SetThreadName(CleanName(typeid(T).name()));
#endif
#endif
std::lock_guard<std::mutex> lock(thread_id_mutex);
if (first_thread_id != std::thread::id())
first_thread_id = GetThreadID();
}
init_signal->Set(OnInit());
bool run = run_signal->Wait();
init_signal->Set(true);
if (!run)
return;
OnStart();
for (;;)
{
AsyncFunction<T> func;
auto reason = queue.Wait(func);
if (reason == queue.ReasonData)
{
func(static_cast<T *>(this));
}
else if (reason == queue.ReasonExit)
{
break;
}
else
{
OnOtherEvent(reason);
}
}
OnExit();
}
static Queue queue;
static std::thread::id first_thread_id;
static std::mutex thread_id_mutex;
std::thread thread;
};
class ThreadManager
{
public:
static bool Run();
static void Exit();
static void Wait();
private:
friend class ThreadInterface;
static void AddThread(ThreadInterface *thread);
static void RemoveThread(ThreadInterface *thread);
static std::vector<ThreadInterface *> threads;
};
template<class T, class QueueType>
QueueType BaseThread<T, QueueType>::queue;
template<class T, class QueueType>
std::thread::id BaseThread<T, QueueType>::first_thread_id;
template<class T, class QueueType>
std::mutex BaseThread<T, QueueType>::thread_id_mutex;