TrinityCore
Loading...
Searching...
No Matches
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.
 
TaskSchedulerClearValidator ()
 Clears the validator which is asked if tasks are allowed to be executed.
 
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.
 
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.
 
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.
 
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.
 
template<class _Rep , class _Period >
TaskSchedulerRescheduleAll (std::chrono::duration< _Rep, _Period > const &duration)
 Reschedule all tasks with a given duration.
 
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.
 
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.
 
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.
 

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.
 
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.
 

Static Private Member Functions

static bool EmptyValidator ()
 
static void EmptyCallback ()
 

Private Attributes

std::shared_ptr< TaskSchedulerself_reference
 Contains a self reference to track if this object was deleted or not.
 
timepoint_t _now
 The current time point (now)
 
TaskQueue _task_holder
 The Task Queue which contains all task objects.
 
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).

Definition at line 47 of file TaskScheduler.h.

Member Typedef Documentation

◆ AsyncHolder

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

Definition at line 158 of file TaskScheduler.h.

◆ clock_t

typedef std::chrono::steady_clock TaskScheduler::clock_t
private

Definition at line 52 of file TaskScheduler.h.

◆ duration_t

typedef clock_t::duration TaskScheduler::duration_t
private

Definition at line 54 of file TaskScheduler.h.

◆ group_t

typedef uint32 TaskScheduler::group_t
private

Definition at line 57 of file TaskScheduler.h.

◆ predicate_t

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

Definition at line 63 of file TaskScheduler.h.

◆ repeated_t

Definition at line 59 of file TaskScheduler.h.

◆ success_t

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

Definition at line 65 of file TaskScheduler.h.

◆ task_handler_t

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

Definition at line 61 of file TaskScheduler.h.

◆ TaskContainer

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

Definition at line 116 of file TaskScheduler.h.

◆ timepoint_t

typedef clock_t::time_point TaskScheduler::timepoint_t
private

Definition at line 53 of file TaskScheduler.h.

Constructor & Destructor Documentation

◆ TaskScheduler() [1/4]

TaskScheduler::TaskScheduler ( )
inline

Definition at line 176 of file TaskScheduler.h.

◆ TaskScheduler() [2/4]

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

Definition at line 180 of file TaskScheduler.h.

◆ TaskScheduler() [3/4]

TaskScheduler::TaskScheduler ( TaskScheduler const &  )
delete

◆ TaskScheduler() [4/4]

TaskScheduler::TaskScheduler ( TaskScheduler &&  )
delete

Member Function Documentation

◆ 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.

Definition at line 39 of file TaskScheduler.cpp.

+ 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

Definition at line 45 of file TaskScheduler.cpp.

+ 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!

Definition at line 53 of file TaskScheduler.cpp.

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

Definition at line 62 of file TaskScheduler.cpp.

+ 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.

Definition at line 21 of file TaskScheduler.cpp.

+ Here is the call graph for this function:

◆ DelayAll() [1/2]

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

Delays all tasks with the given duration.

Definition at line 273 of file TaskScheduler.h.

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

◆ DelayAll() [2/2]

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.

Definition at line 285 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ DelayGroup() [1/2]

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.

Definition at line 293 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ DelayGroup() [2/2]

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.

Definition at line 310 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ Dispatch()

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

Dispatch remaining tasks.

Definition at line 76 of file TaskScheduler.cpp.

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

◆ EmptyCallback()

static void TaskScheduler::EmptyCallback ( )
inlinestaticprivate

Definition at line 171 of file TaskScheduler.h.

◆ EmptyValidator()

static bool TaskScheduler::EmptyValidator ( )
inlinestaticprivate

Definition at line 166 of file TaskScheduler.h.

+ Here is the caller graph for this function:

◆ InsertTask()

TaskScheduler & TaskScheduler::InsertTask ( TaskContainer  task)
private

Insert a new task to the enqueued tasks.

Definition at line 70 of file TaskScheduler.cpp.

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ RescheduleAll() [1/2]

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

Reschedule all tasks with a given duration.

Definition at line 319 of file TaskScheduler.h.

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

◆ RescheduleAll() [2/2]

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.

Definition at line 332 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ RescheduleGroup() [1/2]

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.

Definition at line 340 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ RescheduleGroup() [2/2]

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.

Definition at line 358 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ Schedule() [1/4]

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!

Definition at line 234 of file TaskScheduler.h.

◆ Schedule() [2/4]

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!

Definition at line 225 of file TaskScheduler.h.

◆ Schedule() [3/4]

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!

Definition at line 252 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ Schedule() [4/4]

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!

Definition at line 243 of file TaskScheduler.h.

+ Here is the call graph for this function:

◆ ScheduleAt() [1/2]

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!

Definition at line 379 of file TaskScheduler.h.

◆ ScheduleAt() [2/2]

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

Definition at line 370 of file TaskScheduler.h.

+ 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.

Definition at line 190 of file TaskScheduler.h.

+ Here is the caller graph for this function:

◆ Update() [1/3]

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.

Definition at line 34 of file TaskScheduler.cpp.

+ Here is the call graph for this function:

◆ Update() [2/3]

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.

Definition at line 210 of file TaskScheduler.h.

◆ 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.

Definition at line 27 of file TaskScheduler.cpp.

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ TaskContext

friend class TaskContext
friend

Definition at line 49 of file TaskScheduler.h.

Member Data Documentation

◆ _asyncHolder

AsyncHolder TaskScheduler::_asyncHolder
private

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

Definition at line 162 of file TaskScheduler.h.

◆ _now

timepoint_t TaskScheduler::_now
private

The current time point (now)

Definition at line 153 of file TaskScheduler.h.

◆ _predicate

predicate_t TaskScheduler::_predicate
private

Definition at line 164 of file TaskScheduler.h.

◆ _task_holder

TaskQueue TaskScheduler::_task_holder
private

The Task Queue which contains all task objects.

Definition at line 156 of file TaskScheduler.h.

◆ self_reference

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

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

Definition at line 150 of file TaskScheduler.h.


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