TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TaskScheduler Class Reference

#include <TaskScheduler.h>

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)
 
TaskSchedulerUpdate (size_t const milliseconds, success_t const &callback=EmptyCallback)
 
template<class _Rep , class _Period >
TaskSchedulerUpdate (std::chrono::duration< _Rep, _Period > const &difftime, success_t const &callback=EmptyCallback)
 
TaskSchedulerAsync (std::function< void()> const &callable)
 
template<class _Rep , class _Period >
TaskSchedulerSchedule (std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
 
template<class _Rep , class _Period >
TaskSchedulerSchedule (std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task)
 
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _PeriodRight >
TaskSchedulerSchedule (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, task_handler_t const &task)
 
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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)
 
TaskSchedulerCancelAll ()
 
TaskSchedulerCancelGroup (group_t const group)
 
TaskSchedulerCancelGroupsOf (std::vector< group_t > const &groups)
 
template<class _Rep , class _Period >
TaskSchedulerDelayAll (std::chrono::duration< _Rep, _Period > const &duration)
 Delays all tasks with the given duration. More...
 
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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<class _Rep , class _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<class _RepLeft , class _PeriodLeft , class _RepRight , class _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<class _Rep , class _Period >
TaskSchedulerRescheduleAll (std::chrono::duration< _Rep, _Period > const &duration)
 Reschedule all tasks with a given duration. More...
 
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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<class _Rep , class _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<class _RepLeft , class _PeriodLeft , class _RepRight , class _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::steady_clock clock_t
 
typedef clock_t::time_point timepoint_t
 
typedef clock_t::duration duration_t
 
typedef uint32 group_t
 
typedef uint32 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...
 
template<class _Rep , class _Period >
TaskSchedulerScheduleAt (timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
 
template<class _Rep , class _Period >
TaskSchedulerScheduleAt (timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task)
 
void Dispatch (success_t const &callback)
 Dispatch remaining tasks. More...
 

Static Private Member Functions

static bool EmptyValidator ()
 
static void EmptyCallback ()
 
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _PeriodRight >
static std::chrono::milliseconds 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
 
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 dialogs for example).

Member Typedef Documentation

typedef std::queue<std::function<void()> > TaskScheduler::AsyncHolder
private
typedef std::chrono::steady_clock TaskScheduler::clock_t
private
typedef clock_t::duration TaskScheduler::duration_t
private
typedef uint32 TaskScheduler::group_t
private
typedef std::function<bool()> TaskScheduler::predicate_t
private
typedef std::function<void()> TaskScheduler::success_t
private
typedef std::function<void(TaskContext)> TaskScheduler::task_handler_t
private
typedef std::shared_ptr<Task> TaskScheduler::TaskContainer
private
typedef clock_t::time_point TaskScheduler::timepoint_t
private

Constructor & Destructor Documentation

TaskScheduler::TaskScheduler ( )
inline
184  : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { }
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.h:157
Definition: TaskScheduler.h:49
static bool EmptyValidator()
Definition: TaskScheduler.h:173
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
predicate_t _predicate
Definition: TaskScheduler.h:171
template<typename P >
TaskScheduler::TaskScheduler ( P &&  predicate)
inline
188  : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.h:157
Definition: TaskScheduler.h:49
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
predicate_t _predicate
Definition: TaskScheduler.h:171
TaskScheduler::TaskScheduler ( TaskScheduler const )
delete
TaskScheduler::TaskScheduler ( TaskScheduler &&  )
delete

Member Function Documentation

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.

39 {
40  _asyncHolder.push(callable);
41  return *this;
42 }
AsyncHolder _asyncHolder
Definition: TaskScheduler.h:169

+ Here is the caller graph for this function:

TaskScheduler & TaskScheduler::CancelAll ( )

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

Clear the task holder

45 {
49  return *this;
50 }
std::queue< std::function< void()> > AsyncHolder
Definition: TaskScheduler.h:165
AsyncHolder _asyncHolder
Definition: TaskScheduler.h:169
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void Clear()
Definition: TaskScheduler.cpp:130

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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!

