AzerothCore 3.3.5a
OpenSource WoW Emulator
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 ()
 
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.
 
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).

Member Typedef Documentation

◆ AsyncHolder

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

◆ clock_t

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

◆ duration_t

typedef clock_t::duration TaskScheduler::duration_t
private

◆ group_t

typedef uint32 TaskScheduler::group_t
private

◆ predicate_t

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

◆ repeated_t

◆ 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
170 : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { }
Definition: TaskScheduler.h:36
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.h:143
predicate_t _predicate
Definition: TaskScheduler.h:157
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:146
static bool EmptyValidator()
Definition: TaskScheduler.h:159

◆ TaskScheduler() [2/4]

template<typename P >
TaskScheduler::TaskScheduler ( P &&  predicate)
inline
173 : self_reference(this, [](TaskScheduler const*) { }), _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

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

28{
29 _asyncHolder.push(callable);
30 return *this;
31}
AsyncHolder _asyncHolder
Definition: TaskScheduler.h:155

References _asyncHolder.

Referenced by TaskContext::Async().

◆ CancelAll()

TaskScheduler & TaskScheduler::CancelAll ( )

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

Clear the task holder

34{
38 return *this;
39}
std::queue< std::function< void()> > AsyncHolder
Definition: TaskScheduler.h:151
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:149
void Clear()
Definition: TaskScheduler.cpp:127

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

