Horizon Official Technical Documentation
TaskScheduler Class Reference

The TaskScheduler class provides the ability to schedule std::function's in the near future. Use TaskScheduler::Update to update the scheduler. Popular methods are: More...

#include <TaskScheduler.hpp>

+ Collaboration diagram for TaskScheduler:

Classes

struct  Compare
 Container which provides Task order, insert and reschedule operations. More...
 
class  Task
 
class  TaskQueue
 

Public Member Functions

 TaskScheduler ()
 
template<typename P >
 TaskScheduler (P &&predicate)
 
 TaskScheduler (TaskScheduler const &)=delete
 
 TaskScheduler (TaskScheduler &&)=delete
 
TaskScheduleroperator= (TaskScheduler const &)=delete
 
TaskScheduleroperator= (TaskScheduler &&)=delete
 
template<typename P >
TaskSchedulerSetValidator (P &&predicate)
 Sets a Validator which is asked if tasks are allowed to be executed. More...
 
TaskSchedulerClearValidator ()
 Clears the Validator which is asked if tasks are allowed to be executed. More...
 
TaskSchedulerUpdate (success_t const &callback=EmptyCallback)
 Update the scheduler to the current time. Calls the optional callback on successfully finish. More...
 
TaskSchedulerUpdate (size_t const microseconds, success_t const &callback=EmptyCallback)
 Update the scheduler with a difftime in ms. Calls the optional callback on successfully finish. More...
 