53 {
54  _task_holder.RemoveIf([group](TaskContainer const& task) -> bool
55  {
56  return task->IsInGroup(group);
57  });
58  return *this;
59 }
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void RemoveIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:135
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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}"

62 {
63  std::for_each(groups.begin(), groups.end(),
64  std::bind(&TaskScheduler::CancelGroup, this, std::placeholders::_1));
65 
66  return *this;
67 }
TaskScheduler & CancelGroup(group_t const group)
Definition: TaskScheduler.cpp:52

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

TaskScheduler & TaskScheduler::ClearValidator ( )

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

21 {
23  return *this;
24 }
static bool EmptyValidator()
Definition: TaskScheduler.h:173
predicate_t _predicate
Definition: TaskScheduler.h:171

+ Here is the call graph for this function:

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

Delays all tasks with the given duration.

281  {
282  _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool
283  {
284  task->_end += duration;
285  return true;
286  });
287  return *this;
288  }
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:144
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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.

294  {
296  }
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
TaskScheduler & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration.
Definition: TaskScheduler.h:280
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
template<class _Rep , class _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.

301  {
302  _task_holder.ModifyIf([&duration, group](TaskContainer const& task) -> bool
303  {
304  if (task->IsInGroup(group))
305  {
306  task->_end += duration;
307  return true;
308  }
309  else
310  return false;
311  });
312  return *this;
313  }
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:144
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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.

320  {
321  return DelayGroup(group, RandomDurationBetween(min, max));
322  }
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.h:300
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
void TaskScheduler::Dispatch ( success_t const callback)
private

Dispatch remaining tasks.

76 {
77  // If the validation failed abort the dispatching here.
78  if (!_predicate())
79  return;
80 
81  // Process all asyncs
82  while (!_asyncHolder.empty())
83  {
84  _asyncHolder.front()();
85  _asyncHolder.pop();
86 
87  // If the validation failed abort the dispatching here.
88  if (!_predicate())
89  return;
90  }
91 
92  while (!_task_holder.IsEmpty())
93  {
94  if (_task_holder.First()->_end > _now)
95  break;
96 
97  // Perfect forward the context to the handler
98  // Use weak references to catch destruction before callbacks.
99  TaskContext context(_task_holder.Pop(), std::weak_ptr<TaskScheduler>(self_reference));
100 
101  // Invoke the context
102  context.Invoke();
103 
104  // If the validation failed abort the dispatching here.
105  if (!_predicate())
106  return;
107  }
108 
109  // On finish call the final callback
110  callback();
111 }
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.h:157
AsyncHolder _asyncHolder
Definition: TaskScheduler.h:169
TaskContainer const & First() const
Definition: TaskScheduler.cpp:125
TaskContainer Pop()
Pops the task out of the container.
Definition: TaskScheduler.cpp:118
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
bool IsEmpty() const
Definition: TaskScheduler.cpp:159
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
predicate_t _predicate
Definition: TaskScheduler.h:171
Definition: TaskScheduler.h:411

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void TaskScheduler::EmptyCallback ( )
inlinestaticprivate
179  {
180  }
static bool TaskScheduler::EmptyValidator ( )
inlinestaticprivate
174  {
175  return true;
176  }

+ Here is the caller graph for this function:

TaskScheduler & TaskScheduler::InsertTask ( TaskContainer  task)
private

Insert a new task to the enqueued tasks.

70 {
71  _task_holder.Push(std::move(task));
72  return *this;
73 }
void Push(TaskContainer &&task)
Definition: TaskScheduler.cpp:113
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

TaskScheduler& TaskScheduler::operator= ( TaskScheduler const )
delete
TaskScheduler& TaskScheduler::operator= ( TaskScheduler &&  )
delete
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _PeriodRight >
static std::chrono::milliseconds TaskScheduler::RandomDurationBetween ( std::chrono::duration< _RepLeft, _PeriodLeft > const min,
std::chrono::duration< _RepRight, _PeriodRight > const max 
)
inlinestaticprivate
399  {
400  auto const milli_min = std::chrono::duration_cast<std::chrono::milliseconds>(min);
401  auto const milli_max = std::chrono::duration_cast<std::chrono::milliseconds>(max);
402 
403  // TC specific: use SFMT URandom
404  return std::chrono::milliseconds(urand(uint32(milli_min.count()), uint32(milli_max.count())));
405  }
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:45
float milliseconds()
Definition: units.h:92
uint32_t uint32
Definition: g3dmath.h:168

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Reschedule all tasks with a given duration.

