-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgl_task_dispatcher.hpp
More file actions
490 lines (428 loc) · 11.9 KB
/
vgl_task_dispatcher.hpp
File metadata and controls
490 lines (428 loc) · 11.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#ifndef VGL_TASK_DISPATCHER_HPP
#define VGL_TASK_DISPATCHER_HPP
#include "vgl_fence_pool.hpp"
#include "vgl_task_queue.hpp"
#include "vgl_thread.hpp"
#include "vgl_vector.hpp"
#if defined(VGL_USE_LD)
#include <sstream>
#endif
namespace vgl
{
namespace detail
{
/// Task queue class.
class InternalTaskDispatcher
{
private:
/// Threads created for this task queue.
vector<Thread> m_threads;
/// Queue for tasks (any thread).
TaskQueue m_tasks_any;
/// Queue for tasks (main thread).
TaskQueue m_tasks_main;
/// Pool of free data structures for fences.
FencePool m_fence_pool;
/// Guard mutex.
Mutex m_mutex = Mutex(nullptr);
/// Main thread ID.
Thread::id_type m_main_thread_id = 0;
/// Target concurrency level.
unsigned m_concurrency = 0;
/// Number of threads active currently.
unsigned m_threads_active = 0;
/// Number of threads waiting for tasks to execute.
unsigned m_threads_waiting = 0;
#if defined(VGL_USE_LD)
/// Flag signifying the task queue is being destroyed.
///
/// The flag is disabled for optimized build, because the program should never exit cleanly.
bool m_quitting = false;
#endif
public:
/// Default constructor.
constexpr explicit InternalTaskDispatcher() = default;
/// Destructor.
~InternalTaskDispatcher()
{
#if defined(VGL_USE_LD)
#if defined(DEBUG)
if(!m_mutex.getMutexImpl())
{
if((m_main_thread_id != 0) ||
!m_tasks_any.empty() ||
!m_tasks_main.empty() ||
!m_threads.empty() ||
!m_fence_pool.empty())
{
VGL_THROW_RUNTIME_ERROR("task queue was never initialized but is not at initial state");
}
}
else
#endif
{
{
ScopedAcquire sa(m_mutex);
m_quitting = true;
m_tasks_any.uninitialize();
m_tasks_main.uninitialize();
}
// Threads must be joined before destroying anything else.
m_threads.clear();
}
#endif
}
/// Deleted copy constructor.
InternalTaskDispatcher(const InternalTaskDispatcher&) = delete;
/// Deleted assignment.
InternalTaskDispatcher& operator=(const InternalTaskDispatcher&) = delete;
private:
/// Acquire or reuse fence data (locked).
///
/// \return Fence data structure to use.
FenceData* acquireFenceDataSafe()
{
ScopedAcquire sa(m_mutex);
return m_fence_pool.acquire();
}
/// Immediately execute given task function.
///
/// Return an inactive fence containing the return value.
/// \param func Function to dispatch.
/// \param params Function parameters.
Fence immediateDispatch(TaskFunc func, void* params)
{
FenceData* ret = acquireFenceDataSafe();
ret->setActive(false);
ret->setReturnValue(func(params));
return Fence(ret);
}
/// Internally wait (create a fence) and dispatch.
///
/// \param queue Internal task queue.
/// \param func Function to dispatch.
/// \param params Function parameters.
FenceData* internalDispatch(TaskQueue& task_queue, TaskFunc func, void* params)
{
FenceData* ret = m_fence_pool.acquire();
ret->setActive(true);
ret->setReturnValue(nullptr);
task_queue.emplace(ret, func, params);
return ret;
}
/// Is the calling thread the main thread.
///
/// \return True if yes, false if no.
bool isMainThread() const
{
return (m_main_thread_id == Thread::get_current_thread_id());
}
/// Is this a spawned thread?
bool isSpawnedThread()
{
Thread::id_type current_thread_id = Thread::get_current_thread_id();
for(const auto& vv : m_threads)
{
if(vv.getId() == current_thread_id)
{
return true;
}
}
return false;
}
/// Spawn a new thread.
///
/// Thread that has not entered execution is considered waiting.
void spawnThread()
{
#if defined(VGL_USE_LD)
string threadName = "InternalTaskDispatcher(" + to_string(m_threads.size()) + ")";
m_threads.emplace_back(task_thread_func, this, threadName.c_str());
#else
m_threads.emplace_back(task_thread_func, this);
#endif
++m_threads_waiting;
}
/// Spawns a thread if it's necessary.
///
/// Must be out of waiting threads and below concurrency limit.
void spawnThreadIfBelowConcurrency()
{
if((m_threads_waiting < m_tasks_any.size()) &&
(m_threads.size() < m_concurrency))
{
spawnThread();
}
}
/// Thread function.
/// \return Thread return value.
Thread::return_type threadFunc()
{
ScopedAcquire sa(m_mutex);
--m_threads_waiting;
#if defined(VGL_USE_LD)
while(!m_quitting)
#else
for(;;)
#endif
{
if((m_threads_active < m_concurrency) && !m_tasks_any.empty())
{
++m_threads_active;
{
// Release lock for the duration of executing the task.
Task task = m_tasks_any.acquire();
sa.release();
task();
}
sa.acquire();
--m_threads_active;
}
else
{
++m_threads_waiting;
m_tasks_any.wait(sa);
--m_threads_waiting;
}
}
return 0;
}
public:
/// Initialize the task queue.
///
/// \param op Number of threads to initialize.
void initialize(unsigned op)
{
m_concurrency = op;
m_main_thread_id = Thread::get_current_thread_id();
m_tasks_any.initialize();
m_tasks_main.initialize();
m_mutex = Mutex();
}
/// Gets a main context task.
///
/// \return Main context task.
Task acquireMainTask()
{
ScopedAcquire sa(m_mutex);
#if defined(VGL_USE_LD)
while(!m_quitting)
#else
for(;;)
#endif
{
if(m_tasks_main.empty())
{
m_tasks_main.wait(sa);
}
else
{
return m_tasks_main.acquire();
}
}
return Task();
}
/// Mark fence data as inactive (from locked context) and signal threads waiting on it.
///
/// \param op Fence data.
void fenceSignal(FenceData& op)
{
{
ScopedAcquire sa(m_mutex);
op.setActive(false);
}
op.signal();
}
/// Wait on a fence internal state.
///
/// \param op Fence.
/// \return Stored return value from the fence.
void* fenceWait(Fence& op)
{
ScopedAcquire sa(m_mutex);
// Fence may have turned inactive before the wait point is reached.
if(op)
{
#if defined(VGL_USE_LD) && defined(DEBUG)
if(isMainThread())
{
VGL_THROW_RUNTIME_ERROR("cannot wait on main thread");
}
#endif
bool is_spawned = isSpawnedThread();
// If waiting would lock the last concurrent thread, spawn a new thread.
if(is_spawned)
{
if(m_threads_waiting <= 0)
{
spawnThread();
}
--m_threads_active;
}
m_tasks_any.signal();
op.wait(sa);
if(is_spawned)
{
++m_threads_active;
}
}
FenceData* data = op.releaseData();
m_fence_pool.emplace(data);
return data->getReturnValue();
}
/// Dispatch a task (any thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
void dispatch(TaskFunc func, void* params)
{
ScopedAcquire sa(m_mutex);
m_tasks_any.emplace(func, params);
spawnThreadIfBelowConcurrency();
}
/// Dispatch a task (main thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
void dispatchMain(TaskFunc func, void* params)
{
ScopedAcquire sa(m_mutex);
m_tasks_main.emplace(func, params);
}
/// Dispatch a task and wait for it to complete (any thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
/// \return Fence.
Fence wait(TaskFunc func, void* params)
{
// Prevent deadlock - main thread cannot wait.
if(isMainThread())
{
return immediateDispatch(func, params);
}
ScopedAcquire sa(m_mutex);
FenceData* data = internalDispatch(m_tasks_any, func, params);
spawnThreadIfBelowConcurrency();
return Fence(data);
}
/// Dispatch a task and wait for it to complete (main thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
/// \return Fence.
Fence waitMain(TaskFunc func, void* params)
{
// Prevent deadlock - main thread cannot wait.
if(isMainThread())
{
return immediateDispatch(func, params);
}
ScopedAcquire sa(m_mutex);
FenceData* data = internalDispatch(m_tasks_main, func, params);
return Fence(data);
}
private:
/// Task dispatcher thread.
///
/// \param op Pointer to task queue.
static Thread::return_type task_thread_func(void* op)
{
return static_cast<InternalTaskDispatcher*>(op)->threadFunc();
}
};
}
/// Task dispatcher interface class.
class TaskDispatcher
{
private:
/// Global queue for tasks.
static detail::InternalTaskDispatcher g_instance;
public:
/// Initialize task system.
///
/// \param op Concurrency level.
static void initialize(unsigned op)
{
g_instance.initialize(op);
}
/// Signal fence data.
///
/// \param op Fence data.
static void fence_data_signal(detail::FenceData& op)
{
g_instance.fenceSignal(op);
}
/// Wait on fence.
///
/// \param op Fence data.
static void* fence_wait(Fence& op)
{
return g_instance.fenceWait(op);
}
/// Get a main loop task.
///
/// This function blocks if main loop tasks are not available.
///
/// \return New main loop task.
static Task acquire_main()
{
return g_instance.acquireMainTask();
}
/// Dispatch task (any thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
static void dispatch(TaskFunc func, void* params)
{
g_instance.dispatch(func, params);
}
/// Dispatch task (main thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
static void dispatch_main(TaskFunc func, void* params)
{
g_instance.dispatchMain(func, params);
}
/// Wait on a task (any thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
/// \return Fence.
static Fence wait(TaskFunc func, void* params)
{
return g_instance.wait(func, params);
}
/// Wait on a task (main thread).
///
/// \param func Function to dispatch.
/// \param params Function parameters.
/// \return Fence.
static Fence wait_main(TaskFunc func, void* params)
{
return g_instance.waitMain(func, params);
}
};
namespace detail
{
/// Internal signal of fence data.
///
/// \param op Fence data.
inline void internal_fence_data_signal(FenceData& op)
{
TaskDispatcher::fence_data_signal(op);
}
/// Internal wait for fence data on the task dispatcher.
///
/// \param op Fence data.
inline void* internal_fence_wait(Fence& op)
{
return TaskDispatcher::fence_wait(op);
}
}
}
#if !defined(VGL_USE_LD)
#include "vgl_task_dispatcher.cpp"
#endif
#endif