libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
eventloop.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <variant>
11#include <functional>
13#include <promeki/core/atomic.h>
14#include <promeki/core/queue.h>
15#include <promeki/core/list.h>
17#include <promeki/core/event.h>
18
20
21class ObjectBase;
22
58class EventLoop {
59 public:
66
71 static EventLoop *current();
72
81
90
91 EventLoop(const EventLoop &) = delete;
92 EventLoop &operator=(const EventLoop &) = delete;
93
98 int exec();
99
109 void processEvents(uint32_t flags = 0, unsigned int timeoutMs = 0);
110
119 void quit(int returnCode = 0);
120
129 void postCallable(std::function<void()> func);
130
141 void postEvent(ObjectBase *receiver, Event *event);
142
155 int startTimer(ObjectBase *receiver, unsigned int intervalMs,
156 bool singleShot = false);
157
169 int startTimer(unsigned int intervalMs,
170 std::function<void()> func,
171 bool singleShot = false);
172
177 void stopTimer(int timerId);
178
183 bool isRunning() const { return _running.value(); }
184
189 int exitCode() const { return _exitCode.value(); }
190
191 private:
192 struct CallableItem { std::function<void()> func; };
193 struct EventItem { ObjectBase *receiver; Event *event; };
194 struct QuitItem { int code; };
195 using Item = std::variant<CallableItem, EventItem, QuitItem>;
196
197 struct TimerInfo {
198 int id;
199 ObjectBase *receiver;
200 std::function<void()> func;
201 unsigned int intervalMs;
202 bool singleShot;
203 TimeStamp nextFire;
204 };
205
206 static thread_local EventLoop *_current;
207
208 Queue<Item> _queue;
209 Atomic<bool> _running;
210 Atomic<int> _exitCode;
211 List<TimerInfo> _timers;
212 Atomic<int> _nextTimerId{1};
213
214 bool dispatchItem(Item &item);
215 void processTimers();
216 unsigned int nextTimerTimeout() const;
217};
218
T value() const
Loads the current value with acquire semantics.
Definition atomic.h:46
Per-thread event loop providing event dispatch, timers, and posted callables.
Definition eventloop.h:58
void stopTimer(int timerId)
Stops and removes a timer.
ProcessEventsFlag
Flags controlling processEvents() behavior.
Definition eventloop.h:61
@ ExcludeTimers
Skip timer processing.
Definition eventloop.h:62
@ WaitForMore
Block until at least one event is available.
Definition eventloop.h:64
@ ExcludePosted
Skip posted callables and events.
Definition eventloop.h:63
EventLoop()
Constructs an EventLoop and registers it as current for this thread.
static EventLoop * current()
Returns the EventLoop running on the current thread.
void postCallable(std::function< void()> func)
Posts a callable to be executed on this EventLoop's thread.
int startTimer(unsigned int intervalMs, std::function< void()> func, bool singleShot=false)
Starts a standalone callable timer.
void quit(int returnCode=0)
Signals the event loop to exit with the given return code.
int startTimer(ObjectBase *receiver, unsigned int intervalMs, bool singleShot=false)
Starts a timer that delivers TimerEvent to an ObjectBase.
void postEvent(ObjectBase *receiver, Event *event)
Posts an Event to this EventLoop for delivery to a receiver.
int exitCode() const
Returns the exit code set by the most recent quit().
Definition eventloop.h:189
~EventLoop()
Destroys the EventLoop.
bool isRunning() const
Returns whether the event loop is currently running.
Definition eventloop.h:183
void processEvents(uint32_t flags=0, unsigned int timeoutMs=0)
Processes pending events and returns.
int exec()
Blocks and processes events until quit() is called.
Base class for the event system.
Definition event.h:29
Dynamic array container wrapping std::vector.
Definition list.h:40
Base object for promeki.
Definition objectbase.h:129
A monotonic timestamp based on std::chrono::steady_clock.
Definition timestamp.h:32
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19