-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtask_runner.hpp
774 lines (706 loc) · 29.3 KB
/
task_runner.hpp
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#pragma once
#include "fly/fly.hpp"
#include "fly/task/types.hpp"
#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <type_traits>
/**
* Helper macro to create a TaskLocation from the current location.
*/
#define FROM_HERE \
fly::task::TaskLocation({__FILE__, __FUNCTION__, static_cast<std::uint32_t>(__LINE__)})
namespace fly::task {
class TaskManager;
/**
* Base class for controlling the execution of tasks. Concrete task runners control the ordering and
* execution of tasks.
*
* Tasks may generally be any callable type (lambda, std::function, etc.). Specific posting methods
* may place restrictions on the callable type, on either the return type of the invocation or the
* arguments the task accepts.
*
* Tasks whose result is a non-void type may pass their result to a reply task. For example:
*
* auto task = []() -> int
* {
* // Task body here.
* return 12389;
* };
*
* auto reply = [](int task_result)
* {
* assert(task_result == 12389);
* // Reply body here.
* };
*
* task_runner->post_task_with_reply(FROM_HERE, std::move(task), std::move(reply));
*
* Tasks whose result is void may indicate their completion to a reply task. For example:
*
* auto task = []()
* {
* // Task body here.
* };
*
* auto reply = []()
* {
* // Reply body here.
* };
*
* task_runner->post_task_with_reply(FROM_HERE, std::move(task), std::move(reply));
*
* Mismatching of task result types and reply parameter types is explicitly forbidden at compile
* time. Tasks that return a non-void result must be paired with a reply which is invocable with
* only that type. Tasks which return void must be paired with a reply that is invocable without
* arguments.
*
* Reply tasks are not executed immediately after a task is complete. Rather, they are posted for
* execution on the same task runner on which that task was posted.
*
* Once a task is posted, it may be attempted to be cancelled in a number of ways:
*
* 1. Use one of the posting methods which accepts a weak pointer to the owner of the task. When the
* task is ready to be executed, if the weak pointer cannot be promoted to a strong pointer, the
* task is dropped. The task must accept a single argument, the shared pointer obtained from the
* weak pointer. For example:
*
* auto task = [](std::shared_ptr<MyClass> self)
* {
* // Task body here.
* };
*
* std::weak_ptr<MyClass> weak_self = shared_from_this();
* task_runner->post_task(FROM_HERE, weak_self, std::move(task));
*
* Reply tasks may be cancelled in the same manner. The reply task must then accept the result of
* the task and the shared pointer obtained from the weak pointer. If the task was dropped due to
* being unable to promote the weak pointer, the reply is also dropped. If the task was executed,
* when the reply is ready to be executed, if the weak pointer cannot be promoted to a strong
* pointer, the reply is dropped. For example:
*
* auto task = [](std::shared_ptr<MyClass> self) -> int
* {
* // Task body here.
* return 12389;
* };
*
* auto reply = [](int task_result, std::shared_ptr<MyClass> self)
* {
* assert(task_result == 12389);
* // Reply body here.
* };
*
* std::weak_ptr<MyClass> weak_self = shared_from_this();
* task_runner->post_task_with_reply(FROM_HERE, weak_self, std::move(task), std::move(reply));
*
* 2. Deleting the task runner onto which the task was posted. This will only cancel the task if the
* task manager has not yet instructed the task runner to execute the task.
*
* @author Timothy Flynn ([email protected])
* @version August 12, 2018
*/
class TaskRunner : public std::enable_shared_from_this<TaskRunner>
{
friend class TaskManager;
public:
/**
* Destructor.
*/
virtual ~TaskRunner() = default;
/**
* Post a task for execution. The task may be any callable type.
*
* @tparam TaskType Callable type of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
template <typename TaskType>
bool post_task(TaskLocation &&location, TaskType &&task);
/**
* Post a task for execution with protection by the provided weak pointer. The task may be any
* callable type which accepts a single argument, a locked shared pointer obtained from the weak
* pointer. When the task is ready to be executed, if the weak pointer fails to be locked, the
* task is dropped.
*
* @tparam TaskType Callable type of the task.
* @tparam OwnerType Type of the owner of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param weak_owner A weak pointer to the owner of the task.
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
template <typename OwnerType, typename TaskType>
bool post_task(TaskLocation &&location, std::weak_ptr<OwnerType> weak_owner, TaskType &&task);
/**
* Post a task for execution. The task may be any callable type.
*
* When the task has been executed, the reply task is then posted for execution on this same
* task runner. The reply task may be any callable type that is invocable with the return type
* of the task (if non-void), or without any arguments (if void).
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return True if the task was posted for execution.
*/
template <typename TaskType, typename ReplyType>
bool post_task_with_reply(TaskLocation &&location, TaskType &&task, ReplyType &&reply);
/**
* Post a task for execution with protection by the provided weak pointer. The task may be any
* callable type which accepts a single argument, a locked shared pointer obtained from the weak
* pointer. When the task is ready to be executed, if the weak pointer fails to be locked, the
* task is dropped.
*
* When the task has been executed, the reply task is then posted for execution on this same
* task runner with protection by the same weak pointer. The reply task may be any callable type
* that is invocable with the return type of the task (if non-void) and a locked shared pointer
* obtained from the weak pointer, or with only the locked shared pointer. When the reply is
* ready to be executed, if the weak pointer fails to be locked, the reply is dropped.
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
* @tparam OwnerType Type of the owner of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param weak_owner A weak pointer to the owner of the task.
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return True if the task was posted for execution.
*/
template <typename OwnerType, typename TaskType, typename ReplyType>
bool post_task_with_reply(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
TaskType &&task,
ReplyType &&reply);
/**
* Schedule a task to be posted after a delay. The task may be any callable type.
*
* @tparam TaskType Callable type of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param delay Delay before posting the task.
* @param task The task to be executed.
*
* @return True if the task was posted for delayed execution.
*/
template <typename TaskType>
bool
post_task_with_delay(TaskLocation &&location, std::chrono::milliseconds delay, TaskType &&task);
/**
* Schedule a task to be posted after a delay with protection by the provided weak pointer. The
* task may be any callable type which accepts a single argument, a locked shared pointer
* obtained from the weak pointer. When the task is ready to be executed, if the weak pointer
* fails to be locked, the task is dropped.
*
* @tparam TaskType Callable type of the task.
* @tparam OwnerType Type of the owner of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param weak_owner A weak pointer to the owner of the task.
* @param delay Delay before posting the task.
* @param task The task to be executed.
*
* @return True if the task was posted for delayed execution.
*/
template <typename OwnerType, typename TaskType>
bool post_task_with_delay(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
std::chrono::milliseconds delay,
TaskType &&task);
/**
* Schedule a task to be posted after a delay. The task may be any callable type.
*
* When the task has been executed, the reply task is then posted for execution on this same
* task runner. The reply task may be any callable type that is invocable with the return type
* of the task (if non-void), or without any arguments (if void).
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param delay Delay before posting the task.
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return True if the task was posted for execution.
*/
template <typename TaskType, typename ReplyType>
bool post_task_with_delay_and_reply(
TaskLocation &&location,
std::chrono::milliseconds delay,
TaskType &&task,
ReplyType &&reply);
/**
* Schedule a task to be posted after a delay with protection by the provided weak pointer. The
* task may be any callable type which accepts a single argument, a locked shared pointer
* obtained from the weak pointer. When the task is ready to be executed, if the weak pointer
* fails to be locked, the task is dropped.
*
* When the task has been executed, the reply task is then posted for execution on this same
* task runner with protection by the same weak pointer. The reply task may be any callable type
* that is invocable with the return type of the task (if non-void) and a locked shared pointer
* obtained from the weak pointer, or with only the locked shared pointer. When the reply is
* ready to be executed, if the weak pointer fails to be locked, the reply is dropped.
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
* @tparam OwnerType Type of the owner of the task.
*
* @param location The location from which the task was posted (use FROM_HERE).
* @param weak_owner A weak pointer to the owner of the task.
* @param delay Delay before posting the task.
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return True if the task was posted for execution.
*/
template <typename OwnerType, typename TaskType, typename ReplyType>
bool post_task_with_delay_and_reply(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
std::chrono::milliseconds delay,
TaskType &&task,
ReplyType &&reply);
protected:
/**
* Private constructor. Task runners may only be created by the task manager.
*
* @param task_manager The task manager.
*/
TaskRunner(std::shared_ptr<TaskManager> task_manager) noexcept;
/**
* Post a task for execution in accordance with the concrete task runner's policy.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
virtual bool post_task_internal(TaskLocation &&location, Task &&task) = 0;
/**
* Completion notification triggered by the task manager that a task has finished execution.
*
* @param location The location from which the task was posted.
*/
virtual void task_complete(TaskLocation &&location) = 0;
/**
* Forward a task to the task manager to be executed as soon as a worker thread is available.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
bool post_task_to_task_manager(TaskLocation &&location, Task &&task);
/**
* Forward a task to the task manager to be scheduled for excution after a delay. The task will
* be stored on the task manager's timer thread. Once the given delay has expired, the task will
* be handed back to the task runner to govern when the task will be posted from there.
*
* @param location The location from which the task was posted.
* @param delay Delay before posting the task.
* @param task The task to be executed.
*
* @return True if the task was posted for delayed execution.
*/
bool post_task_to_task_manager_with_delay(
TaskLocation &&location,
std::chrono::milliseconds delay,
Task &&task);
private:
/**
* Container to wrap around a generic callable type to allow perfect forwarding into lambdas.
*/
template <typename TaskType>
struct TaskHolder
{
TaskType m_task;
};
/**
* Wrap a task in a generic lambda to be agnostic to the return type of the task.
*
* @tparam TaskType Callable type of the task.
*
* @param task The task to be executed.
*
* @return The wrapped task.
*/
template <typename TaskType>
Task wrap_task(TaskType &&task);
/**
* Wrap a task in a generic lambda to be agnostic to the return type of the task. When the task
* is ready to be executed, if the provided weak pointer fails to be locked, the task is
* dropped.
*
* @tparam TaskType Callable type of the task.
* @tparam OwnerType Type of the owner of the task.
*
* @param weak_owner A weak pointer to the owner of the task.
* @param task The task to be executed.
*
* @return The wrapped task.
*/
template <typename OwnerType, typename TaskType>
Task wrap_task(std::weak_ptr<OwnerType> weak_owner, TaskType &&task);
/**
* Wrap a task in a generic lambda to be agnostic to the return type of the task.
*
* When the task has been executed, the result of the task (if any) is bound to the provided
* reply task. The reply task is then posted for execution on this same task runner.
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
*
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return The wrapped task.
*/
template <typename TaskType, typename ReplyType>
Task wrap_task(TaskType &&task, ReplyType &&reply);
/**
* Wrap a task in a generic lambda to be agnostic to the return type of the task. When the task
* is ready to be executed, if the provided weak pointer fails to be locked, the task is
* dropped.
*
* When the task has been executed, the result of the task (if any) is bound to the provided
* reply task. The reply task is then posted for execution on this same task runner with
* protection by the same weak pointer.
*
* @tparam TaskType Callable type of the task.
* @tparam ReplyType Callable type of the reply.
* @tparam OwnerType Type of the owner of the task.
*
* @param weak_owner A weak pointer to the owner of the task.
* @param task The task to be executed.
* @param reply The reply to be executed with the result of the task.
*
* @return The wrapped task.
*/
template <typename OwnerType, typename TaskType, typename ReplyType>
Task wrap_task(std::weak_ptr<OwnerType> weak_owner, TaskType &&task, ReplyType &&reply);
/**
* Execute a task.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*/
void execute(TaskLocation &&location, Task &&task);
std::weak_ptr<TaskManager> m_weak_task_manager;
};
/**
* Task runner implementation for executing tasks in parallel. Tasks posted to this task runner may
* be executed in any order.
*
* @author Timothy Flynn ([email protected])
* @version August 12, 2018
*/
class ParallelTaskRunner : public TaskRunner
{
public:
/**
* Create a parallel task runner.
*
* @param task_manager The task manager this runner should interface with.
*
* @return The created task runner.
*/
static std::shared_ptr<ParallelTaskRunner> create(std::shared_ptr<TaskManager> task_manager);
protected:
explicit ParallelTaskRunner(std::shared_ptr<TaskManager> task_manager) noexcept;
/**
* Post a task for execution immediately.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
bool post_task_internal(TaskLocation &&location, Task &&task) override;
/**
* This implementation does nothing.
*
* @param location The location from which the task was posted.
*/
void task_complete(TaskLocation &&location) override;
};
/**
* Task runner implementation for executing tasks in sequence. Only one task posted to this task
* runner will execute at a time. Tasks are executed in a FIFO manner; once one task completes, the
* next task in line will be posted for execution.
*
* The caveat is with delayed tasks. If task A is posted with some delay, then task B is posted with
* no delay, task B will be posted for execution first. Task A will only be posted for execution
* once its delay has expired.
*
* @author Timothy Flynn ([email protected])
* @version August 12, 2018
*/
class SequencedTaskRunner : public TaskRunner
{
public:
/**
* Create a sequenced task runner.
*
* @param task_manager The task manager this runner should interface with.
*
* @return The created task runner.
*/
static std::shared_ptr<SequencedTaskRunner> create(std::shared_ptr<TaskManager> task_manager);
protected:
explicit SequencedTaskRunner(std::shared_ptr<TaskManager> task_manager) noexcept;
/**
* Post a task for execution within this sequence. If a task is not already running, the task is
* posted for execution immediately. Otherwise, the task is queued until the currently running
* task (and all tasks queued before it) have completed.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*
* @return True if the task was posted for execution.
*/
bool post_task_internal(TaskLocation &&location, Task &&task) override;
/**
* When a task is complete, post the next task in the pending queue.
*
* @param location The location from which the task was posted.
*/
void task_complete(TaskLocation &&location) override;
private:
/**
* Structure to hold a task until it is ready to be executed within its sequence.
*/
struct PendingTask
{
TaskLocation m_location;
Task m_task;
};
/**
* If no task has been posted for execution, post either the first task in the pending queue (if
* there is one) or the given task (if non-null). If a task is currently posted for execution,
* or if there was a task in the pending queue queue, add the given task to the queue.
*
* @param location The location from which the task was posted.
* @param task The task to be executed.
*
* @return True if the task was posted for execution or added to the pending queue.
*/
bool maybe_post_task(TaskLocation &&location, Task &&task);
std::mutex m_pending_tasks_mutex;
std::queue<PendingTask> m_pending_tasks;
bool m_has_running_task {false};
};
//==================================================================================================
template <typename TaskType>
bool TaskRunner::post_task(TaskLocation &&location, TaskType &&task)
{
return post_task_internal(std::move(location), wrap_task(std::forward<TaskType>(task)));
}
//==================================================================================================
template <typename OwnerType, typename TaskType>
bool TaskRunner::post_task(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
TaskType &&task)
{
return post_task_internal(
std::move(location),
wrap_task(std::move(weak_owner), std::forward<TaskType>(task)));
}
//==================================================================================================
template <typename TaskType, typename ReplyType>
bool TaskRunner::post_task_with_reply(TaskLocation &&location, TaskType &&task, ReplyType &&reply)
{
return post_task_internal(
std::move(location),
wrap_task(std::forward<TaskType>(task), std::forward<ReplyType>(reply)));
}
//==================================================================================================
template <typename OwnerType, typename TaskType, typename ReplyType>
bool TaskRunner::post_task_with_reply(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
TaskType &&task,
ReplyType &&reply)
{
return post_task_internal(
std::move(location),
wrap_task(
std::move(weak_owner),
std::forward<TaskType>(task),
std::forward<ReplyType>(reply)));
}
//==================================================================================================
template <typename TaskType>
bool TaskRunner::post_task_with_delay(
TaskLocation &&location,
std::chrono::milliseconds delay,
TaskType &&task)
{
return post_task_to_task_manager_with_delay(
std::move(location),
std::move(delay),
wrap_task(std::forward<TaskType>(task)));
}
//==================================================================================================
template <typename OwnerType, typename TaskType>
bool TaskRunner::post_task_with_delay(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
std::chrono::milliseconds delay,
TaskType &&task)
{
return post_task_to_task_manager_with_delay(
std::move(location),
std::move(delay),
wrap_task(std::move(weak_owner), std::forward<TaskType>(task)));
}
//==================================================================================================
template <typename TaskType, typename ReplyType>
bool TaskRunner::post_task_with_delay_and_reply(
TaskLocation &&location,
std::chrono::milliseconds delay,
TaskType &&task,
ReplyType &&reply)
{
return post_task_to_task_manager_with_delay(
std::move(location),
std::move(delay),
wrap_task(std::forward<TaskType>(task), std::forward<ReplyType>(reply)));
}
//==================================================================================================
template <typename OwnerType, typename TaskType, typename ReplyType>
bool TaskRunner::post_task_with_delay_and_reply(
TaskLocation &&location,
std::weak_ptr<OwnerType> weak_owner,
std::chrono::milliseconds delay,
TaskType &&task,
ReplyType &&reply)
{
return post_task_to_task_manager_with_delay(
std::move(location),
std::move(delay),
wrap_task(std::move(weak_owner), std::forward<TaskType>(task), std::move(reply)));
}
//==================================================================================================
template <typename TaskType>
Task TaskRunner::wrap_task(TaskType &&task)
{
static_assert(std::is_invocable_v<TaskType>, "Task must be invocable without any arguments");
TaskHolder<TaskType> holder {std::forward<TaskType>(task)};
return [holder = std::move(holder)](TaskRunner *, TaskLocation) mutable {
FLY_UNUSED(std::invoke(std::move(holder.m_task)));
};
}
//==================================================================================================
template <typename OwnerType, typename TaskType>
Task TaskRunner::wrap_task(std::weak_ptr<OwnerType> weak_owner, TaskType &&task)
{
using StrongOwnerType = std::shared_ptr<OwnerType>;
static_assert(
std::is_invocable_v<TaskType, StrongOwnerType>,
"Task must be invocable with only a strong pointer to its owner");
TaskHolder<TaskType> holder {std::forward<TaskType>(task)};
return [weak_owner = std::move(weak_owner),
holder = std::move(holder)](TaskRunner *, TaskLocation) mutable {
if (StrongOwnerType owner = weak_owner.lock(); owner)
{
FLY_UNUSED(std::invoke(std::move(holder.m_task), std::move(owner)));
}
};
}
//==================================================================================================
template <typename TaskType, typename ReplyType>
Task TaskRunner::wrap_task(TaskType &&task, ReplyType &&reply)
{
static_assert(std::is_invocable_v<TaskType>, "Task must be invocable without any arguments");
using ResultType = std::invoke_result_t<TaskType>;
static constexpr bool s_result_is_void = std::is_void_v<ResultType>;
static_assert(
(s_result_is_void && std::is_invocable_v<ReplyType>) ||
(!s_result_is_void && std::is_invocable_v<ReplyType, ResultType>),
"Either the task must return a non-void type and the reply must be invocable with only "
"that type, or the task must return void and the reply must be invocable without any "
"arguments");
TaskHolder<TaskType> task_holder {std::forward<TaskType>(task)};
TaskHolder<ReplyType> reply_holder {std::forward<ReplyType>(reply)};
return [task_holder = std::move(task_holder),
reply_holder =
std::move(reply_holder)](TaskRunner *runner, TaskLocation location) mutable {
if constexpr (s_result_is_void)
{
std::invoke(std::move(task_holder.m_task));
runner->post_task(std::move(location), std::move(reply_holder.m_task));
}
else
{
auto result = std::invoke(std::move(task_holder.m_task));
runner->post_task(
std::move(location),
std::bind(std::move(reply_holder.m_task), std::move(result)));
}
};
}
//==================================================================================================
template <typename OwnerType, typename TaskType, typename ReplyType>
Task TaskRunner::wrap_task(std::weak_ptr<OwnerType> weak_owner, TaskType &&task, ReplyType &&reply)
{
using StrongOwnerType = std::shared_ptr<OwnerType>;
static_assert(
std::is_invocable_v<TaskType, StrongOwnerType>,
"Task must be invocable with only a strong pointer to its owner");
using ResultType = std::invoke_result_t<TaskType, StrongOwnerType>;
static constexpr bool s_result_is_void = std::is_void_v<ResultType>;
static_assert(
(s_result_is_void && std::is_invocable_v<ReplyType, StrongOwnerType>) ||
(!s_result_is_void && std::is_invocable_v<ReplyType, ResultType, StrongOwnerType>),
"Either the task must return a non-void type and the reply must be invocable with that "
"type and a strong pointer to its owner, or the task must return void and the reply must "
"be invocable with only a strong pointer to its owner");
TaskHolder<TaskType> task_holder {std::forward<TaskType>(task)};
TaskHolder<ReplyType> reply_holder {std::forward<ReplyType>(reply)};
return [weak_owner = std::move(weak_owner),
task_holder = std::move(task_holder),
reply_holder =
std::move(reply_holder)](TaskRunner *runner, TaskLocation location) mutable {
if (StrongOwnerType owner = weak_owner.lock(); owner)
{
if constexpr (s_result_is_void)
{
std::invoke(std::move(task_holder.m_task), std::move(owner));
runner->post_task(
std::move(location),
std::move(weak_owner),
std::move(reply_holder.m_task));
}
else
{
auto result = std::invoke(std::move(task_holder.m_task), std::move(owner));
runner->post_task(
std::move(location),
std::move(weak_owner),
std::bind(
std::move(reply_holder.m_task),
std::move(result),
std::placeholders::_1));
}
}
};
}
} // namespace fly::task