17#include "AUI/Reflect/AEnumerate.h"
18#include <AUI/Thread/AMutex.h>
19#include <AUI/Thread/AConditionVariable.h>
20#include <AUI/Thread/IEventLoop.h>
21#include <AUI/Util/ABitField.h>
37 DONT_BLOCK_INFINITELY = 0b1,
46 DONT_BLOCK_TIMED = 0b10,
51 DONT_BLOCK = DONT_BLOCK_INFINITELY | DONT_BLOCK_TIMED,
60 using SchedulerDuration = std::chrono::microseconds;
63 std::chrono::milliseconds timeout;
64 std::chrono::high_resolution_clock::time_point nextExecution;
65 std::function<void()> callback;
69 std::chrono::high_resolution_clock::time_point executionTime;
70 std::function<void()> callback;
88 template<
typename Duration>
89 void enqueue(Duration timeout, std::function<
void()> callback) {
90 std::unique_lock lock(mSync);
92 std::chrono::duration_cast<SchedulerDuration>(timeout) + currentTime(),
95 enqueueTask(std::move(asTask));
107 template<
typename Duration>
109 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(timeout);
112 millis + currentTime(),
115 auto asTimer = _new<Timer>(std::move(t));
117 std::unique_lock lock(mSync);
118 mTimers.push_back(asTimer);
119 enqueueTimer(asTimer);
124 void removeTimer(
const TimerHandle& t);
127 bool emptyTasks() const noexcept {
128 return mTasks.empty();
140 bool mIsRunning =
false;
142 std::list<Task> mTasks;
143 std::list<_<Timer>> mTimers;
145 static std::chrono::high_resolution_clock::time_point currentTime() noexcept {
146 return std::chrono::high_resolution_clock::now();
149 void enqueueTimer(
const _<Timer>& timer) {
151 timer->nextExecution,
154 std::unique_lock lock(mSync);
159 timer->nextExecution += timer->timeout;
160 enqueueTask(std::move(t));
164 void enqueueTask(Task&& asTask) {
165 auto whereToInsert = std::find_if(mTasks.begin(), mTasks.end(), [&](
const Task& rhs) {
166 return asTask.executionTime < rhs.executionTime;
169 mTasks.insert(whereToInsert, std::move(asTask));
Bit field implementation.
Definition: ABitField.h:20
Represents a condition variable.
Definition: AConditionVariable.h:24
void notify_all() noexcept
Definition: AConditionVariable.h:62
Basic scheduler used for timers.
Definition: AScheduler.h:58
TimerHandle timer(Duration timeout, std::function< void()> callback)
Creates a timer.
Definition: AScheduler.h:108
Definition: IEventLoop.h:17
virtual void loop()=0
Do message processing loop.
virtual void notifyProcessMessages()=0
Notifies this IEventLoop that its thread got a new message to process.
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
@ NONE
Image is kept in it's original size.
AUI_ENUM_FLAG(ASide)
Describes sides of a 2D rectangle.
Definition: ASide.h:24
Basic syscall-based synchronization primitive.
Definition: AMutex.h:33
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:51