template<typename _Rep , typename _Period >
TaskSchedulerUpdate (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. More...
 
TaskSchedulerAsync (std::function< void()> const &callable)
 Schedule an callable function that is executed at the next update tick. Its safe to modify the TaskScheduler from within the callable. More...
 
template<typename _Rep , typename _Period >
TaskSchedulerSchedule (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::Schedule instead! More...
 
std::size_t Count (group_t const &group)
 
template<typename _Rep , typename _Period >
TaskSchedulerSchedule (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::Schedule instead! More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerSchedule (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 context! Use TaskContext::Schedule instead! More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerSchedule (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::Schedule instead! More...
 
TaskSchedulerCancelAll ()
 Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead! More...
 
TaskSchedulerCancelGroup (group_t const group)
 Cancel all tasks of a single group. Never call this from within a task context! Use TaskContext::CancelGroup instead! More...
 
TaskSchedulerCancelGroupsOf (std::vector< group_t > const &groups)
 Cancels all groups in the given std::vector. Hint: Use std::initializer_list for this: "{1, 2, 3, 4}". More...
 
template<typename _Rep , typename _Period >
TaskSchedulerDelayAll (std::chrono::duration< _Rep, _Period > const &duration)
 Delays all tasks with the given duration. More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerDelayAll (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. More...
 
template<typename _Rep , typename _Period >
TaskSchedulerDelayGroup (group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
 Delays all tasks of a group with the given duration. More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerDelayGroup (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. More...
 
template<typename _Rep , typename _Period >
TaskSchedulerRescheduleAll (std::chrono::duration< _Rep, _Period > const &duration)
 Reschedule all tasks with a given duration. More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerRescheduleAll (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. More...
 
template<typename _Rep , typename _Period >
TaskSchedulerRescheduleGroup (group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
 Reschedule all tasks of a group with the given duration. More...
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskSchedulerRescheduleGroup (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. More...
 

Private Types

typedef std::chrono::system_clock clock_t
 
typedef clock_t::time_point timepoint_t
 
typedef std::function< clock_t::duration()> duration_calculator_t
 
typedef uint64_t group_t
 
typedef unsigned int repeated_t
 
typedef std::function< void(TaskContext)> task_handler_t
 
typedef std::function< bool()> predicate_t
 
typedef std::function< void()> success_t
 
typedef std::shared_ptr< TaskTaskContainer
 
typedef std::queue< std::function< void()> > AsyncHolder
 

Private Member Functions

TaskSchedulerInsertTask (TaskContainer task)
 Insert a new task to the enqueued tasks. More...
 
TaskSchedulerScheduleAt (timepoint_t const &end, duration_calculator_t &&duration_calculator, task_handler_t const &task)
 
TaskSchedulerScheduleAt (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::schedule instead! More...
 
TaskSchedulerRescheduleAt (timepoint_t const &end)
 
TaskSchedulerRescheduleAtWithPredicate (timepoint_t const &end, std::function< bool(TaskContainer const &)> const &predicate)
 Reschedule all tasks with a given duration relative to the given time. More...
 
void Dispatch (success_t const &callback)
 Dispatch remaining tasks when the given condition fits. More...
 

Static Private Member Functions

template<typename _Rep , typename _Period >
static duration_calculator_t MakeDurationCalculator (std::chrono::duration< _Rep, _Period > const &duration)
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
static duration_calculator_t MakeDurationCalculator (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
 
template<typename T , typename... Args>
static std::unique_ptr< T > MakeUnique (Args &&... args)
 
static bool EmptyValidator ()
 
static void EmptyCallback ()
 
static bool AlwaysTruePredicate (TaskContainer const &)
 
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
static std::chrono::duration< _RepLeft, _PeriodLeft > RandomDurationBetween (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
 

Private Attributes

std::shared_ptr< TaskSchedulerself_reference
 Contains a self reference to track if this object was deleted or not. More...
 
timepoint_t _now
 The current time point (now) More...
 
TaskQueue _task_holder
 The Task Queue which contains all task objects. More...
 
AsyncHolder _asyncHolder
 Contains all asynchronous tasks which will be invoked at the next update tick. More...
 
predicate_t _predicate
 

Friends

class TaskContext
 

Detailed Description

The TaskScheduler class provides the ability to schedule std::function's in the near future. Use TaskScheduler::Update to update the scheduler. Popular methods are:

  • Schedule (Schedules a std::function which will be executed in the near future).
  • Schedules an asynchronous function which will be executed at the next update tick.
  • Cancel, Delay & Reschedule (Methods to manipulate already scheduled tasks). Tasks are organized in groups (uint), multiple tasks can have the same group id, you can provide a group or not, but keep in mind that you can only manipulate specific tasks through its group id! Tasks callbacks use the function signature void(TaskContext) where TaskContext provides access to the function schedule plan which makes it possible to repeat the task with the same duration or a new one. It also provides access to the repeat counter which is useful for task that repeat itself often but behave different every time (spoken event HLogs for example).

Member Typedef Documentation

◆ AsyncHolder

typedef std::queue<std::function<void()> > TaskScheduler::AsyncHolder
private

◆ clock_t

typedef std::chrono::system_clock TaskScheduler::clock_t
private

◆ duration_calculator_t

typedef std::function<clock_t::duration()> TaskScheduler::duration_calculator_t
private

◆ group_t

typedef uint64_t TaskScheduler::group_t
private

◆ predicate_t

typedef std::function<bool()> TaskScheduler::predicate_t
private

◆ repeated_t

typedef unsigned int TaskScheduler::repeated_t
private

◆ success_t

typedef std::function<void()> TaskScheduler::success_t
private

◆ task_handler_t

typedef std::function<void(TaskContext)> TaskScheduler::task_handler_t
private

◆ TaskContainer

typedef std::shared_ptr<Task> TaskScheduler::TaskContainer
private

◆ timepoint_t

typedef clock_t::time_point TaskScheduler::timepoint_t
private

Constructor & Destructor Documentation

◆ TaskScheduler() [1/4]

TaskScheduler::TaskScheduler ( )
inline
228 : self_reference(this, [](TaskScheduler const*) { }),
229 _now(clock_t::now()), _predicate(EmptyValidator) { }
The TaskScheduler class provides the ability to schedule std::function's in the near future....
Definition: TaskScheduler.hpp:58
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.hpp:201
predicate_t _predicate
Definition: TaskScheduler.hpp:215
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.hpp:204
static bool EmptyValidator()
Definition: TaskScheduler.hpp:217

◆ TaskScheduler() [2/4]

template<typename P >
TaskScheduler::TaskScheduler ( P &&  predicate)
inline
233 : self_reference(this, [](TaskScheduler const*) { }),
234 _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }

◆ TaskScheduler() [3/4]

TaskScheduler::TaskScheduler ( TaskScheduler const &  )
delete

◆ TaskScheduler() [4/4]

TaskScheduler::TaskScheduler ( TaskScheduler &&  )
delete

Member Function Documentation

◆ AlwaysTruePredicate()

static bool TaskScheduler::AlwaysTruePredicate ( TaskContainer const &  )
inlinestaticprivate
428 {
429 return true;
430 }

Referenced by RescheduleAt().

+ Here is the caller graph for this function:

◆ Async()

TaskScheduler & TaskScheduler::Async ( std::function< void()> const &  callable)

Schedule an callable function that is executed at the next update tick. Its safe to modify the TaskScheduler from within the callable.

38{
39 _asyncHolder.push(callable);
40 return *this;
41}
AsyncHolder _asyncHolder
Contains all asynchronous tasks which will be invoked at the next update tick.
Definition: TaskScheduler.hpp:213

References _asyncHolder.

Referenced by TaskContext::Async().

+ Here is the caller graph for this function:

◆ CancelAll()

TaskScheduler & TaskScheduler::CancelAll ( )

Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead!

Clear the task holder

44{
48 return *this;
49}
void Clear()
Definition: TaskScheduler.cpp:135
std::queue< std::function< void()> > AsyncHolder
Definition: TaskScheduler.hpp:209
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.hpp:207

References _asyncHolder, _task_holder, and TaskScheduler::TaskQueue::Clear().

Referenced by TaskContext::CancelAll(), Horizon::Zone::ZoneKernel::finalize(), Horizon::Auth::AuthServer::update(), and Horizon::Char::CharServer::update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CancelGroup()

TaskScheduler & TaskScheduler::CancelGroup ( group_t const  group)

Cancel all tasks of a single group. Never call this from within a task context! Use TaskContext::CancelGroup instead!

52{
54 [group] (TaskContainer const &task) -> bool
55 { return task->IsInGroup(group); });
56 return *this;
57}
void RemoveIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:153
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.hpp:165

References _task_holder, and TaskScheduler::TaskQueue::RemoveIf().

Referenced by TaskContext::CancelGroup(), and CancelGroupsOf().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CancelGroupsOf()

TaskScheduler & TaskScheduler::CancelGroupsOf ( std::vector< group_t > const &  groups)

Cancels all groups in the given std::vector. Hint: Use std::initializer_list for this: "{1, 2, 3, 4}".

60{
61 std::for_each(groups.begin(), groups.end(),
62 std::bind(&TaskScheduler::CancelGroup, this, std::placeholders::_1));
63
64 return *this;
65}
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

References CancelGroup().

Referenced by TaskContext::CancelGroupsOf().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ClearValidator()

TaskScheduler & TaskScheduler::ClearValidator ( )

Clears the Validator which is asked if tasks are allowed to be executed.

Copyright 2014-2015 Denis Blank denis.nosp@m..bla.nosp@m.nk@ou.nosp@m.tloo.nosp@m.k.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

20{
22 return *this;
23}

References _predicate, and EmptyValidator().

+ Here is the call graph for this function:

◆ Count()

std::size_t TaskScheduler::Count ( group_t const &  group)
74{
75 return _task_holder.Count(group);
76}
std::size_t Count(group_t const &group)
Definition: TaskScheduler.cpp:140

References _task_holder, and TaskScheduler::TaskQueue::Count().

+ Here is the call graph for this function:

◆ DelayAll() [1/2]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::DelayAll ( std::chrono::duration< _Rep, _Period > const &  duration)
inline

Delays all tasks with the given duration.

329 {
330 _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool
331 {
332 task->_end += duration;
333 return true;
334 });
335 return *this;
336 }
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:162

References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().

Referenced by DelayAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DelayAll() [2/2]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & TaskScheduler::DelayAll ( std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inline

Delays all tasks with a random duration between min and max.

342 {
343 return DelayAll(RandomDurationBetween(min, max));
344 }
TaskScheduler & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration.
Definition: TaskScheduler.hpp:328
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

References DelayAll(), and RandomDurationBetween().

+ Here is the call graph for this function:

◆ DelayGroup() [1/2]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::DelayGroup ( group_t const  group,
std::chrono::duration< _Rep, _Period > const &  duration 
)
inline

Delays all tasks of a group with the given 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 }

References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().

Referenced by DelayGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DelayGroup() [2/2]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & TaskScheduler::DelayGroup ( group_t const  group,
std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inline

Delays all tasks of a group with a random duration between min and max.

368 {
369 return DelayGroup(group, RandomDurationBetween(min, max));
370 }
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

References DelayGroup(), and RandomDurationBetween().

+ Here is the call graph for this function:

◆ Dispatch()

void TaskScheduler::Dispatch ( success_t const &  callback)
private

Dispatch remaining tasks when the given condition fits.

79{
80 // If the validation failed abort the dispatching here.
81 if (!_predicate())
82 return;
83
84 // Process all asyncs
85 while (!_asyncHolder.empty())
86 {
87 _asyncHolder.front()();
88 _asyncHolder.pop();
89
90 // If the validation failed abort the dispatching here.
91 if (!_predicate())
92 return;
93 }
94
95 while (!_task_holder.IsEmpty())
96 {
97 if (_task_holder.First()->_end > _now)
98 break;
99
100 // Perfect forward the context to the handler
101 // Use weak references to catch destruction before callbacks.
102 TaskContext context(_task_holder.Pop(), std::weak_ptr<TaskScheduler>(self_reference));
103
104 // Invoke the context
105 context.Invoke();
106
107 // If the validation failed abort the dispatching here.
108 if (!_predicate())
109 return;
110 }
111
112 // On finish call the final callback
113 callback();
114}
Definition: TaskScheduler.hpp:487
bool IsEmpty() const
Definition: TaskScheduler.cpp:177
TaskContainer Pop()
Pops the task out of the container.
Definition: TaskScheduler.cpp:121
TaskContainer const & First() const
Definition: TaskScheduler.cpp:129

References _asyncHolder, _now, _predicate, _task_holder, TaskScheduler::TaskQueue::First(), TaskContext::Invoke(), TaskScheduler::TaskQueue::IsEmpty(), TaskScheduler::TaskQueue::Pop(), and self_reference.

Referenced by TaskContext::Async(), TaskContext::CancelAll(), TaskContext::CancelGroup(), TaskContext::CancelGroupsOf(), and Update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ EmptyCallback()

static void TaskScheduler::EmptyCallback ( )
inlinestaticprivate
223 {
224 }

◆ EmptyValidator()

static bool TaskScheduler::EmptyValidator ( )
inlinestaticprivate
218 {
219 return true;
220 }

Referenced by ClearValidator().

+ Here is the caller graph for this function:

◆ InsertTask()

TaskScheduler & TaskScheduler::InsertTask ( TaskContainer  task)
private

Insert a new task to the enqueued tasks.

68{
69 _task_holder.Push(std::move(task));
70 return *this;
71}
void Push(TaskContainer &&task)
Definition: TaskScheduler.cpp:116

References _task_holder, and TaskScheduler::TaskQueue::Push().

Referenced by TaskContext::Repeat(), and ScheduleAt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MakeDurationCalculator() [1/2]

template<typename _Rep , typename _Period >
static duration_calculator_t TaskScheduler::MakeDurationCalculator ( std::chrono::duration< _Rep, _Period > const &  duration)
inlinestaticprivate
71 {
72 return [duration]
73 {
74 return duration;
75 };
76 }

Referenced by TaskContext::Repeat(), Schedule(), and TaskContext::Schedule().

+ Here is the caller graph for this function:

◆ MakeDurationCalculator() [2/2]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
static duration_calculator_t TaskScheduler::MakeDurationCalculator ( std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inlinestaticprivate
83 {
84 return std::bind(RandomDurationBetween<_RepLeft, _PeriodLeft, _RepRight, _PeriodRight>, min, max);
85 }

◆ MakeUnique()

template<typename T , typename... Args>
static std::unique_ptr< T > TaskScheduler::MakeUnique ( Args &&...  args)
inlinestaticprivate
102 {
103 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
104 }

◆ operator=() [1/2]

TaskScheduler & TaskScheduler::operator= ( TaskScheduler &&  )
delete

◆ operator=() [2/2]

TaskScheduler & TaskScheduler::operator= ( TaskScheduler const &  )
delete

◆ RandomDurationBetween()

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
static std::chrono::duration< _RepLeft, _PeriodLeft > TaskScheduler::RandomDurationBetween ( std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inlinestaticprivate
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 }

Referenced by DelayAll(), TaskContext::DelayAll(), DelayGroup(), TaskContext::DelayGroup(), RescheduleAll(), TaskContext::RescheduleAll(), RescheduleGroup(), and TaskContext::RescheduleGroup().

+ Here is the caller graph for this function:

◆ RescheduleAll() [1/2]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::RescheduleAll ( std::chrono::duration< _Rep, _Period > const &  duration)
inline

Reschedule all tasks with a given duration.

375 {
376 return RescheduleAt(_now + duration);
377 }
TaskScheduler & RescheduleAt(timepoint_t const &end)
Definition: TaskScheduler.hpp:432

References _now, and RescheduleAt().

Referenced by RescheduleAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RescheduleAll() [2/2]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & TaskScheduler::RescheduleAll ( std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inline

Reschedule all tasks with a random duration between min and max.

383 {
384 return RescheduleAll(RandomDurationBetween(min, max));
385 }
TaskScheduler & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with a given duration.
Definition: TaskScheduler.hpp:374

References RandomDurationBetween(), and RescheduleAll().

+ Here is the call graph for this function:

◆ RescheduleAt()

TaskScheduler & TaskScheduler::RescheduleAt ( timepoint_t const &  end)
inlineprivate
433 {
435 }
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
static bool AlwaysTruePredicate(TaskContainer const &)
Definition: TaskScheduler.hpp:427

References AlwaysTruePredicate(), and RescheduleAtWithPredicate().

Referenced by RescheduleAll(), and TaskContext::RescheduleAll().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RescheduleAtWithPredicate()

TaskScheduler & TaskScheduler::RescheduleAtWithPredicate ( timepoint_t const &  end,
std::function< bool(TaskContainer const &)> const &  predicate 
)
inlineprivate

Reschedule all tasks with a given duration relative to the given time.

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 }

References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().

Referenced by RescheduleAt(), RescheduleGroup(), and TaskContext::RescheduleGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RescheduleGroup() [1/2]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::RescheduleGroup ( group_t const  group,
std::chrono::duration< _Rep, _Period > const &  duration 
)
inline

Reschedule all tasks of a group with the given duration.

390 {
391 return RescheduleAtWithPredicate(_now + duration, [&](TaskContainer const& task)
392 {
393 return task->IsInGroup(group);
394 });
395 }

References _now, and RescheduleAtWithPredicate().

Referenced by RescheduleGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RescheduleGroup() [2/2]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & TaskScheduler::RescheduleGroup ( group_t const  group,
std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max 
)
inline

Reschedule all tasks of a group with a random duration between min and max.

402 {
403 return RescheduleGroup(group, RandomDurationBetween(min, max));
404 }
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

References RandomDurationBetween(), and RescheduleGroup().

+ Here is the call graph for this function:

◆ Schedule() [1/4]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::Schedule ( std::chrono::duration< _Rep, _Period > const &  time,
group_t const  group,
task_handler_t const &  task 
)
inline

Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!

291 {
292 return ScheduleAt(_now, MakeDurationCalculator(time), group, task);
293 }
TaskScheduler & ScheduleAt(timepoint_t const &end, duration_calculator_t &&duration_calculator, task_handler_t const &task)
Definition: TaskScheduler.hpp:410
static duration_calculator_t MakeDurationCalculator(std::chrono::duration< _Rep, _Period > const &duration)
Definition: TaskScheduler.hpp:70

References _now, MakeDurationCalculator(), and ScheduleAt().

+ Here is the call graph for this function:

◆ Schedule() [2/4]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::Schedule ( std::chrono::duration< _Rep, _Period > const &  time,
task_handler_t const &  task 
)
inline

Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!

280 {
281 return ScheduleAt(_now, MakeDurationCalculator(time), task);
282 }

References _now, MakeDurationCalculator(), and ScheduleAt().

Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Zone::ZoneKernel::initialize(), Horizon::Auth::AuthServer::initialize(), Horizon::Char::CharServer::initialize(), and Horizon::Zone::ScriptManager::initialize_basic_state().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Schedule() [3/4]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & 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 
)
inline

Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!

310 {
311 return ScheduleAt(_now, MakeDurationCalculator(min, max), group, task);
312 }

References _now, MakeDurationCalculator(), and ScheduleAt().

+ Here is the call graph for this function:

◆ Schedule() [4/4]

template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight >
TaskScheduler & TaskScheduler::Schedule ( std::chrono::duration< _RepLeft, _PeriodLeft > const &  min,
std::chrono::duration< _RepRight, _PeriodRight > const &  max,
task_handler_t const &  task 
)
inline

Schedule an event with a randomized rate between min and max rate. Never call this from within a task context! Use TaskContext::Schedule instead!

300 {
301 return ScheduleAt(_now, MakeDurationCalculator(min, max), task);
302 }

References _now, MakeDurationCalculator(), and ScheduleAt().

+ Here is the call graph for this function:

◆ ScheduleAt() [1/2]

TaskScheduler & TaskScheduler::ScheduleAt ( timepoint_t const &  end,
duration_calculator_t &&  duration_calculator,
group_t const  group,
task_handler_t const &  task 
)
inlineprivate

Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::schedule instead!

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 }
unsigned int repeated_t
Definition: TaskScheduler.hpp:90
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:67

References InsertTask().

+ Here is the call graph for this function:

◆ ScheduleAt() [2/2]

TaskScheduler & TaskScheduler::ScheduleAt ( timepoint_t const &  end,
duration_calculator_t &&  duration_calculator,
task_handler_t const &  task 
)
inlineprivate
412 {
413 return InsertTask(TaskContainer(new Task(end + duration_calculator(), std::move(duration_calculator), task)));
414 }

References InsertTask().

Referenced by Schedule(), and TaskContext::Schedule().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetValidator()

template<typename P >
TaskScheduler & TaskScheduler::SetValidator ( P &&  predicate)
inline

Sets a Validator which is asked if tasks are allowed to be executed.

244 {
245 _predicate = std::forward<P>(predicate);
246 return *this;
247 }

References _predicate.

◆ Update() [1/3]

TaskScheduler & TaskScheduler::Update ( size_t const  microseconds,
success_t const &  callback = EmptyCallback 
)

Update the scheduler with a difftime in ms. Calls the optional callback on successfully finish.

33{
34 return Update(std::chrono::microseconds(microseconds), callback);
35}
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

References Update().

+ Here is the call graph for this function:

◆ Update() [2/3]

template<typename _Rep , typename _Period >
TaskScheduler & TaskScheduler::Update ( std::chrono::duration< _Rep, _Period > const &  difftime,
success_t const &  callback = EmptyCallback 
)
inline

Update the scheduler with a difftime. Calls the optional callback on successfully finish.

265 {
266 _now += difftime;
267 Dispatch(callback);
268 return *this;
269 }
void Dispatch(success_t const &callback)
Dispatch remaining tasks when the given condition fits.
Definition: TaskScheduler.cpp:78

References _now, and Dispatch().

+ Here is the call graph for this function:

◆ Update() [3/3]

TaskScheduler & TaskScheduler::Update ( success_t const &  callback = EmptyCallback)

Update the scheduler to the current time. Calls the optional callback on successfully finish.

26{
27 _now = clock_t::now();
28 Dispatch(callback);
29 return *this;
30}

References _now, and Dispatch().

Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Zone::ZoneServer::update(), Update(), Horizon::Zone::GameLogicProcess::update(), Horizon::Zone::ScriptManager::update(), Horizon::Auth::AuthServer::update(), and Horizon::Char::CharServer::update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ TaskContext

friend class TaskContext
friend

Member Data Documentation

◆ _asyncHolder

AsyncHolder TaskScheduler::_asyncHolder
private

Contains all asynchronous tasks which will be invoked at the next update tick.

Referenced by Async(), CancelAll(), and Dispatch().

◆ _now

timepoint_t TaskScheduler::_now
private

The current time point (now)

Referenced by Dispatch(), RescheduleAll(), RescheduleGroup(), Schedule(), and Update().

◆ _predicate

predicate_t TaskScheduler::_predicate
private

◆ _task_holder

TaskQueue TaskScheduler::_task_holder
private

The Task Queue which contains all task objects.

Referenced by CancelAll(), CancelGroup(), Count(), DelayAll(), DelayGroup(), Dispatch(), InsertTask(), and RescheduleAtWithPredicate().

◆ self_reference

std::shared_ptr<TaskScheduler> TaskScheduler::self_reference
private

Contains a self reference to track if this object was deleted or not.

Referenced by Dispatch().


The documentation for this class was generated from the following files: