29 #ifndef PT_SYSTEM_EVENTLOOP_H
30 #define PT_SYSTEM_EVENTLOOP_H
33 #include <Pt/Signal.h>
34 #include <Pt/Timespan.h>
35 #include <Pt/Allocator.h>
36 #include <Pt/Connectable.h>
37 #include <Pt/System/Api.h>
38 #include <Pt/System/Mutex.h>
39 #include <Pt/System/Timer.h>
40 #include <Pt/System/EventSink.h>
44 #if __cplusplus >= 202002L
45 #include <Pt/Coroutine.h>
46 #include <Pt/System/Selectable.h>
57 #if __cplusplus >= 202002L
90 static const std::size_t WaitInfinite =
static_cast<const std::size_t
>(-1);
94 static const std::size_t WaitMax = WaitInfinite - 1;
122 #if __cplusplus >= 202002L
126 AsyncYield yieldAsync();
149 virtual Selector& selector() = 0;
156 virtual void onRun() = 0;
159 virtual void onExit() = 0;
171 virtual void onProcessEvents() = 0;
174 virtual void onAttachTimer(
Timer& timer) = 0;
177 virtual void onDetachTimer(
Timer& timer) = 0;
180 virtual void onAttachSelectable(
Selectable&) = 0;
183 virtual void onDetachSelectable(
Selectable&) = 0;
197 class PT_SYSTEM_API EventQueue
204 virtual ~EventQueue();
207 {
return *_usedalloc; }
211 void pushEvent(
const Event& event);
219 std::deque<Event*> _eventQueue;
224 class PT_SYSTEM_API TimerQueue
226 typedef std::multimap<Timespan, Timer*> TimerMap;
231 virtual ~TimerQueue();
233 void addTimer(Timer& timer);
235 void removeTimer(Timer& timer);
237 std::size_t processTimers();
243 #if __cplusplus >= 202002L
251 class PT_SYSTEM_API AsyncYield :
public Pt::Awaiter
255 explicit AsyncYield(EventLoop& loop)
257 this->setActive(loop);
260 virtual ~AsyncYield()
265 void await_resume() noexcept
269 void onBegin()
override
274 void cancel()
override
279 void onCancel()
override
290 bool onRun()
override
297 inline AsyncYield EventLoop::yieldAsync()
299 return AsyncYield(*
this);
302 #endif // __cplusplus >= 202002L
308 #endif // PT_SYSTEM_EVENTLOOP_H
Core module.
Definition: Allocator.h:33
Base class for all event types.
Definition: Event.h:50
void run()
Starts the loop.
Notifies clients in constant intervals.
Definition: Timer.h:73
Signal & exited()
Emited when the eventloop is exited.
Definition: EventLoop.h:119
Multicast Signal to call multiple slots.
Definition: Api-Signal.h:111
void processEvents()
Process all queued events.
void cancel()
Cancels all operations.
virtual void onQueueEvent(const Event &ev)=0
Only queues an event.
void setReady(Selectable &s)
Sets the Selectable as ready without waking the loop.
Definition: EventLoop.h:143
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
void exit()
Stops the loop.
virtual void onWake()=0
Triggers event processing.
Dispatches operations through an event loop.
Definition: Selectable.h:45
Allocator interface.
Definition: Allocator.h:82
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:83
virtual ~EventLoop()
Destructor.
virtual void onCommitEvent(const Event &ev)=0
Queues an event and triggers event processing.
void post(Selectable &s)
Posts the loop to run a selectable.
Definition: EventLoop.h:136
Receiver for events.
Definition: EventSink.h:46
Signal< const Event & > & eventReceived()
Reports all events.
Definition: EventLoop.h:114