TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TaskScheduler.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _TASK_SCHEDULER_H_
19 #define _TASK_SCHEDULER_H_
20 
21 #include <algorithm>
22 #include <chrono>
23 #include <vector>
24 #include <queue>
25 #include <memory>
26 #include <utility>
27 #include <set>
28 
29 #include <boost/optional.hpp>
30 
31 #include "Util.h"
32 #include "Duration.h"
33 
34 class TaskContext;
35 
50 {
51  friend class TaskContext;
52 
53  // Time definitions (use steady clock)
54  typedef std::chrono::steady_clock clock_t;
55  typedef clock_t::time_point timepoint_t;
56  typedef clock_t::duration duration_t;
57 
58  // Task group type
59  typedef uint32 group_t;
60  // Task repeated type
61  typedef uint32 repeated_t;
62  // Task handle type
63  typedef std::function<void(TaskContext)> task_handler_t;
64  // Predicate type
65  typedef std::function<bool()> predicate_t;
66  // Success handle type
67  typedef std::function<void()> success_t;
68 
69  class Task
70  {
71  friend class TaskContext;
72  friend class TaskScheduler;
73 
74  timepoint_t _end;
75  duration_t _duration;
76  boost::optional<group_t> _group;
77  repeated_t _repeated;
78  task_handler_t _task;
79 
80  public:
81  // All Argument construct
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) { }
85 
86  // Minimal Argument construct
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) { }
89 
90  // Copy construct
91  Task(Task const&) = delete;
92  // Move construct
93  Task(Task&&) = delete;
94  // Copy Assign
95  Task& operator= (Task const&) = default;
96  // Move Assign
97  Task& operator= (Task&& right) = delete;
98 
99  // Order tasks by its end
100  inline bool operator< (Task const& other) const
101  {
102  return _end < other._end;
103  }
104 
105  inline bool operator> (Task const& other) const
106  {
107  return _end > other._end;
108  }
109 
110  // Compare tasks with its end
111  inline bool operator== (Task const& other)
112  {
113  return _end == other._end;
114  }
115 
116  // Returns true if the task is in the given group
117  inline bool IsInGroup(group_t const group) const
118  {
119  return _group == group;
120  }
121  };
122 
123  typedef std::shared_ptr<Task> TaskContainer;
124 
126  struct Compare
127  {
128  bool operator() (TaskContainer const& left, TaskContainer const& right)
129  {
130  return (*left.get()) < (*right.get());
131  };
132  };
133 
135  {
136  std::multiset<TaskContainer, Compare> container;
137 
138  public:
139  // Pushes the task in the container
140  void Push(TaskContainer&& task);
141 
143  TaskContainer Pop();
144 
145  TaskContainer const& First() const;
146 
147  void Clear();
148 
149  void RemoveIf(std::function<bool(TaskContainer const&)> const& filter);
150 
151  void ModifyIf(std::function<bool(TaskContainer const&)> const& filter);
152 
153  bool IsEmpty() const;
154  };
155 
157  std::shared_ptr<TaskScheduler> self_reference;
158 
160  timepoint_t _now;
161 
164 
165  typedef std::queue<std::function<void()>> AsyncHolder;
166 
169  AsyncHolder _asyncHolder;
170 
171  predicate_t _predicate;
172 
173  static bool EmptyValidator()
174  {
175  return true;
176  }
177 
178  static void EmptyCallback()
179  {
180  }
181 
182 public:
184  : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { }
185 
186  template<typename P>
187  TaskScheduler(P&& predicate)
188  : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }
189 
190  TaskScheduler(TaskScheduler const&) = delete;
191  TaskScheduler(TaskScheduler&&) = delete;
192  TaskScheduler& operator= (TaskScheduler const&) = delete;
194 
196  template<typename P>
198  {
199  _predicate = std::forward<P>(predicate);
200  return *this;
201  }
202 
204  TaskScheduler& ClearValidator();
205 
208  TaskScheduler& Update(success_t const& callback = EmptyCallback);
209 
212  TaskScheduler& Update(size_t const milliseconds, success_t const& callback = EmptyCallback);
213 
216  template<class _Rep, class _Period>
217  TaskScheduler& Update(std::chrono::duration<_Rep, _Period> const& difftime,
218  success_t const& callback = EmptyCallback)
219  {
220  _now += difftime;
221  Dispatch(callback);
222  return *this;
223  }
224 
227  TaskScheduler& Async(std::function<void()> const& callable);
228 
231  template<class _Rep, class _Period>
232  TaskScheduler& Schedule(std::chrono::duration<_Rep, _Period> const& time,
233  task_handler_t const& task)
234  {
235  return ScheduleAt(_now, time, task);
236  }
237 
240  template<class _Rep, class _Period>
241  TaskScheduler& Schedule(std::chrono::duration<_Rep, _Period> const& time,
242  group_t const group, task_handler_t const& task)
243  {
244  return ScheduleAt(_now, time, group, task);
245  }
246 
249  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
250  TaskScheduler& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
251  std::chrono::duration<_RepRight, _PeriodRight> const& max, task_handler_t const& task)
252  {
253  return Schedule(RandomDurationBetween(min, max), task);
254  }
255 
258  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
259  TaskScheduler& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
260  std::chrono::duration<_RepRight, _PeriodRight> const& max, group_t const group,
261  task_handler_t const& task)
262  {
263  return Schedule(RandomDurationBetween(min, max), group, task);
264  }
265 
268  TaskScheduler& CancelAll();
269 
272  TaskScheduler& CancelGroup(group_t const group);
273 
276  TaskScheduler& CancelGroupsOf(std::vector<group_t> const& groups);
277 
279  template<class _Rep, class _Period>
280  TaskScheduler& DelayAll(std::chrono::duration<_Rep, _Period> const& duration)
281  {
282  _task_holder.ModifyIf([&duration](TaskContainer const& task) -> bool
283  {
284  task->_end += duration;
285  return true;
286  });
287  return *this;
288  }
289 
291  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
292  TaskScheduler& DelayAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
293  std::chrono::duration<_RepRight, _PeriodRight> const& max)
294  {
295  return DelayAll(RandomDurationBetween(min, max));
296  }
297 
299  template<class _Rep, class _Period>
300  TaskScheduler& DelayGroup(group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
301  {
302  _task_holder.ModifyIf([&duration, group](TaskContainer const& task) -> bool
303  {
304  if (task->IsInGroup(group))
305  {
306  task->_end += duration;
307  return true;
308  }
309  else
310  return false;
311  });
312  return *this;
313  }
314 
316  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
317  TaskScheduler& DelayGroup(group_t const group,
318  std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
319  std::chrono::duration<_RepRight, _PeriodRight> const& max)
320  {
321  return DelayGroup(group, RandomDurationBetween(min, max));
322  }
323 
325  template<class _Rep, class _Period>
326  TaskScheduler& RescheduleAll(std::chrono::duration<_Rep, _Period> const& duration)
327  {
328  auto const end = _now + duration;
329  _task_holder.ModifyIf([end](TaskContainer const& task) -> bool
330  {
331  task->_end = end;
332  return true;
333  });
334  return *this;
335  }
336 
338  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
339  TaskScheduler& RescheduleAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
340  std::chrono::duration<_RepRight, _PeriodRight> const& max)
341  {
342  return RescheduleAll(RandomDurationBetween(min, max));
343  }
344 
346  template<class _Rep, class _Period>
347  TaskScheduler& RescheduleGroup(group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
348  {
349  auto const end = _now + duration;
350  _task_holder.ModifyIf([end, group](TaskContainer const& task) -> bool
351  {
352  if (task->IsInGroup(group))
353  {
354  task->_end = end;
355  return true;
356  }
357  else
358  return false;
359  });
360  return *this;
361  }
362 
364  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
365  TaskScheduler& RescheduleGroup(group_t const group,
366  std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
367  std::chrono::duration<_RepRight, _PeriodRight> const& max)
368  {
369  return RescheduleGroup(group, RandomDurationBetween(min, max));
370  }
371 
372 private:
374  TaskScheduler& InsertTask(TaskContainer task);
375 
376  template<class _Rep, class _Period>
377  TaskScheduler& ScheduleAt(timepoint_t const& end,
378  std::chrono::duration<_Rep, _Period> const& time, task_handler_t const& task)
379  {
380  return InsertTask(TaskContainer(new Task(end + time, time, task)));
381  }
382 
385  template<class _Rep, class _Period>
386  TaskScheduler& ScheduleAt(timepoint_t const& end,
387  std::chrono::duration<_Rep, _Period> const& time,
388  group_t const group, task_handler_t const& task)
389  {
390  static repeated_t const DEFAULT_REPEATED = 0;
391  return InsertTask(TaskContainer(new Task(end + time, time, group, DEFAULT_REPEATED, task)));
392  }
393 
394  // Returns a random duration between min and max
395  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
397  RandomDurationBetween(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
398  std::chrono::duration<_RepRight, _PeriodRight> const& max)
399  {
400  auto const milli_min = std::chrono::duration_cast<std::chrono::milliseconds>(min);
401  auto const milli_max = std::chrono::duration_cast<std::chrono::milliseconds>(max);
402 
403  // TC specific: use SFMT URandom
404  return std::chrono::milliseconds(urand(uint32(milli_min.count()), uint32(milli_max.count())));
405  }
406 
408  void Dispatch(success_t const& callback);
409 };
410 
412 {
413  friend class TaskScheduler;
414 
417 
419  std::weak_ptr<TaskScheduler> _owner;
420 
422  std::shared_ptr<bool> _consumed;
423 
425  TaskContext& Dispatch(std::function<TaskScheduler&(TaskScheduler&)> const& apply);
426 
427 public:
428  // Empty constructor
430  : _task(), _owner(), _consumed(std::make_shared<bool>(true)) { }
431 
432  // Construct from task and owner
433  explicit TaskContext(TaskScheduler::TaskContainer&& task, std::weak_ptr<TaskScheduler>&& owner)
434  : _task(task), _owner(owner), _consumed(std::make_shared<bool>(false)) { }
435 
436  // Copy construct
437  TaskContext(TaskContext const& right)
438  : _task(right._task), _owner(right._owner), _consumed(right._consumed) { }
439 
440  // Move construct
442  : _task(std::move(right._task)), _owner(std::move(right._owner)), _consumed(std::move(right._consumed)) { }
443 
444  // Copy assign
446  {
447  _task = right._task;
448  _owner = right._owner;
449  _consumed = right._consumed;
450  return *this;
451  }
452 
453  // Move assign
455  {
456  _task = std::move(right._task);
457  _owner = std::move(right._owner);
458  _consumed = std::move(right._consumed);
459  return *this;
460  }
461 
463  bool IsExpired() const;
464 
466  bool IsInGroup(TaskScheduler::group_t const group) const;
467 
469  TaskContext& SetGroup(TaskScheduler::group_t const group);
470 
472  TaskContext& ClearGroup();
473 
475  TaskScheduler::repeated_t GetRepeatCounter() const;
476 
481  template<class _Rep, class _Period>
482  TaskContext& Repeat(std::chrono::duration<_Rep, _Period> const& duration)
483  {
484  AssertOnConsumed();
485 
486  // Set new duration, in-context timing and increment repeat counter
487  _task->_duration = duration;
488  _task->_end += duration;
489  _task->_repeated += 1;
490  (*_consumed) = true;
491  return Dispatch(std::bind(&TaskScheduler::InsertTask, std::placeholders::_1, _task));
492  }
493 
498  {
499  return Repeat(_task->_duration);
500  }
501 
506  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
507  TaskContext& Repeat(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
508  std::chrono::duration<_RepRight, _PeriodRight> const& max)
509  {
510  return Repeat(TaskScheduler::RandomDurationBetween(min, max));
511  }
512 
515  TaskContext& Async(std::function<void()> const& callable);
516 
521  template<class _Rep, class _Period>
522  TaskContext& Schedule(std::chrono::duration<_Rep, _Period> const& time,
523  TaskScheduler::task_handler_t const& task)
524  {
525  auto const end = _task->_end;
526  return Dispatch([end, time, task](TaskScheduler& scheduler) -> TaskScheduler&
527  {
528  return scheduler.ScheduleAt<_Rep, _Period>(end, time, task);
529  });
530  }
531 
536  template<class _Rep, class _Period>
537  TaskContext& Schedule(std::chrono::duration<_Rep, _Period> const& time,
538  TaskScheduler::group_t const group, TaskScheduler::task_handler_t const& task)
539  {
540  auto const end = _task->_end;
541  return Dispatch([end, time, group, task](TaskScheduler& scheduler) -> TaskScheduler&
542  {
543  return scheduler.ScheduleAt<_Rep, _Period>(end, time, group, task);
544  });
545  }
546 
551  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
552  TaskContext& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
553  std::chrono::duration<_RepRight, _PeriodRight> const& max, TaskScheduler::task_handler_t const& task)
554  {
555  return Schedule(TaskScheduler::RandomDurationBetween(min, max), task);
556  }
557 
562  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
563  TaskContext& Schedule(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
564  std::chrono::duration<_RepRight, _PeriodRight> const& max, TaskScheduler::group_t const group,
565  TaskScheduler::task_handler_t const& task)
566  {
567  return Schedule(TaskScheduler::RandomDurationBetween(min, max), group, task);
568  }
569 
572 
575 
578  TaskContext& CancelGroupsOf(std::vector<TaskScheduler::group_t> const& groups);
579 
581  template<class _Rep, class _Period>
582  TaskContext& DelayAll(std::chrono::duration<_Rep, _Period> const& duration)
583  {
584  return Dispatch(std::bind(&TaskScheduler::DelayAll<_Rep, _Period>, std::placeholders::_1, duration));
585  }
586 
588  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
589  TaskContext& DelayAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
590  std::chrono::duration<_RepRight, _PeriodRight> const& max)
591  {
593  }
594 
596  template<class _Rep, class _Period>
597  TaskContext& DelayGroup(TaskScheduler::group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
598  {
599  return Dispatch(std::bind(&TaskScheduler::DelayGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
600  }
601 
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)
607  {
608  return DelayGroup(group, TaskScheduler::RandomDurationBetween(min, max));
609  }
610 
612  template<class _Rep, class _Period>
613  TaskContext& RescheduleAll(std::chrono::duration<_Rep, _Period> const& duration)
614  {
615  return Dispatch(std::bind(&TaskScheduler::RescheduleAll, std::placeholders::_1, duration));
616  }
617 
619  template<class _RepLeft, class _PeriodLeft, class _RepRight, class _PeriodRight>
620  TaskContext& RescheduleAll(std::chrono::duration<_RepLeft, _PeriodLeft> const& min,
621  std::chrono::duration<_RepRight, _PeriodRight> const& max)
622  {
624  }
625 
627  template<class _Rep, class _Period>
628  TaskContext& RescheduleGroup(TaskScheduler::group_t const group, std::chrono::duration<_Rep, _Period> const& duration)
629  {
630  return Dispatch(std::bind(&TaskScheduler::RescheduleGroup<_Rep, _Period>, std::placeholders::_1, group, duration));
631  }
632 
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)
638  {
639  return RescheduleGroup(group, TaskScheduler::RandomDurationBetween(min, max));
640  }
641 
642 private:
644  void AssertOnConsumed() const;
645 
647  void Invoke();
648 };
649 
650 #endif
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
Definition: SFMT.h:153
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
STL namespace.
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