18 #ifndef _TASK_SCHEDULER_H_
19 #define _TASK_SCHEDULER_H_
29 #include <boost/optional.hpp>
54 typedef std::chrono::steady_clock
clock_t;
82 Task(timepoint_t
const& end, duration_t
const& duration, boost::optional<group_t>
const& group,
83 repeated_t
const repeated, task_handler_t
const& task)
84 : _end(end), _duration(duration), _group(group), _repeated(repeated), _task(task) { }
87 Task(timepoint_t
const& end, duration_t
const& duration, task_handler_t
const& task)
88 : _end(end), _duration(duration), _group(
boost::none), _repeated(0), _task(task) { }
102 return _end < other.
_end;
107 return _end > other.
_end;
113 return _end == other.
_end;
119 return _group == group;
128 bool operator() (TaskContainer
const&
left, TaskContainer
const& right)
130 return (*left.get()) < (*right.get());
140 void Push(TaskContainer&& task);
145 TaskContainer
const& First()
const;
149 void RemoveIf(std::function<
bool(TaskContainer
const&)>
const& filter);
151 void ModifyIf(std::function<
bool(TaskContainer
const&)>
const& filter);
153 bool IsEmpty()
const;
184 : self_reference(this, [](
TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { }
188 : self_reference(this, [](
TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }
199 _predicate = std::forward<P>(predicate);
216 template<
class _Rep,
class _Period>
218 success_t
const& callback = EmptyCallback)
231 template<
class _Rep,
class _Period>
233 task_handler_t
const& task)
235 return ScheduleAt(_now, time, task);
240 template<
class _Rep,
class _Period>
242 group_t
const group, task_handler_t
const& task)
244 return ScheduleAt(_now, time, group, task);
249 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
251 std::chrono::duration<_RepRight, _PeriodRight>
const&
max, task_handler_t
const& task)
253 return Schedule(RandomDurationBetween(min, max), task);
258 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
260 std::chrono::duration<_RepRight, _PeriodRight>
const&
max, group_t
const group,
261 task_handler_t
const& task)
263 return Schedule(RandomDurationBetween(min, max), group, task);
276 TaskScheduler& CancelGroupsOf(std::vector<group_t>
const& groups);
279 template<
class _Rep,
class _Period>
282 _task_holder.
ModifyIf([&duration](TaskContainer
const& task) ->
bool
284 task->_end += duration;
291 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
293 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
295 return DelayAll(RandomDurationBetween(min, max));
299 template<
class _Rep,
class _Period>
302 _task_holder.
ModifyIf([&duration, group](TaskContainer
const& task) ->
bool
304 if (task->IsInGroup(group))
306 task->_end += duration;
316 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
318 std::chrono::duration<_RepLeft, _PeriodLeft>
const&
min,
319 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
321 return DelayGroup(group, RandomDurationBetween(min, max));
325 template<
class _Rep,
class _Period>
328 auto const end = _now + duration;
329 _task_holder.
ModifyIf([end](TaskContainer
const& task) ->
bool
338 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
340 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
342 return RescheduleAll(RandomDurationBetween(min, max));
346 template<
class _Rep,
class _Period>
349 auto const end = _now + duration;
350 _task_holder.
ModifyIf([end, group](TaskContainer
const& task) ->
bool
352 if (task->IsInGroup(group))
364 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
366 std::chrono::duration<_RepLeft, _PeriodLeft>
const&
min,
367 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
369 return RescheduleGroup(group, RandomDurationBetween(min, max));
376 template<
class _Rep,
class _Period>
378 std::chrono::duration<_Rep, _Period>
const& time, task_handler_t
const& task)
380 return InsertTask(TaskContainer(
new Task(end + time, time, task)));
385 template<
class _Rep,
class _Period>
387 std::chrono::duration<_Rep, _Period>
const& time,
388 group_t
const group, task_handler_t
const& task)
390 static repeated_t
const DEFAULT_REPEATED = 0;
391 return InsertTask(TaskContainer(
new Task(end + time, time, group, DEFAULT_REPEATED, task)));
395 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
398 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
408 void Dispatch(success_t
const& callback);
430 : _task(), _owner(), _consumed(
std::make_shared<
bool>(
true)) { }
434 : _task(task), _owner(owner), _consumed(
std::make_shared<
bool>(
false)) { }
438 : _task(right._task), _owner(right._owner), _consumed(right._consumed) { }
442 : _task(
std::move(right._task)), _owner(
std::move(right._owner)), _consumed(
std::move(right._consumed)) { }
456 _task = std::move(right._task);
457 _owner = std::move(right._owner);
458 _consumed = std::move(right._consumed);
463 bool IsExpired()
const;
481 template<
class _Rep,
class _Period>
487 _task->_duration = duration;
488 _task->_end += duration;
489 _task->_repeated += 1;
499 return Repeat(_task->_duration);
506 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
508 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
521 template<
class _Rep,
class _Period>
525 auto const end = _task->_end;
528 return scheduler.
ScheduleAt<_Rep, _Period>(end, time, task);
536 template<
class _Rep,
class _Period>
540 auto const end = _task->_end;
543 return scheduler.
ScheduleAt<_Rep, _Period>(end, time, group, task);
551 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
562 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
581 template<
class _Rep,
class _Period>
584 return Dispatch(std::bind(&TaskScheduler::DelayAll<_Rep, _Period>, std::placeholders::_1, duration));
588 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
590 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
596 template<
class _Rep,
class _Period>
599 return Dispatch(std::bind(&TaskScheduler::DelayGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
603 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
605 std::chrono::duration<_RepLeft, _PeriodLeft>
const&
min,
606 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
612 template<
class _Rep,
class _Period>
619 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
621 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
627 template<
class _Rep,
class _Period>
630 return Dispatch(std::bind(&TaskScheduler::RescheduleGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
634 template<
class _RepLeft,
class _PeriodLeft,
class _RepRight,
class _PeriodRight>
636 std::chrono::duration<_RepLeft, _PeriodLeft>
const&
min,
637 std::chrono::duration<_RepRight, _PeriodRight>
const&
max)
644 void AssertOnConsumed()
const;
std::shared_ptr< TaskScheduler > self_reference
Contains a self reference to track if this object was deleted or not.
Definition: TaskScheduler.h:157
TaskContext(TaskContext &&right)
Definition: TaskScheduler.h:441
Definition: TaskScheduler.h:134
std::queue< std::function< void()> > AsyncHolder
Definition: TaskScheduler.h:165
TaskScheduler::TaskContainer _task
Associated task.
Definition: TaskScheduler.h:416
TaskContext & 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.
Definition: TaskScheduler.h:620
TaskScheduler & SetValidator(P &&predicate)
Sets a validator which is asked if tasks are allowed to be executed.
Definition: TaskScheduler.h:197
Definition: TaskScheduler.h:49
TaskContext & DelayGroup(TaskScheduler::group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks of a group with the given duration from within the context.
Definition: TaskScheduler.h:597
TaskContext & Repeat(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max)
Definition: TaskScheduler.h:507
TaskScheduler & ScheduleAt(timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:377
TaskContext & RescheduleGroup(TaskScheduler::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.
Definition: TaskScheduler.h:635
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
static void EmptyCallback()
Definition: TaskScheduler.h:178
AsyncHolder _asyncHolder
Definition: TaskScheduler.h:169
TaskContext & DelayAll(std::chrono::duration< _Rep, _Period > const &duration)
Delays all tasks with the given duration from within the context.
Definition: TaskScheduler.h:582
TaskContext(TaskContext const &right)
Definition: TaskScheduler.h:437
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.
Definition: TaskScheduler.h:339
TaskContext & DelayGroup(TaskScheduler::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 from within the context...
Definition: TaskScheduler.h:604
TaskContext & RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks of a group with the given duration.
Definition: TaskScheduler.h:628
std::function< bool()> predicate_t
Definition: TaskScheduler.h:65
TaskContext()
Definition: TaskScheduler.h:429
#define false
Definition: CascPort.h:18
std::shared_ptr< bool > _consumed
Marks the task as consumed.
Definition: TaskScheduler.h:422
Definition: TaskScheduler.h:69
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.
Definition: TaskScheduler.h:292
bool IsInGroup(group_t const group) const
Definition: TaskScheduler.h:117
bool operator<(const String &left, const String &right)
Definition: String.cpp:785
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
TaskScheduler & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with a given duration.
Definition: TaskScheduler.h:326
TaskContext & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, TaskScheduler::group_t const group, TaskScheduler::task_handler_t const &task)
Definition: TaskScheduler.h:563
void apply(T *val)
Definition: ByteConverter.h:41
TaskQueue _task_holder
The Task Queue which contains all task objects.
Definition: TaskScheduler.h:163
void Dispatch(success_t const &callback)
Dispatch remaining tasks.
Definition: TaskScheduler.cpp:75
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task)
Definition: TaskScheduler.h:241
TaskContext & Repeat(std::chrono::duration< _Rep, _Period > const &duration)
Definition: TaskScheduler.h:482
void ModifyIf(std::function< bool(TaskContainer const &)> const &filter)
Definition: TaskScheduler.cpp:144
TaskScheduler & CancelGroupsOf(std::vector< group_t > const &groups)
Definition: TaskScheduler.cpp:61
T max(const T &x, const T &y)
Definition: g3dmath.h:320
#define bool
Definition: CascPort.h:16
#define true
Definition: CascPort.h:17
task_handler_t _task
Definition: TaskScheduler.h:78
repeated_t _repeated
Definition: TaskScheduler.h:77
TaskContext & RescheduleAll(std::chrono::duration< _Rep, _Period > const &duration)
Reschedule all tasks with the given duration.
Definition: TaskScheduler.h:613
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.
Definition: TaskScheduler.h:365
static bool EmptyValidator()
Definition: TaskScheduler.h:173
T min(const T &x, const T &y)
Definition: g3dmath.h:305
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)
Definition: TaskScheduler.h:259
TaskScheduler(P &&predicate)
Definition: TaskScheduler.h:187
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
TaskScheduler & Update(std::chrono::duration< _Rep, _Period > const &difftime, success_t const &callback=EmptyCallback)
Definition: TaskScheduler.h:217
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:45
Container which provides Task order, insert and reschedule operations.
Definition: TaskScheduler.h:126
Vector2int16 & operator=(const Any &a)
timepoint_t _end
Definition: TaskScheduler.h:74
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
uint32_t uint32
Definition: Define.h:150
std::multiset< TaskContainer, Compare > container
Definition: TaskScheduler.h:136
bool left(const int *a, const int *b, const int *c)
Definition: RecastContour.cpp:487
float milliseconds()
Definition: units.h:92
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.
Definition: TaskScheduler.h:317
boost::optional< group_t > _group
Definition: TaskScheduler.h:76
#define TC_COMMON_API
Definition: Define.h:116
timepoint_t _now
The current time point (now)
Definition: TaskScheduler.h:160
TaskScheduler & InsertTask(TaskContainer task)
Insert a new task to the enqueued tasks.
Definition: TaskScheduler.cpp:69
std::function< void()> success_t
Definition: TaskScheduler.h:67
TaskContext & Repeat()
Definition: TaskScheduler.h:497
TaskContext & 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 from within the context.
Definition: TaskScheduler.h:589
duration_t _duration
Definition: TaskScheduler.h:75
TaskScheduler & CancelAll()
Definition: TaskScheduler.cpp:44
TaskScheduler & Schedule(std::chrono::duration< _Rep, _Period > const &time, task_handler_t const &task)
Definition: TaskScheduler.h:232
TaskContext(TaskScheduler::TaskContainer &&task, std::weak_ptr< TaskScheduler > &&owner)
Definition: TaskScheduler.h:433
TaskScheduler()
Definition: TaskScheduler.h:183
Task(timepoint_t const &end, duration_t const &duration, task_handler_t const &task)
Definition: TaskScheduler.h:87
std::shared_ptr< Task > TaskContainer
Definition: TaskScheduler.h:123
Task(timepoint_t const &end, duration_t const &duration, boost::optional< group_t > const &group, repeated_t const repeated, task_handler_t const &task)
Definition: TaskScheduler.h:82
TaskContext & Schedule(std::chrono::duration< _Rep, _Period > const &time, TaskScheduler::group_t const group, TaskScheduler::task_handler_t const &task)
Definition: TaskScheduler.h:537
clock_t::time_point timepoint_t
Definition: TaskScheduler.h:55
predicate_t _predicate
Definition: TaskScheduler.h:171
uint32 repeated_t
Definition: TaskScheduler.h:61
uint32_t uint32
Definition: g3dmath.h:168
#define const
Definition: zconf.h:217
TaskScheduler & ScheduleAt(timepoint_t const &end, std::chrono::duration< _Rep, _Period > const &time, group_t const group, task_handler_t const &task)
Definition: TaskScheduler.h:386
uint8 const P[]
Definition: AuthenticationPackets.cpp:225
std::weak_ptr< TaskScheduler > _owner
Owner.
Definition: TaskScheduler.h:419
uint32 group_t
Definition: TaskScheduler.h:59
TaskScheduler & Async(std::function< void()> const &callable)
Definition: TaskScheduler.cpp:38
TaskContext & Schedule(std::chrono::duration< _Rep, _Period > const &time, TaskScheduler::task_handler_t const &task)
Definition: TaskScheduler.h:522
std::function< void(TaskContext)> task_handler_t
Definition: TaskScheduler.h:63
TaskScheduler & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, task_handler_t const &task)
Definition: TaskScheduler.h:250
Definition: TaskScheduler.h:411
std::chrono::steady_clock clock_t
Definition: TaskScheduler.h:54
TaskContext & Schedule(std::chrono::duration< _RepLeft, _PeriodLeft > const &min, std::chrono::duration< _RepRight, _PeriodRight > const &max, TaskScheduler::task_handler_t const &task)
Definition: TaskScheduler.h:552
void Update(uint32 diff)
Definition: WeatherMgr.cpp:150
TaskScheduler & CancelGroup(group_t const group)
Definition: TaskScheduler.cpp:52
bool operator>(const String &left, const String &right)
Definition: String.cpp:790
clock_t::duration duration_t
Definition: TaskScheduler.h:56