Referenced by BossAI::_JustDied(), BossAI::_Reset(), TaskContext::CancelAll(), instance_the_black_morass::instance_the_black_morass_InstanceMapScript::CleanupInstance(), boss_bug_trio::DamageTaken(), npc_drakonid_spawner::DoAction(), boss_twinemperorsAI::EnterEvadeMode(), boss_glob_of_viscidus::InitializeAI(), boss_thekal::boss_thekalAI::JustEngagedWith(), npc_anubisath_guardian::JustEngagedWith(), npc_ahnqiraji_critter::JustEngagedWith(), boss_high_botanist_freywinn::JustEngagedWith(), boss_jindo::JustEngagedWith(), boss_fankriss::JustEngagedWith(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::OnUnitDeath(), boss_mor_grayhoof::Reset(), npc_corrupted_totem::Reset(), npc_lava_spawn::Reset(), boss_kormok::Reset(), npc_apothecary_baxter::Reset(), boss_baroness_anastari::boss_baroness_anastariAI::Reset(), boss_jarien::Reset(), boss_sothos::Reset(), boss_twilight_corrupter::Reset(), npc_batrider::Reset(), npc_healing_ward::Reset(), npc_shade_of_jindo::Reset(), npc_ohgan::npc_ohganAI::Reset(), npc_vilebranch_speaker::Reset(), npc_spawn_of_marli::Reset(), npc_zealot_lorkhan::npc_zealot_lorkhanAI::Reset(), npc_zealot_zath::npc_zealot_zathAI::Reset(), boss_azuregos::boss_azuregosAI::Reset(), boss_isalien::Reset(), boss_ayamiss::Reset(), npc_hivezara_stinger::Reset(), npc_obsidian_destroyer::Reset(), boss_bug_trio::Reset(), boss_eye_of_cthun::Reset(), boss_cthun::Reset(), boss_fankriss::Reset(), boss_ouro::Reset(), boss_viscidus::Reset(), npc_anubisath_defender::Reset(), npc_vekniss_stinger::Reset(), npc_obsidian_eradicator::Reset(), npc_anubisath_warder::Reset(), npc_obsidian_nullifier::Reset(), npc_ahnqiraji_critter::Reset(), npc_steamrigger_mechanic::Reset(), boss_hungarfen::Reset(), npc_raliq_the_drunk::npc_raliq_the_drunkAI::Reset(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::ResetRajaxxWaves(), npc_dream_fog::npc_dream_fogAI::ScheduleEvents(), World::ShutdownServ(), boss_attumen::boss_attumenAI::SpellHit(), and boss_cthun::SummonedCreatureDies().

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

42{
43 _task_holder.RemoveIf([group](TaskContainer const & task) -> bool
44 {
45 return task->IsInGroup(group);
46 });
47 return *this;
48}
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:109
void RemoveIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:132

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

Referenced by TaskContext::CancelGroup(), CancelGroupsOf(), boss_mor_grayhoof::DamageTaken(), boss_malchezaar::DamageTaken(), boss_ayamiss::DamageTaken(), boss_ouro::DamageTaken(), boss_twinemperorsAI::DoAction(), instance_the_black_morass::instance_the_black_morass_InstanceMapScript::ScheduleNextPortal(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::SetData(), and boss_ouro::Submerge().

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

51{
52 std::for_each(groups.begin(), groups.end(),
53 std::bind(&TaskScheduler::CancelGroup, this, std::placeholders::_1));
54
55 return *this;
56}
TaskScheduler & CancelGroup(group_t const group)
Definition: TaskScheduler.cpp:41

References CancelGroup().

Referenced by TaskContext::CancelGroupsOf().

◆ ClearValidator()

TaskScheduler & TaskScheduler::ClearValidator ( )

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

10{
12 return *this;
13}

References _predicate, and EmptyValidator().

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

266 {
267 _task_holder.ModifyIf([&duration](TaskContainer const & task) -> bool
268 {
269 task->_end += duration;
270 return true;
271 });
272 return *this;
273 }
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:145

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

Referenced by boss_hungarfen::DamageTaken(), DelayAll(), boss_bug_trio::DoAction(), and boss_twinemperorsAI::DoAction().

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

279 {
280 return DelayAll(RandomDurationBetween(min, max));
281 }
TaskScheduler & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration.
Definition: TaskScheduler.h:265
static std::chrono::milliseconds RandomDurationBetween(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:385

References DelayAll(), and RandomDurationBetween().

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

286 {
287 _task_holder.ModifyIf([&duration, group](TaskContainer const & task) -> bool
288 {
289 if (task->IsInGroup(group))
290 {
291 task->_end += duration;
292 return true;
293 }
294 else
295 {
296 return false;
297 }
298 });
299 return *this;
300 }

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

Referenced by boss_mor_grayhoof::DamageTaken(), DelayGroup(), and boss_malchezaar::JustEngagedWith().

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

307 {
308 return DelayGroup(group, RandomDurationBetween(min, max));
309 }
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:285

References DelayGroup(), and RandomDurationBetween().

◆ Dispatch()

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

Dispatch remaining tasks.

65{
66 // If the validation failed abort the dispatching here.
67 if (!_predicate())
68 {
69 return;
70 }
71
72 // Process all asyncs
73 while (!_asyncHolder.empty())
74 {
75 _asyncHolder.front()();
76 _asyncHolder.pop();
77
78 // If the validation failed abort the dispatching here.
79 if (!_predicate())
80 {
81 return;
82 }
83 }
84
85 while (!_task_holder.IsEmpty())
86 {
87 if (_task_holder.First()->_end > _now)
88 {
89 break;
90 }
91
92 // Perfect forward the context to the handler
93 // Use weak references to catch destruction before callbacks.
94 TaskContext context(_task_holder.Pop(), std::weak_ptr<TaskScheduler>(self_reference));
95
96 // Invoke the context
97 context.Invoke();
98
99 // If the validation failed abort the dispatching here.
100 if (!_predicate())
101 {
102 return;
103 }
104 }
105
106 // On finish call the final callback
107 callback();
108}
bool IsEmpty() const
Definition: TaskScheduler.cpp:162
TaskContainer Pop()
Pops the task out of the container.
Definition: TaskScheduler.cpp:115
TaskContainer const & First() const
Definition: TaskScheduler.cpp:122
Definition: TaskScheduler.h:400

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

◆ EmptyCallback()

static void TaskScheduler::EmptyCallback ( )
inlinestaticprivate
165 {
166 }

◆ EmptyValidator()

static bool TaskScheduler::EmptyValidator ( )
inlinestaticprivate
160 {
161 return true;
162 }

Referenced by ClearValidator().

◆ InsertTask()

TaskScheduler & TaskScheduler::InsertTask ( TaskContainer  task)
private

Insert a new task to the enqueued tasks.

59{
60 _task_holder.Push(std::move(task));
61 return *this;
62}
void Push(TaskContainer &&task)
Definition: TaskScheduler.cpp:110

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

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ RandomDurationBetween()

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
387 {
388 auto const milli_min = std::chrono::duration_cast<std::chrono::milliseconds>(min);
389 auto const milli_max = std::chrono::duration_cast<std::chrono::milliseconds>(max);
390
391 // TC specific: use SFMT URandom
392 return std::chrono::milliseconds(urand(uint32(milli_min.count()), uint32(milli_max.count())));
393 }
std::uint32_t uint32
Definition: Define.h:108
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:44

References urand().

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

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

314 {
315 auto const end = _now + duration;
316 _task_holder.ModifyIf([end](TaskContainer const & task) -> bool
317 {
318 task->_end = end;
319 return true;
320 });
321 return *this;
322 }

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

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

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

328 {
329 return RescheduleAll(RandomDurationBetween(min, max));
330 }
TaskScheduler & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with a given duration.
Definition: TaskScheduler.h:313

References RandomDurationBetween(), and RescheduleAll().

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

335 {
336 auto const end = _now + duration;
337 _task_holder.ModifyIf([end, group](TaskContainer const & task) -> bool
338 {
339 if (task->IsInGroup(group))
340 {
341 task->_end = end;
342 return true;
343 }
344 else
345 {
346 return false;
347 }
348 });
349 return *this;
350 }

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

Referenced by RescheduleGroup().

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

357 {
358 return RescheduleGroup(group, RandomDurationBetween(min, max));
359 }
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:334

References RandomDurationBetween(), and RescheduleGroup().

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

228 {
229 return ScheduleAt(_now, time, group, task);
230 }
TaskScheduler & ScheduleAt(timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:366

References _now, and ScheduleAt().

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

219 {
220 return ScheduleAt(_now, time, task);
221 }

References _now, and ScheduleAt().

Referenced by boss_mor_grayhoof::CastRandomSpell(), boss_thekal::boss_thekalAI::CheckPhaseTransition(), boss_mor_grayhoof::DamageTaken(), boss_isalien::DamageTaken(), boss_malchezaar::DamageTaken(), boss_ouro::DamageTaken(), boss_hungarfen::DamageTaken(), boss_viscidus::DamageTaken(), boss_apothecary_hummel::boss_apothecary_hummelAI::DoAction(), boss_twinemperorsAI::DoAction(), npc_thrall_warchief::npc_thrall_warchiefAI::DoAction(), boss_broggok::DoAction(), boss_ouro::Emerge(), boss_jindo::EnterEvadeMode(), boss_glob_of_viscidus::InitializeAI(), npc_underbog_mushroom::InitializeAI(), npc_corrupted_totem::IsSummonedBy(), boss_kormok::IsSummonedBy(), boss_jarien::IsSummonedBy(), boss_sothos::IsSummonedBy(), npc_drakonid_spawner::IsSummonedBy(), boss_attumen::boss_attumenAI::IsSummonedBy(), boss_mor_grayhoof::JustEngagedWith(), npc_lava_spawn::JustEngagedWith(), boss_malchezaar::JustEngagedWith(), npc_malchezaar_axe::JustEngagedWith(), boss_kormok::JustEngagedWith(), boss_baroness_anastari::boss_baroness_anastariAI::JustEngagedWith(), boss_jarien::JustEngagedWith(), boss_sothos::JustEngagedWith(), boss_twilight_corrupter::JustEngagedWith(), npc_batrider::JustEngagedWith(), npc_vilebranch_speaker::JustEngagedWith(), npc_spawn_of_marli::JustEngagedWith(), boss_thekal::boss_thekalAI::JustEngagedWith(), npc_zealot_lorkhan::npc_zealot_lorkhanAI::JustEngagedWith(), npc_zealot_zath::npc_zealot_zathAI::JustEngagedWith(), npc_hallows_end_soh::JustEngagedWith(), boss_azuregos::boss_azuregosAI::JustEngagedWith(), boss_aeonus::JustEngagedWith(), boss_chrono_lord_deja::JustEngagedWith(), boss_temporus::JustEngagedWith(), boss_isalien::JustEngagedWith(), npc_anubisath_guardian::JustEngagedWith(), npc_obsidian_destroyer::JustEngagedWith(), npc_claw_tentacle::JustEngagedWith(), npc_dirt_mound::JustEngagedWith(), npc_anubisath_defender::JustEngagedWith(), npc_obsidian_eradicator::JustEngagedWith(), npc_anubisath_warder::JustEngagedWith(), npc_obsidian_nullifier::JustEngagedWith(), npc_ahnqiraji_critter::JustEngagedWith(), boss_talon_king_ikiss::JustEngagedWith(), boss_dalliah_the_doomsayer::JustEngagedWith(), boss_zereketh_the_unbound::JustEngagedWith(), boss_commander_sarannis::JustEngagedWith(), boss_high_botanist_freywinn::JustEngagedWith(), boss_laj::JustEngagedWith(), boss_thorngrin_the_tender::JustEngagedWith(), boss_warp_splinter::JustEngagedWith(), npc_raliq_the_drunk::npc_raliq_the_drunkAI::JustEngagedWith(), npc_steamrigger_mechanic::JustEngagedWith(), boss_midnight::boss_midnightAI::JustEngagedWith(), npc_ohgan::npc_ohganAI::JustEngagedWith(), npc_hivezara_stinger::JustEngagedWith(), boss_kri::JustEngagedWith(), boss_vem::JustEngagedWith(), boss_yauj::JustEngagedWith(), boss_fankriss::JustEngagedWith(), boss_twinemperorsAI::JustEngagedWith(), boss_veknilash::JustEngagedWith(), boss_veklor::JustEngagedWith(), npc_vekniss_stinger::JustEngagedWith(), boss_hungarfen::JustEngagedWith(), boss_ayamiss::MovementInform(), boss_bug_trio::MovementInform(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::OnUnitDeath(), instance_temple_of_ahnqiraj::instance_temple_of_ahnqiraj_InstanceMapScript::OnUnitDeath(), npc_apothecary_baxter::Reset(), go_direbrew_mole_machine::go_direbrew_mole_machineAI::Reset(), go_sand_trap::Reset(), npc_eye_tentacle::Reset(), npc_claw_tentacle::Reset(), npc_giant_claw_tentacle::Reset(), npc_giant_eye_tentacle::Reset(), npc_ahnqiraji_critter::Reset(), npc_raliq_the_drunk::npc_raliq_the_drunkAI::Reset(), Schedule(), npc_dream_fog::npc_dream_fogAI::ScheduleEvents(), instance_the_black_morass::instance_the_black_morass_InstanceMapScript::ScheduleNextPortal(), boss_baroness_anastari::boss_baroness_anastariAI::SchedulePossession(), boss_commander_sarannis::ScheduleReinforcements(), boss_eye_of_cthun::ScheduleTask(), npc_giant_claw_tentacle::ScheduleTasks(), boss_attumen::boss_attumenAI::ScheduleTasks(), boss_ayamiss::ScheduleTasks(), boss_cthun::ScheduleTasks(), instance_the_black_morass::instance_the_black_morass_InstanceMapScript::SetBossState(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::SetData(), boss_thekal::boss_thekalAI::SetData(), World::ShutdownServ(), npc_netherspite_infernal::SpellHit(), boss_attumen::boss_attumenAI::SpellHit(), boss_cthun::SummonedCreatureDies(), and npc_drakonid_spawner::SummonedCreatureDies().

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

247 {
248 return Schedule(RandomDurationBetween(min, max), group, task);
249 }
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:217

References RandomDurationBetween(), and Schedule().

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

237 {
238 return Schedule(RandomDurationBetween(min, max), task);
239 }

References RandomDurationBetween(), and Schedule().

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

378 {
379 static repeated_t const DEFAULT_REPEATED = 0;
380 return InsertTask(TaskContainer(new Task(end + time, time, group, DEFAULT_REPEATED, task)));
381 }
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:58
uint32 repeated_t
Definition: TaskScheduler.h:47

References InsertTask().

◆ 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
368 {
369 return InsertTask(TaskContainer(new Task(end + time, time, task)));
370 }

References InsertTask().

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

◆ SetValidator()

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

23{
24 return Update(std::chrono::milliseconds(milliseconds), callback);
25}
TaskScheduler & Update(success_t const &callback=EmptyCallback)
Definition: TaskScheduler.cpp:15

References Update().

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

204 {
205 _now += difftime;
206 Dispatch(callback);
207 return *this;
208 }
void Dispatch(success_t const &callback)
Dispatch remaining tasks.
Definition: TaskScheduler.cpp:64

References _now, and Dispatch().

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

16{
17 _now = clock_t::now();
18 Dispatch(callback);
19 return *this;
20}

References _now, and Dispatch().

Referenced by Update(), World::Update(), instance_ruins_of_ahnqiraj::instance_ruins_of_ahnqiraj_InstanceMapScript::Update(), go_sand_trap::UpdateAI(), BossAI::UpdateAI(), boss_mor_grayhoof::UpdateAI(), npc_drakonid_spawner::UpdateAI(), npc_lava_spawn::UpdateAI(), boss_attumen::boss_attumenAI::UpdateAI(), boss_midnight::boss_midnightAI::UpdateAI(), npc_netherspite_infernal::UpdateAI(), npc_malchezaar_axe::UpdateAI(), boss_kormok::UpdateAI(), boss_apothecary_hummel::boss_apothecary_hummelAI::UpdateAI(), npc_apothecary_baxter::UpdateAI(), boss_baroness_anastari::boss_baroness_anastariAI::UpdateAI(), boss_jarien::UpdateAI(), boss_sothos::UpdateAI(), boss_twilight_corrupter::UpdateAI(), npc_batrider::UpdateAI(), boss_jindo::UpdateAI(), npc_healing_ward::UpdateAI(), npc_shade_of_jindo::UpdateAI(), npc_ohgan::npc_ohganAI::UpdateAI(), npc_vilebranch_speaker::UpdateAI(), npc_spawn_of_marli::UpdateAI(), boss_thekal::boss_thekalAI::UpdateAI(), npc_zealot_lorkhan::npc_zealot_lorkhanAI::UpdateAI(), npc_zealot_zath::npc_zealot_zathAI::UpdateAI(), npc_coren_direbrew_sisters::UpdateAI(), go_direbrew_mole_machine::go_direbrew_mole_machineAI::UpdateAI(), npc_hallows_end_soh::UpdateAI(), boss_azuregos::boss_azuregosAI::UpdateAI(), boss_isalien::UpdateAI(), boss_ayamiss::UpdateAI(), npc_anubisath_guardian::UpdateAI(), npc_hivezara_stinger::UpdateAI(), npc_obsidian_destroyer::UpdateAI(), boss_bug_trio::UpdateAI(), boss_cthun::UpdateAI(), npc_eye_tentacle::UpdateAI(), npc_claw_tentacle::UpdateAI(), npc_giant_eye_tentacle::UpdateAI(), boss_fankriss::UpdateAI(), boss_ouro::UpdateAI(), npc_dirt_mound::UpdateAI(), boss_twinemperorsAI::UpdateAI(), boss_viscidus::UpdateAI(), boss_glob_of_viscidus::UpdateAI(), npc_anubisath_defender::UpdateAI(), npc_vekniss_stinger::UpdateAI(), npc_anubisath_warder::UpdateAI(), npc_obsidian_nullifier::UpdateAI(), npc_ahnqiraji_critter::UpdateAI(), boss_hungarfen::UpdateAI(), npc_underbog_mushroom::UpdateAI(), npc_raliq_the_drunk::npc_raliq_the_drunkAI::UpdateAI(), and npc_dream_fog::npc_dream_fogAI::UpdateAI().

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(), DelayAll(), DelayGroup(), Dispatch(), InsertTask(), RescheduleAll(), and RescheduleGroup().

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