TrinityCore
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
 
 ~TaskScheduler ()=default
 
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=nullptr)
 
TaskSchedulerUpdate (size_t milliseconds, success_t const &callback=nullptr)
 
template<class Rep , class Period >
TaskSchedulerUpdate (std::chrono::duration< Rep, Period > difftime, success_t const &callback=nullptr)
 
TaskSchedulerAsync (std::function< void()> callable)
 
template<class Rep , class Period >
TaskSchedulerSchedule (std::chrono::duration< Rep, Period > time, task_handler_t task)
 
template<class Rep , class Period >
TaskSchedulerSchedule (std::chrono::duration< Rep, Period > time, group_t const group, task_handler_t task)
 
template<class RepLeft , class PeriodLeft , class RepRight , class PeriodRight >
TaskSchedulerSchedule (std::chrono::duration< RepLeft, PeriodLeft > min, std::chrono::duration< RepRight, PeriodRight > max, task_handler_t task)
 
template<class RepLeft , class PeriodLeft , class RepRight , class PeriodRight >
TaskSchedulerSchedule (std::chrono::duration< RepLeft, PeriodLeft > min, std::chrono::duration< RepRight, PeriodRight > max, group_t const group, task_handler_t task)
 
TaskSchedulerCancelAll ()
 
TaskSchedulerCancelGroup (group_t group)
 
TaskSchedulerCancelGroupsOf (std::vector< group_t > const &groups)
 
template<class Rep , class Period >
TaskSchedulerDelayAll (std::chrono::duration< Rep, Period > duration)
 Delays all tasks with the given duration. More...
 
template<class RepLeft , class PeriodLeft , class RepRight , class PeriodRight >
TaskSchedulerDelayAll (std::chrono::duration< RepLeft, PeriodLeft > min, std::chrono::duration< RepRight, PeriodRight > 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 > 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 > min, std::chrono::duration< RepRight, PeriodRight > 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 > duration)
 Reschedule all tasks with a given duration. More...
 
template<class RepLeft , class PeriodLeft , class RepRight , class PeriodRight >
TaskSchedulerRescheduleAll (std::chrono::duration< RepLeft, PeriodLeft > min, std::chrono::duration< RepRight, PeriodRight > 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 > 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 > min, std::chrono::duration< RepRight, PeriodRight > 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 end, std::chrono::duration< Rep, Period > time, task_handler_t task)
 
template<class Rep , class Period >
TaskSchedulerScheduleAt (timepoint_t end, std::chrono::duration< Rep, Period > time, group_t const group, task_handler_t task)
 
void Dispatch (success_t const &callback)
 Dispatch remaining tasks. More...
 

Static Private Member Functions

static bool EmptyValidator ()
 

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

Definition at line 47 of file TaskScheduler.h.

Member Typedef Documentation

◆ AsyncHolder

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

Definition at line 160 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 118 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 174 of file TaskScheduler.h.

◆ TaskScheduler() [2/4]

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

Definition at line 178 of file TaskScheduler.h.

◆ TaskScheduler() [3/4]

TaskScheduler::TaskScheduler ( TaskScheduler const &  )
delete

◆ TaskScheduler() [4/4]

TaskScheduler::TaskScheduler ( TaskScheduler &&  )
delete

◆ ~TaskScheduler()

TaskScheduler::~TaskScheduler ( )
default

Member Function Documentation

◆ Async()

TaskScheduler & TaskScheduler::Async ( std::function< void()>  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  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:
+ Here is the caller graph for this function:

◆ DelayAll() [1/2]

template<class Rep , class Period >
TaskScheduler & TaskScheduler::DelayAll ( std::chrono::duration< Rep, Period 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 >  min,
std::chrono::duration< RepRight, PeriodRight >  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 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:
+ Here is the caller 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 >  min,
std::chrono::duration< RepRight, PeriodRight >  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:

◆ EmptyValidator()

static bool TaskScheduler::EmptyValidator ( )
inlinestaticprivate

Definition at line 168 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 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 >  min,
std::chrono::duration< RepRight, PeriodRight >  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 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:
+ Here is the caller 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 >  min,
std::chrono::duration< RepRight, PeriodRight >  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 time,
group_t const  group,
task_handler_t  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 time,
task_handler_t  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 >  min,
std::chrono::duration< RepRight, PeriodRight >  max,
group_t const  group,
task_handler_t  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 >  min,
std::chrono::duration< RepRight, PeriodRight >  max,
task_handler_t  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  end,
std::chrono::duration< Rep, Period time,
group_t const  group,
task_handler_t  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  end,
std::chrono::duration< Rep, Period time,
task_handler_t  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  milliseconds,
success_t const &  callback = nullptr 
)

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 difftime,
success_t const &  callback = nullptr 
)
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 = nullptr)

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 Function 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 164 of file TaskScheduler.h.

◆ _now

timepoint_t TaskScheduler::_now
private

The current time point (now)

Definition at line 155 of file TaskScheduler.h.

◆ _predicate

predicate_t TaskScheduler::_predicate
private

Definition at line 166 of file TaskScheduler.h.

◆ _task_holder

TaskQueue TaskScheduler::_task_holder
private

The Task Queue which contains all task objects.

Definition at line 158 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 152 of file TaskScheduler.h.


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