327  {
328  auto const end = _now + duration;
329  _task_holder.ModifyIf([end](TaskContainer const& task) -> bool
330  {
331  task->_end = end;
332  return true;
333  });
334  return *this;
335  }
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:144
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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.

341  {
343  }
TaskScheduler & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with a given duration.
Definition: TaskScheduler.h:326
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
template<class _Rep , class _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.

348  {
349  auto const end = _now + duration;
350  _task_holder.ModifyIf([end, group](TaskContainer const& task) -> bool
351  {
352  if (task->IsInGroup(group))
353  {
354  task->_end = end;
355  return true;
356  }
357  else
358  return false;
359  });
360  return *this;
361  }
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:144
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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.

368  {
369  return RescheduleGroup(group, RandomDurationBetween(min, max));
370  }
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.h:347
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
template<class _Rep , class _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!

234  {
235  return ScheduleAt(_now, time, task);
236  }
TaskScheduler & ScheduleAt(timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:377
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160

+ Here is the caller graph for this function:

template<class _Rep , class _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!

243  {
244  return ScheduleAt(_now, time, group, task);
245  }
TaskScheduler & ScheduleAt(timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:377
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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!

252  {
253  return Schedule(RandomDurationBetween(min, max), task);
254  }
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:232
template<class _RepLeft , class _PeriodLeft , class _RepRight , class _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!

262  {
263  return Schedule(RandomDurationBetween(min, max), group, task);
264  }
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:397
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:232
template<class _Rep , class _Period >
TaskScheduler& TaskScheduler::ScheduleAt ( timepoint_t const end,
std::chrono::duration< _Rep, _Period > const time,
task_handler_t const task 
)
inlineprivate
379  {
380  return InsertTask(TaskContainer(new Task(end + time, time, task)));
381  }
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:69
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123

+ Here is the caller graph for this function:

template<class _Rep , class _Period >
TaskScheduler& TaskScheduler::ScheduleAt ( timepoint_t const end,
std::chrono::duration< _Rep, _Period > const time,
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!

389  {
390  static repeated_t const DEFAULT_REPEATED = 0;
391  return InsertTask(TaskContainer(new Task(end + time, time, group, DEFAULT_REPEATED, task)));
392  }
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:69
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123
uint32 repeated_t
Definition: TaskScheduler.h:61
template<typename P >
TaskScheduler& TaskScheduler::SetValidator ( P &&  predicate)
inline

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

198  {
199  _predicate = std::forward<P>(predicate);
200  return *this;
201  }
predicate_t _predicate
Definition: TaskScheduler.h:171

+ Here is the caller graph for this function:

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

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

27 {
28  _now = clock_t::now();
29  Dispatch(callback);
30  return *this;
31 }
void Dispatch(success_t const &callback)
Dispatch remaining tasks.
Definition: TaskScheduler.cpp:75
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

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

34 {
36 }
TaskScheduler & Update(success_t const &callback=EmptyCallback)
Definition: TaskScheduler.cpp:26
float milliseconds()
Definition: units.h:92

+ Here is the call graph for this function:

template<class _Rep , class _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.

219  {
220  _now += difftime;
221  Dispatch(callback);
222  return *this;
223  }
void Dispatch(success_t const &callback)
Dispatch remaining tasks.
Definition: TaskScheduler.cpp:75
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160

Friends And Related Function Documentation

friend class TaskContext
friend

Member Data Documentation

AsyncHolder TaskScheduler::_asyncHolder
private

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

timepoint_t TaskScheduler::_now
private

The current time point (now)

predicate_t TaskScheduler::_predicate
private
TaskQueue TaskScheduler::_task_holder
private

The Task Queue which contains all task objects.

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

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


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