17#ifndef HORIZON_CORE_UTILITY_TASKSCHEDULER_HPP
18#define HORIZON_CORE_UTILITY_TASKSCHEDULER_HPP
40typedef std::chrono::hours
Hours;
62 typedef std::chrono::system_clock
clock_t;
69 template<
typename _Rep,
typename _Period>
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)
84 return std::bind(RandomDurationBetween<_RepLeft, _PeriodLeft, _RepRight, _PeriodRight>, min, max);
100 template<
typename T,
typename... Args>
103 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
172 return (*left.get()) < (*right.get());
262 template<
typename _Rep,
typename _Period>
277 template<
typename _Rep,
typename _Period>
288 template<
typename _Rep,
typename _Period>
297 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
299 std::chrono::duration<_RepRight, _PeriodRight>
const& max,
task_handler_t const& task)
306 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
308 std::chrono::duration<_RepRight, _PeriodRight>
const& max,
group_t const group,
327 template<
typename _Rep,
typename _Period>
332 task->_end += duration;
339 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
341 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
347 template<
typename _Rep,
typename _Period>
352 if (task->IsInGroup(group))
354 task->_end += duration;
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)
373 template<
typename _Rep,
typename _Period>
380 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
382 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
388 template<
typename _Rep,
typename _Period>
393 return task->IsInGroup(group);
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)
424 return InsertTask(
TaskContainer(
new Task(end + duration_calculator(), std::move(duration_calculator), group, DEFAULT_REPEATED, task)));
454 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
455 std::chrono::duration<_RepLeft, _PeriodLeft>
457 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
459 if (!min.count() && !max.count())
460 return std::chrono::duration<_RepLeft, _PeriodLeft>(0);
462 using normalized_t = std::chrono::duration<_RepLeft, _PeriodLeft>;
463 auto normalized = std::chrono::duration_cast<normalized_t>(max);
465 if (min.count() > normalized.count())
466 throw std::logic_error(
"min > max");
468 static std::mutex _lock;
469 std::lock_guard<std::mutex> guard(_lock);
472 static std::random_device rd;
473 static std::mt19937 rng(rd());
475 std::uniform_int_distribution<typename std::chrono::duration<_RepLeft, _PeriodLeft>::rep>
476 _distribution(min.count(), normalized.count());
479 return std::chrono::duration<_RepLeft, _PeriodLeft>(_distribution(rng));
534 _task = std::move(right._task);
535 _owner = std::move(right._owner);
559 template<
typename _Rep,
typename _Period>
575 _task->_repeated += 1;
584 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
586 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
600 template<
typename _Rep,
typename _Period>
614 template<
typename _Rep,
typename _Period>
628 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
642 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
664 template<
typename _Rep,
typename _Period>
667 return Dispatch(std::bind(&TaskScheduler::DelayAll<_Rep, _Period>, std::placeholders::_1, duration));
671 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
673 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
679 template<
typename _Rep,
typename _Period>
682 return Dispatch(std::bind(&TaskScheduler::DelayGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
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)
695 template<
typename _Rep,
typename _Period>
702 template<
typename _RepLeft,
typename _PeriodLeft,
typename _RepRight,
typename _PeriodRight>
704 std::chrono::duration<_RepRight, _PeriodRight>
const& max)
710 template<
typename _Rep,
typename _Period>
716 return task->IsInGroup(group);
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)
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
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