Horizon Official Technical Documentation
TaskScheduler.hpp
Go to the documentation of this file.
1
17#ifndef HORIZON_CORE_UTILITY_TASKSCHEDULER_HPP
18#define HORIZON_CORE_UTILITY_TASKSCHEDULER_HPP
19
20#include <chrono>
21#include <cstdint>
22#include <functional>
23#include <memory>
24#include <set>
25#include <queue>
26#include <mutex>
27#include <random>
28
30typedef std::chrono::microseconds Microseconds;
31typedef std::chrono::milliseconds Milliseconds;
32
34typedef std::chrono::seconds Seconds;
35
37typedef std::chrono::minutes Minutes;
38
40typedef std::chrono::hours Hours;
41
42class TaskContext;
43
58{
59 friend class TaskContext;
60
61 // Time definitions (use steady clock)
62 typedef std::chrono::system_clock clock_t;
63 typedef clock_t::time_point timepoint_t;
64
65 // Duration calculator
66 typedef std::function<clock_t::duration()> duration_calculator_t;
67
68 // Static time
69 template<typename _Rep, typename _Period>
70 static duration_calculator_t MakeDurationCalculator(std::chrono::duration<_Rep, _Period> const& duration)
71 {
72 return [duration]
73 {
74 return duration;
75 };
76 }
77
78 // Random time between min and max
79 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
81 std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
82 std::chrono::duration<_RepRight, _PeriodRight> const& max)
83 {
84 return std::bind(RandomDurationBetween<_RepLeft, _PeriodLeft, _RepRight, _PeriodRight>, min, max);
85 }
86
87 // Task group type
88 typedef uint64_t group_t;
89 // Task repeated type
90 typedef unsigned int repeated_t;
91
92 // Task handle type
93 typedef std::function<void(TaskContext)> task_handler_t;
94 // Predicate type
95 typedef std::function<bool()> predicate_t;
96 // Success handle type
97 typedef std::function<void()> success_t;
98
99 // C++11 std::make_unique workarround
100 template<typename T, typename... Args>
101 static std::unique_ptr<T> MakeUnique(Args&&... args)
102 {
103 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
104 }
105
106 class Task
107 {
108 friend class TaskContext;
109 friend class TaskScheduler;
110
113 std::unique_ptr<group_t> _group;
116
117 public:
118 // All Argument construct
119 Task(timepoint_t const& end, duration_calculator_t&& duration_calculator,
120 group_t const group,
121 repeated_t const repeated, task_handler_t const& task)
122 : _end(end), _duration_calculator(std::move(duration_calculator)),
124 _repeated(repeated), _task(task) { }
125
126 // Minimal Argument construct
127 Task(timepoint_t const& end, duration_calculator_t&& duration_calculator,
128 task_handler_t const& task)
129 : _end(end), _duration_calculator(std::move(duration_calculator)),
130 _group(nullptr), _repeated(0), _task(task) { }
131
132 // Copy construct
133 Task(Task const&) = delete;
134 // Move construct
135 Task(Task&&) = delete;
136 // Copy Assign
137 Task& operator= (Task const&) = delete;
138 // Move Assign
139 Task& operator= (Task&& right) = delete;
140
141 // Order tasks by its end
142 inline bool operator< (Task const& other) const
143 {
144 return _end < other._end;
145 }
146
147 inline bool operator> (Task const& other) const
148 {
149 return _end > other._end;
150 }
151
152 // Compare tasks with its end
153 inline bool operator== (Task const& other)
154 {
155 return _end == other._end;
156 }
157
158 // Returns true if the task is in the given group
159 inline bool IsInGroup(group_t const group) const
160 {
161 return _group && (*_group == group);
162 }
163 };
164
165 typedef std::shared_ptr<Task> TaskContainer;
166
168 struct Compare
169 {
170 bool operator() (TaskContainer const& left, TaskContainer const& right) const
171 {
172 return (*left.get()) < (*right.get());
173 };
174 };
175
177 {
178 std::multiset<TaskContainer, Compare> container;
179
180 public:
181 // Pushes the task in the container
182 void Push(TaskContainer&& task);
183
186
187 TaskContainer const& First() const;
188
189 void Clear();
190
191 void RemoveIf(std::function<bool(TaskContainer const&)> const& filter);
192
193 void ModifyIf(std::function<bool(TaskContainer const&)> const& filter);
194
195 std::size_t Count(group_t const &group);
196
197 bool IsEmpty() const;
198 };
199
201 std::shared_ptr<TaskScheduler> self_reference;
202
205
208
209 typedef std::queue<std::function<void()>> AsyncHolder;
210
214
216
217 static bool EmptyValidator()
218 {
219 return true;
220 }
221
222 static void EmptyCallback()
223 {
224 }
225
226public:
228 : self_reference(this, [](TaskScheduler const*) { }),
229 _now(clock_t::now()), _predicate(EmptyValidator) { }
230
231 template<typename P>
232 TaskScheduler(P&& predicate)
233 : self_reference(this, [](TaskScheduler const*) { }),
234 _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }
235
236 TaskScheduler(TaskScheduler const&) = delete;
240
242 template<typename P>
244 {
245 _predicate = std::forward<P>(predicate);
246 return *this;
247 }
248
251
254 TaskScheduler& Update(success_t const& callback = EmptyCallback);
255
258 TaskScheduler& Update(size_t const microseconds, success_t const& callback = EmptyCallback);
259
262 template<typename _Rep, typename _Period>
263 TaskScheduler& Update(std::chrono::duration<_Rep, _Period> const& difftime,
264 success_t const& callback = EmptyCallback)
265 {
266 _now += difftime;
267 Dispatch(callback);
268 return *this;
269 }
270
273 TaskScheduler& Async(std::function<void()> const& callable);
274
277 template<typename _Rep, typename _Period>
278 TaskScheduler& Schedule(std::chrono::duration<_Rep, _Period> const& time,
279 task_handler_t const& task)
280 {
281 return ScheduleAt(_now, MakeDurationCalculator(time), task);
282 }
283
284 std::size_t Count(group_t const &group);
285
288 template<typename _Rep, typename _Period>
289 TaskScheduler& Schedule(std::chrono::duration<_Rep, _Period> const& time,
290 group_t const group, task_handler_t const& task)
291 {
292 return ScheduleAt(_now, MakeDurationCalculator(time), group, task);
293 }
294
297 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
298 TaskScheduler& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
299 std::chrono::duration<_RepRight, _PeriodRight> const& max, task_handler_t const& task)
300 {
301 return ScheduleAt(_now, MakeDurationCalculator(min, max), task);
302 }
303
306 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
307 TaskScheduler& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
308 std::chrono::duration<_RepRight, _PeriodRight> const& max, group_t const group,
309 task_handler_t const& task)
310 {
311 return ScheduleAt(_now, MakeDurationCalculator(min, max), group, task);
312 }
313
317
320 TaskScheduler& CancelGroup(group_t const group);
321
324 TaskScheduler& CancelGroupsOf(std::vector<group_t> const& groups);
325
327 template<typename _Rep, typename _Period>
328 TaskScheduler& DelayAll(std::chrono::duration<_Rep, _Period> const& duration)
329 {
330 _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool
331 {
332 task->_end += duration;
333 return true;
334 });
335 return *this;
336 }
337
339 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
340 TaskScheduler& DelayAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
341 std::chrono::duration<_RepRight, _PeriodRight> const& max)
342 {
343 return DelayAll(RandomDurationBetween(min, max));
344 }
345
347 template<typename _Rep, typename _Period>
348 TaskScheduler& DelayGroup(group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
349 {
350 _task_holder.ModifyIf([&duration, group](TaskContainer const& task) -> bool
351 {
352 if (task->IsInGroup(group))
353 {
354 task->_end += duration;
355 return true;
356 }
357 else
358 return false;
359 });
360 return *this;
361 }
362
364 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
366 std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
367 std::chrono::duration<_RepRight, _PeriodRight> const& max)
368 {
369 return DelayGroup(group, RandomDurationBetween(min, max));
370 }
371
373 template<typename _Rep, typename _Period>
374 TaskScheduler& RescheduleAll(std::chrono::duration<_Rep, _Period> const& duration)
375 {
376 return RescheduleAt(_now + duration);
377 }
378
380 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
381 TaskScheduler& RescheduleAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
382 std::chrono::duration<_RepRight, _PeriodRight> const& max)
383 {
384 return RescheduleAll(RandomDurationBetween(min, max));
385 }
386
388 template<typename _Rep, typename _Period>
389 TaskScheduler& RescheduleGroup(group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
390 {
391 return RescheduleAtWithPredicate(_now + duration, [&](TaskContainer const& task)
392 {
393 return task->IsInGroup(group);
394 });
395 }
396
398 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
400 std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
401 std::chrono::duration<_RepRight, _PeriodRight> const& max)
402 {
403 return RescheduleGroup(group, RandomDurationBetween(min, max));
404 }
405
406private:
409
411 duration_calculator_t&& duration_calculator, task_handler_t const& task)
412 {
413 return InsertTask(TaskContainer(new Task(end + duration_calculator(), std::move(duration_calculator), task)));
414 }
415
419 duration_calculator_t&& duration_calculator,
420 group_t const group, task_handler_t const& task)
421 {
422 // FIXME Reuse std::unique_ptr
423 static repeated_t const DEFAULT_REPEATED = 0;
424 return InsertTask(TaskContainer(new Task(end + duration_calculator(), std::move(duration_calculator), group, DEFAULT_REPEATED, task)));
425 }
426
428 {
429 return true;
430 }
431
433 {
435 }
436
438 TaskScheduler& RescheduleAtWithPredicate(timepoint_t const& end, std::function<bool(TaskContainer const&)> const& predicate)
439 {
440 _task_holder.ModifyIf([&](TaskContainer const& task) -> bool
441 {
442 if (predicate(task))
443 {
444 task->_end = end;
445 return true;
446 }
447 else
448 return false;
449 });
450 return *this;
451 }
452
453 // Returns a random duration between min and max
454 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
455 std::chrono::duration<_RepLeft, _PeriodLeft>
456 static RandomDurationBetween(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
457 std::chrono::duration<_RepRight, _PeriodRight> const& max)
458 {
459 if (!min.count() && !max.count())
460 return std::chrono::duration<_RepLeft, _PeriodLeft>(0);
461
462 using normalized_t = std::chrono::duration<_RepLeft, _PeriodLeft>;
463 auto normalized = std::chrono::duration_cast<normalized_t>(max);
464
465 if (min.count() > normalized.count())
466 throw std::logic_error("min > max");
467
468 static std::mutex _lock;
469 std::lock_guard<std::mutex> guard(_lock);
470
471 // Random engine
472 static std::random_device rd;
473 static std::mt19937 rng(rd());
474
475 std::uniform_int_distribution<typename std::chrono::duration<_RepLeft, _PeriodLeft>::rep>
476 _distribution(min.count(), normalized.count());
477
478 // Distribute
479 return std::chrono::duration<_RepLeft, _PeriodLeft>(_distribution(rng));
480 }
481
483 void Dispatch(success_t const& callback);
484};
485
487{
488 friend class TaskScheduler;
489
492
494 std::weak_ptr<TaskScheduler> _owner;
495
497 std::shared_ptr<bool> _consumed;
498
500 TaskContext& Dispatch(std::function<TaskScheduler&(TaskScheduler&)> const& apply);
501
502 // Copy assign
503 // TODO Make TaskContext move only
505 {
506 _task = right._task;
507 _owner = right._owner;
508 _consumed = right._consumed;
509 return *this;
510 }
511
512 // Copy construct
513 // TODO Make TaskContext move only
515 : _task(right._task), _owner(right._owner), _consumed(right._consumed) { }
516
517public:
518 // Empty constructor
520 : _task(), _owner(), _consumed(std::make_shared<bool>(true)) { }
521
522 // Construct from task and owner
523 explicit TaskContext(TaskScheduler::TaskContainer&& task, std::weak_ptr<TaskScheduler>&& owner)
524 : _task(task), _owner(owner), _consumed(std::make_shared<bool>(false)) { }
525
526 // Move construct
528 : _task(std::move(right._task)), _owner(std::move(right._owner)),
529 _consumed(std::move(right._consumed)) { }
530
531 // Move assign
533 {
534 _task = std::move(right._task);
535 _owner = std::move(right._owner);
536 _consumed = std::move(right._consumed);
537 return *this;
538 }
539
541 bool IsExpired() const;
542
544 bool IsInGroup(TaskScheduler::group_t const group) const;
545
548
551
554
559 template<typename _Rep, typename _Period>
560 TaskContext& Repeat(std::chrono::duration<_Rep, _Period> const& duration)
561 {
562 _task->_duration_calculator = TaskScheduler::MakeDurationCalculator(duration);
563 return Repeat();
564 }
565
570 {
572
573 // Set new duration, in-context timing and increment repeat counter
574 _task->_end += _task->_duration_calculator();
575 _task->_repeated += 1;
576 (*_consumed) = true;
577 return Dispatch(std::bind(&TaskScheduler::InsertTask, std::placeholders::_1, std::cref(_task)));
578 }
579
584 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
585 TaskContext& Repeat(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
586 std::chrono::duration<_RepRight, _PeriodRight> const& max)
587 {
588 _task->_duration_calculator = TaskScheduler::MakeDurationCalculator(min, max);
589 return Repeat();
590 }
591
594 TaskContext& Async(std::function<void()> const& callable);
595
600 template<typename _Rep, typename _Period>
601 TaskContext& Schedule(std::chrono::duration<_Rep, _Period> const& time,
603 {
604 return Dispatch([&](TaskScheduler& scheduler) -> TaskScheduler&
605 {
606 return scheduler.ScheduleAt(_task->_end, TaskScheduler::MakeDurationCalculator(time), task);
607 });
608 }
609
614 template<typename _Rep, typename _Period>
615 TaskContext& Schedule(std::chrono::duration<_Rep, _Period> const& time,
617 {
618 return Dispatch([&](TaskScheduler& scheduler) -> TaskScheduler&
619 {
620 return scheduler.ScheduleAt(_task->_end, TaskScheduler::MakeDurationCalculator(time), group, task);
621 });
622 }
623
628 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
629 TaskContext& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
630 std::chrono::duration<_RepRight, _PeriodRight> const& max, TaskScheduler::task_handler_t const& task)
631 {
632 return Dispatch([&](TaskScheduler& scheduler) -> TaskScheduler&
633 {
634 return scheduler.ScheduleAt(_task->_end, TaskScheduler::MakeDurationCalculator(min, max), task);
635 });
636 }
637
642 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
643 TaskContext& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
644 std::chrono::duration<_RepRight, _PeriodRight> const& max, TaskScheduler::group_t const group,
646 {
647 return Dispatch([&](TaskScheduler& scheduler) -> TaskScheduler&
648 {
649 return scheduler.ScheduleAt(_task->_end, TaskScheduler::MakeDurationCalculator(min, max), group, task);
650 });
651 }
652
655
658
661 TaskContext& CancelGroupsOf(std::vector<TaskScheduler::group_t> const& groups);
662
664 template<typename _Rep, typename _Period>
665 TaskContext& DelayAll(std::chrono::duration<_Rep, _Period> const& duration)
666 {
667 return Dispatch(std::bind(&TaskScheduler::DelayAll<_Rep, _Period>, std::placeholders::_1, duration));
668 }
669
671 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
672 TaskContext& DelayAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
673 std::chrono::duration<_RepRight, _PeriodRight> const& max)
674 {
676 }
677
679 template<typename _Rep, typename _Period>
680 TaskContext& DelayGroup(TaskScheduler::group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
681 {
682 return Dispatch(std::bind(&TaskScheduler::DelayGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
683 }
684
686 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
688 std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
689 std::chrono::duration<_RepRight, _PeriodRight> const& max)
690 {
691 return DelayGroup(group, TaskScheduler::RandomDurationBetween(min, max));
692 }
693
695 template<typename _Rep, typename _Period>
696 TaskContext& RescheduleAll(std::chrono::duration<_Rep, _Period> const& duration)
697 {
698 return Dispatch(std::bind(&TaskScheduler::RescheduleAt, std::placeholders::_1, _task->_end + duration));
699 }
700
702 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
703 TaskContext& RescheduleAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
704 std::chrono::duration<_RepRight, _PeriodRight> const& max)
705 {
707 }
708
710 template<typename _Rep, typename _Period>
711 TaskContext& RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
712 {
713 return Dispatch(std::bind(&TaskScheduler::RescheduleAtWithPredicate, std::placeholders::_1, _task->_end + duration,
714 [&](TaskScheduler::TaskContainer const& task)
715 {
716 return task->IsInGroup(group);
717 }));
718 }
719
721 template<typename _RepLeft, typename _PeriodLeft, typename _RepRight, typename _PeriodRight>
723 std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
724 std::chrono::duration<_RepRight, _PeriodRight> const& max)
725 {
727 }
728
729private:
731 void ThrowOnConsumed() const;
732
734 void Invoke();
735};
736
737#endif /* HORIZON_CORE_UTILITY_TASKSCHEDULER_HPP */
std::chrono::hours Hours
Hours shorthand typedef.
Definition: TaskScheduler.hpp:40
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: TaskScheduler.hpp:34
std::chrono::milliseconds Milliseconds
Definition: TaskScheduler.hpp:31
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition: TaskScheduler.hpp:37
std::chrono::microseconds Microseconds
Copyright 2014-2015 Denis Blank denis.blank@outlook.com
Definition: TaskScheduler.hpp:30
Definition: TaskScheduler.hpp:487
TaskContext & RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Reschedule all tasks of a group with a random duration between min and max.
Definition: TaskScheduler.hpp:722
TaskContext & Repeat(std::chrono::duration< _Rep, _Period > const &duration)
Repeats the event and sets a new duration. std::chrono::seconds(5) for example. This will consume the...
Definition: TaskScheduler.hpp:560
void Invoke()
Invokes the associated hook of the task.
Definition: TaskScheduler.cpp:244
TaskContext & Dispatch(std::function< TaskScheduler &(TaskScheduler &)> const &apply)
Dispatches an action safe on the TaskScheduler.
Definition: TaskScheduler.cpp:182
TaskContext & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, TaskScheduler::group_t const group, TaskScheduler::task_handler_t const &task)
Schedule an event with a randomized rate between min and max rate from within the context....
Definition: TaskScheduler.hpp:643
TaskContext(TaskContext &&right)
Definition: TaskScheduler.hpp:527
TaskContext & Repeat(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Repeats the event and set a new duration that is randomized between min and max. std::chrono::seconds...
Definition: TaskScheduler.hpp:585
TaskContext & CancelAll()
Cancels all tasks from within the context.
Definition: TaskScheduler.cpp:222
TaskContext & DelayGroup(TaskScheduler::group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Delays all tasks of a group with a random duration between min and max from within the context.
Definition: TaskScheduler.hpp:687
TaskScheduler::TaskContainer _task
Associated task.
Definition: TaskScheduler.hpp:491
std::weak_ptr< TaskScheduler > _owner
Owner.
Definition: TaskScheduler.hpp:494
TaskContext & SetGroup(TaskScheduler::group_t const group)
Sets the event in the given group.
Definition: TaskScheduler.cpp:200
std::shared_ptr< bool > _consumed
Marks the task as consumed.
Definition: TaskScheduler.hpp:497
TaskContext & Async(std::function< void()> const &callable)
Schedule a callable function that is executed at the next update tick from within the context....
Definition: TaskScheduler.cpp:217
TaskContext & CancelGroup(TaskScheduler::group_t const group)
Cancel all tasks of a single group from within the context.
Definition: TaskScheduler.cpp:227
void ThrowOnConsumed() const
Throws std::logic_error if the task was consumed already.
Definition: TaskScheduler.cpp:237
TaskContext & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, TaskScheduler::task_handler_t const &task)
Schedule an event with a randomized rate between min and max rate from within the context....
Definition: TaskScheduler.hpp:629
TaskContext & Schedule(std::chrono::duration< _Rep, _Period > const &time, TaskScheduler::group_t const group, TaskScheduler::task_handler_t const &task)
Schedule an event with a fixed rate from within the context. Its possible that the new event is execu...
Definition: TaskScheduler.hpp:615
TaskScheduler::repeated_t GetRepeatCounter() const
Returns the repeat counter which increases every time the task is repeated.
Definition: TaskScheduler.cpp:212
TaskContext & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration from within the context.
Definition: TaskScheduler.hpp:665
TaskContext & DelayGroup(TaskScheduler::group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks of a group with the given duration from within the context.
Definition: TaskScheduler.hpp:680
TaskContext & operator=(TaskContext const &right)
Definition: TaskScheduler.hpp:504
TaskContext & DelayAll(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Delays all tasks with a random duration between min and max from within the context.
Definition: TaskScheduler.hpp:672
TaskContext & CancelGroupsOf(std::vector< TaskScheduler::group_t > const &groups)
Cancels all groups in the given std::vector from within the context. Hint: Use std::initializer_list ...
Definition: TaskScheduler.cpp:232
TaskContext & RescheduleAll(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Reschedule all tasks with a random duration between min and max.
Definition: TaskScheduler.hpp:703
TaskContext & ClearGroup()
Removes the group from the event.
Definition: TaskScheduler.cpp:206
TaskContext()
Definition: TaskScheduler.hpp:519
TaskContext(TaskContext const &right)
Definition: TaskScheduler.hpp:514
bool IsInGroup(TaskScheduler::group_t const group) const
Returns true if the event is in the given group.
Definition: TaskScheduler.cpp:195
TaskContext & Schedule(std::chrono::duration< _Rep, _Period > const &time, TaskScheduler::task_handler_t const &task)
Schedule an event with a fixed rate from within the context. Its possible that the new event is execu...
Definition: TaskScheduler.hpp:601
TaskContext & Repeat()
Repeats the event with the same duration. This will consume the task context, its not possible to rep...
Definition: TaskScheduler.hpp:569
TaskContext(TaskScheduler::TaskContainer &&task, std::weak_ptr< TaskScheduler > &&owner)
Definition: TaskScheduler.hpp:523
TaskContext & RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks of a group with the given duration.
Definition: TaskScheduler.hpp:711
TaskContext & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with the given duration.
Definition: TaskScheduler.hpp:696
bool IsExpired() const
Returns true if the owner was deallocated and this context has expired.
Definition: TaskScheduler.cpp:190
Definition: TaskScheduler.hpp:177
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:162
bool IsEmpty() const
Definition: TaskScheduler.cpp:177
TaskContainer Pop()
Pops the task out of the container.
Definition: TaskScheduler.cpp:121
void RemoveIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:153
TaskContainer const & First() const
Definition: TaskScheduler.cpp:129
std::multiset< TaskContainer, Compare > container
Definition: TaskScheduler.hpp:178
void Clear()
Definition: TaskScheduler.cpp:135
std::size_t Count(group_t const &group)
Definition: TaskScheduler.cpp:140
void Push(TaskContainer &&task)
Definition: TaskScheduler.cpp:116
Definition: TaskScheduler.hpp:107
timepoint_t _end
Definition: TaskScheduler.hpp:111
bool IsInGroup(group_t const group) const
Definition: TaskScheduler.hpp:159
task_handler_t _task
Definition: TaskScheduler.hpp:115
Task(timepoint_t const &end, duration_calculator_t &&duration_calculator, group_t const group, repeated_t const repeated, task_handler_t const &task)
Definition: TaskScheduler.hpp:119
Task & operator=(Task const &)=delete
bool operator>(Task const &other) const
Definition: TaskScheduler.hpp:147
Task(timepoint_t const &end, duration_calculator_t &&duration_calculator, task_handler_t const &task)
Definition: TaskScheduler.hpp:127
duration_calculator_t _duration_calculator
Definition: TaskScheduler.hpp:112
Task(Task const &)=delete
repeated_t _repeated
Definition: TaskScheduler.hpp:114
Task(Task &&)=delete
bool operator<(Task const &other) const
Definition: TaskScheduler.hpp:142
std::unique_ptr< group_t > _group
Definition: TaskScheduler.hpp:113
bool operator==(Task const &other)
Definition: TaskScheduler.hpp:153
The TaskScheduler class provides the ability to schedule std::function's in the near future....
Definition: TaskScheduler.hpp:58
TaskScheduler & RescheduleAtWithPredicate(timepoint_t const &end, std::function< bool(TaskContainer const &)> const &predicate)
Reschedule all tasks with a given duration relative to the given time.
Definition: TaskScheduler.hpp:438
TaskScheduler & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration.
Definition: TaskScheduler.hpp:328
TaskScheduler & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with a given duration.
Definition: TaskScheduler.hpp:374
TaskScheduler & CancelAll()
Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead!
Definition: TaskScheduler.cpp:43
std::size_t Count(group_t const &group)
Definition: TaskScheduler.cpp:73
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.hpp:165
TaskScheduler & operator=(TaskScheduler const &)=delete
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Sch...
Definition: TaskScheduler.hpp:278
clock_t::time_point timepoint_t
Definition: TaskScheduler.hpp:63
TaskScheduler & Async(std::function< void()> const &callable)
Schedule an callable function that is executed at the next update tick. Its safe to modify the TaskSc...
Definition: TaskScheduler.cpp:37
TaskScheduler & CancelGroupsOf(std::vector< group_t > const &groups)
Cancels all groups in the given std::vector. Hint: Use std::initializer_list for this: "{1,...
Definition: TaskScheduler.cpp:59
void Dispatch(success_t const &callback)
Dispatch remaining tasks when the given condition fits.
Definition: TaskScheduler.cpp:78
std::function< clock_t::duration()> duration_calculator_t
Definition: TaskScheduler.hpp:66
TaskScheduler & RescheduleAt(timepoint_t const &end)
Definition: TaskScheduler.hpp:432
unsigned int repeated_t
Definition: TaskScheduler.hpp:90
TaskScheduler(TaskScheduler &&)=delete
TaskScheduler & Update(success_t const &callback=EmptyCallback)
Update the scheduler to the current time. Calls the optional callback on successfully finish.
Definition: TaskScheduler.cpp:25
static duration_calculator_t MakeDurationCalculator(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.hpp:80
TaskScheduler & Update(std::chrono::duration< _Rep, _Period > const &difftime, success_t const &callback=EmptyCallback)
Update the scheduler with a difftime. Calls the optional callback on successfully finish.
Definition: TaskScheduler.hpp:263
std::function< void()> success_t
Definition: TaskScheduler.hpp:97
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.hpp:201
std::function< void(TaskContext)> task_handler_t
Definition: TaskScheduler.hpp:93
std::function< bool()> predicate_t
Definition: TaskScheduler.hpp:95
std::chrono::system_clock clock_t
Definition: TaskScheduler.hpp:62
TaskScheduler & DelayAll(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Delays all tasks with a random duration between min and max.
Definition: TaskScheduler.hpp:340
AsyncHolder _asyncHolder
Contains all asynchronous tasks which will be invoked at the next update tick.
Definition: TaskScheduler.hpp:213
TaskScheduler & RescheduleGroup(group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Reschedule all tasks of a group with a random duration between min and max.
Definition: TaskScheduler.hpp:399
TaskScheduler(P &&predicate)
Definition: TaskScheduler.hpp:232
static std::unique_ptr< T > MakeUnique(Args &&... args)
Definition: TaskScheduler.hpp:101
TaskScheduler & DelayGroup(group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks of a group with the given duration.
Definition: TaskScheduler.hpp:348
predicate_t _predicate
Definition: TaskScheduler.hpp:215
TaskScheduler & ScheduleAt(timepoint_t const &end, duration_calculator_t &&duration_calculator, task_handler_t const &task)
Definition: TaskScheduler.hpp:410
TaskScheduler & ScheduleAt(timepoint_t const &end, duration_calculator_t &&duration_calculator, group_t const group, task_handler_t const &task)
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::sch...
Definition: TaskScheduler.hpp:418
static std::chrono::duration< _RepLeft, _PeriodLeft > RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.hpp:456
TaskScheduler & CancelGroup(group_t const group)
Cancel all tasks of a single group. Never call this from within a task context! Use TaskContext::Canc...
Definition: TaskScheduler.cpp:51
TaskScheduler()
Definition: TaskScheduler.hpp:227
TaskScheduler & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, task_handler_t const &task)
Schedule an event with a randomized rate between min and max rate. Never call this from within a task...
Definition: TaskScheduler.hpp:298
static duration_calculator_t MakeDurationCalculator(std::chrono::duration< _Rep, _Period > const &duration)
Definition: TaskScheduler.hpp:70
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task)
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Sch...
Definition: TaskScheduler.hpp:289
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:67
uint64_t group_t
Definition: TaskScheduler.hpp:88
TaskScheduler & ClearValidator()
Clears the Validator which is asked if tasks are allowed to be executed.
Definition: TaskScheduler.cpp:19
TaskScheduler & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, group_t const group, task_handler_t const &task)
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Sch...
Definition: TaskScheduler.hpp:307
TaskScheduler(TaskScheduler const &)=delete
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.hpp:204
std::queue< std::function< void()> > AsyncHolder
Definition: TaskScheduler.hpp:209
static bool AlwaysTruePredicate(TaskContainer const &)
Definition: TaskScheduler.hpp:427
TaskScheduler & DelayGroup(group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Delays all tasks of a group with a random duration between min and max.
Definition: TaskScheduler.hpp:365
static bool EmptyValidator()
Definition: TaskScheduler.hpp:217
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.hpp:207
TaskScheduler & SetValidator(P &&predicate)
Sets a Validator which is asked if tasks are allowed to be executed.
Definition: TaskScheduler.hpp:243
static void EmptyCallback()
Definition: TaskScheduler.hpp:222
TaskScheduler & RescheduleGroup(group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks of a group with the given duration.
Definition: TaskScheduler.hpp:389
TaskScheduler & RescheduleAll(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Reschedule all tasks with a random duration between min and max.
Definition: TaskScheduler.hpp:381
void apply(T *val)
Definition: ByteConverter.hpp:48
Container which provides Task order, insert and reschedule operations.
Definition: TaskScheduler.hpp:169
bool operator()(TaskContainer const &left, TaskContainer const &right) const
Definition: TaskScheduler.hpp:170