Horizon Official Technical Documentation
|
|
The TaskScheduler class provides the ability to schedule std::function's in the near future. Use TaskScheduler::Update to update the scheduler. Popular methods are: More...
#include <TaskScheduler.hpp>
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 | |
TaskScheduler & | operator= (TaskScheduler const &)=delete |
TaskScheduler & | operator= (TaskScheduler &&)=delete |
template<typename P > | |
TaskScheduler & | SetValidator (P &&predicate) |
Sets a Validator which is asked if tasks are allowed to be executed. More... | |
TaskScheduler & | ClearValidator () |
Clears the Validator which is asked if tasks are allowed to be executed. More... | |
TaskScheduler & | Update (success_t const &callback=EmptyCallback) |
Update the scheduler to the current time. Calls the optional callback on successfully finish. More... | |
TaskScheduler & | Update (size_t const microseconds, success_t const &callback=EmptyCallback) |
Update the scheduler with a difftime in ms. Calls the optional callback on successfully finish. More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | Update (std::chrono::duration< _Rep, _Period > const &difftime, success_t const &callback=EmptyCallback) |
Update the scheduler with a difftime. Calls the optional callback on successfully finish. More... | |
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. More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | Schedule (std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task) |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead! More... | |
std::size_t | Count (group_t const &group) |
template<typename _Rep , typename _Period > | |
TaskScheduler & | Schedule (std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task) |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead! More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
TaskScheduler & | Schedule (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, task_handler_t const &task) |
Schedule an event with a randomized rate between min and max rate. Never call this from within a task context! Use TaskContext::Schedule instead! More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
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) |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead! More... | |
TaskScheduler & | CancelAll () |
Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead! More... | |
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! More... | |
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}". More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | DelayAll (std::chrono::duration< _Rep, _Period > const &duration) |
Delays all tasks with the given duration. More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
TaskScheduler & | DelayAll (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
Delays all tasks with a random duration between min and max. More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | DelayGroup (group_t const group, std::chrono::duration< _Rep, _Period > const &duration) |
Delays all tasks of a group with the given duration. More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
TaskScheduler & | DelayGroup (group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
Delays all tasks of a group with a random duration between min and max. More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | RescheduleAll (std::chrono::duration< _Rep, _Period > const &duration) |
Reschedule all tasks with a given duration. More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
TaskScheduler & | RescheduleAll (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
Reschedule all tasks with a random duration between min and max. More... | |
template<typename _Rep , typename _Period > | |
TaskScheduler & | RescheduleGroup (group_t const group, std::chrono::duration< _Rep, _Period > const &duration) |
Reschedule all tasks of a group with the given duration. More... | |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
TaskScheduler & | RescheduleGroup (group_t const group, std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
Reschedule all tasks of a group with a random duration between min and max. More... | |
Private Types | |
typedef std::chrono::system_clock | clock_t |
typedef clock_t::time_point | timepoint_t |
typedef std::function< clock_t::duration()> | duration_calculator_t |
typedef uint64_t | group_t |
typedef unsigned int | repeated_t |
typedef std::function< void(TaskContext)> | task_handler_t |
typedef std::function< bool()> | predicate_t |
typedef std::function< void()> | success_t |
typedef std::shared_ptr< Task > | TaskContainer |
typedef std::queue< std::function< void()> > | AsyncHolder |
Private Member Functions | |
TaskScheduler & | InsertTask (TaskContainer task) |
Insert a new task to the enqueued tasks. More... | |
TaskScheduler & | ScheduleAt (timepoint_t const &end, duration_calculator_t &&duration_calculator, task_handler_t const &task) |
TaskScheduler & | ScheduleAt (timepoint_t const &end, duration_calculator_t &&duration_calculator, group_t const group, task_handler_t const &task) |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::schedule instead! More... | |
TaskScheduler & | RescheduleAt (timepoint_t const &end) |
TaskScheduler & | RescheduleAtWithPredicate (timepoint_t const &end, std::function< bool(TaskContainer const &)> const &predicate) |
Reschedule all tasks with a given duration relative to the given time. More... | |
void | Dispatch (success_t const &callback) |
Dispatch remaining tasks when the given condition fits. More... | |
Static Private Member Functions | |
template<typename _Rep , typename _Period > | |
static duration_calculator_t | MakeDurationCalculator (std::chrono::duration< _Rep, _Period > const &duration) |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
static duration_calculator_t | MakeDurationCalculator (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
template<typename T , typename... Args> | |
static std::unique_ptr< T > | MakeUnique (Args &&... args) |
static bool | EmptyValidator () |
static void | EmptyCallback () |
static bool | AlwaysTruePredicate (TaskContainer const &) |
template<typename _RepLeft , typename _PeriodLeft , typename _RepRight , typename _PeriodRight > | |
static std::chrono::duration< _RepLeft, _PeriodLeft > | RandomDurationBetween (std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max) |
Private Attributes | |
std::shared_ptr< TaskScheduler > | self_reference |
Contains a self reference to track if this object was deleted or not. More... | |
timepoint_t | _now |
The current time point (now) More... | |
TaskQueue | _task_holder |
The Task Queue which contains all task objects. More... | |
AsyncHolder | _asyncHolder |
Contains all asynchronous tasks which will be invoked at the next update tick. More... | |
predicate_t | _predicate |
Friends | |
class | TaskContext |
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:
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
inline |
|
inline |
|
delete |
|
delete |
|
inlinestaticprivate |
Referenced by RescheduleAt().
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.
References _asyncHolder.
Referenced by TaskContext::Async().
TaskScheduler & TaskScheduler::CancelAll | ( | ) |
Cancels all tasks. Never call this from within a task context! Use TaskContext::CancelAll instead!
Clear the task holder
References _asyncHolder, _task_holder, and TaskScheduler::TaskQueue::Clear().
Referenced by TaskContext::CancelAll(), Horizon::Zone::ZoneKernel::finalize(), Horizon::Auth::AuthServer::update(), and Horizon::Char::CharServer::update().
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!
References _task_holder, and TaskScheduler::TaskQueue::RemoveIf().
Referenced by TaskContext::CancelGroup(), and 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}".
References CancelGroup().
Referenced by TaskContext::CancelGroupsOf().
TaskScheduler & TaskScheduler::ClearValidator | ( | ) |
Clears the Validator which is asked if tasks are allowed to be executed.
Copyright 2014-2015 Denis Blank denis.nosp@m..bla.nosp@m.nk@ou.nosp@m.tloo.nosp@m.k.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
References _predicate, and EmptyValidator().
std::size_t TaskScheduler::Count | ( | group_t const & | group | ) |
References _task_holder, and TaskScheduler::TaskQueue::Count().
|
inline |
Delays all tasks with the given duration.
References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().
Referenced by DelayAll().
|
inline |
Delays all tasks with a random duration between min and max.
References DelayAll(), and RandomDurationBetween().
|
inline |
Delays all tasks of a group with the given duration.
References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().
Referenced by DelayGroup().
|
inline |
Delays all tasks of a group with a random duration between min and max.
References DelayGroup(), and RandomDurationBetween().
|
private |
Dispatch remaining tasks when the given condition fits.
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().
|
inlinestaticprivate |
|
inlinestaticprivate |
Referenced by ClearValidator().
|
private |
Insert a new task to the enqueued tasks.
References _task_holder, and TaskScheduler::TaskQueue::Push().
Referenced by TaskContext::Repeat(), and ScheduleAt().
|
inlinestaticprivate |
Referenced by TaskContext::Repeat(), Schedule(), and TaskContext::Schedule().
|
inlinestaticprivate |
|
inlinestaticprivate |
|
delete |
|
delete |
|
inlinestaticprivate |
Referenced by DelayAll(), TaskContext::DelayAll(), DelayGroup(), TaskContext::DelayGroup(), RescheduleAll(), TaskContext::RescheduleAll(), RescheduleGroup(), and TaskContext::RescheduleGroup().
|
inline |
Reschedule all tasks with a given duration.
References _now, and RescheduleAt().
Referenced by RescheduleAll().
|
inline |
Reschedule all tasks with a random duration between min and max.
References RandomDurationBetween(), and RescheduleAll().
|
inlineprivate |
References AlwaysTruePredicate(), and RescheduleAtWithPredicate().
Referenced by RescheduleAll(), and TaskContext::RescheduleAll().
|
inlineprivate |
Reschedule all tasks with a given duration relative to the given time.
References _task_holder, and TaskScheduler::TaskQueue::ModifyIf().
Referenced by RescheduleAt(), RescheduleGroup(), and TaskContext::RescheduleGroup().
|
inline |
Reschedule all tasks of a group with the given duration.
References _now, and RescheduleAtWithPredicate().
Referenced by RescheduleGroup().
|
inline |
Reschedule all tasks of a group with a random duration between min and max.
References RandomDurationBetween(), and RescheduleGroup().
|
inline |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!
References _now, MakeDurationCalculator(), and ScheduleAt().
|
inline |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!
References _now, MakeDurationCalculator(), and ScheduleAt().
Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Zone::ZoneKernel::initialize(), Horizon::Auth::AuthServer::initialize(), Horizon::Char::CharServer::initialize(), and Horizon::Zone::ScriptManager::initialize_basic_state().
|
inline |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::Schedule instead!
References _now, MakeDurationCalculator(), and ScheduleAt().
|
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!
References _now, MakeDurationCalculator(), and ScheduleAt().
|
inlineprivate |
Schedule an event with a fixed rate. Never call this from within a task context! Use TaskContext::schedule instead!
References InsertTask().
|
inlineprivate |
References InsertTask().
Referenced by Schedule(), and TaskContext::Schedule().
|
inline |
Sets a Validator which is asked if tasks are allowed to be executed.
References _predicate.
TaskScheduler & TaskScheduler::Update | ( | size_t const | microseconds, |
success_t const & | callback = EmptyCallback |
||
) |
Update the scheduler with a difftime in ms. Calls the optional callback on successfully finish.
References Update().
|
inline |
Update the scheduler with a difftime. Calls the optional callback on successfully finish.
References _now, and Dispatch().
TaskScheduler & TaskScheduler::Update | ( | success_t const & | callback = EmptyCallback | ) |
Update the scheduler to the current time. Calls the optional callback on successfully finish.
References _now, and Dispatch().
Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Zone::ZoneServer::update(), Update(), Horizon::Zone::GameLogicProcess::update(), Horizon::Zone::ScriptManager::update(), Horizon::Auth::AuthServer::update(), and Horizon::Char::CharServer::update().
|
friend |
|
private |
Contains all asynchronous tasks which will be invoked at the next update tick.
Referenced by Async(), CancelAll(), and Dispatch().
|
private |
The current time point (now)
Referenced by Dispatch(), RescheduleAll(), RescheduleGroup(), Schedule(), and Update().
|
private |
Referenced by ClearValidator(), Dispatch(), and SetValidator().
|
private |
The Task Queue which contains all task objects.
Referenced by CancelAll(), CancelGroup(), Count(), DelayAll(), DelayGroup(), Dispatch(), InsertTask(), and RescheduleAtWithPredicate().
|
private |
Contains a self reference to track if this object was deleted or not.
Referenced by Dispatch().