29#ifndef PT_SYSTEM_EVENTLOOP_H
30#define PT_SYSTEM_EVENTLOOP_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
84 friend class Selectable;
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
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;
186 virtual void onReady(Selectable&) = 0;
189 virtual void onCancel(Selectable&) = 0;
197class PT_SYSTEM_API EventQueue
204 virtual ~EventQueue();
207 {
return *_usedalloc; }
211 void pushEvent(
const Event& event);
219 std::deque<Event*> _eventQueue;
224class 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
261 virtual ~AsyncYield()
266 void await_resume()
noexcept
Allocator interface.
Definition Allocator.h:82
Provides the base class for I/O-driven co_await-able operations.
Definition Coroutine.h:126
void setReady()
Resumes the waiting coroutine.
Definition Coroutine.h:162
Connectable()
Default constructor.
Base class for typed events.
Definition Event.h:66
Multicast Signal to call multiple slots.
Definition Signal.h:190
Awaitable that schedules a resume in the EventLoop.
Definition EventLoop.h:254
void onDetach(Pt::System::EventLoop &) override
Detached from loop.
Definition EventLoop.h:288
void cancel() override
Cancels the pending operation.
Definition EventLoop.h:275
void onBegin() override
Starts the asynchronous operation.
Definition EventLoop.h:270
void onAttach(Pt::System::EventLoop &) override
Attached to loop.
Definition EventLoop.h:285
bool onRun() override
Check if ready and run.
Definition EventLoop.h:291
void onCancel() override
Aborts the in-flight operation.
Definition EventLoop.h:280
Event loop of a thread or process.
Definition EventLoop.h:83
void run()
Starts the loop.
Signal< const Event & > & eventReceived()
Reports all events.
Definition EventLoop.h:114
AsyncYield yieldAsync()
Pauses a coroutine and delegates execution back to the loop.
Definition EventLoop.h:298
virtual void onQueueEvent(const Event &ev)=0
Only queues an event.
void exit()
Stops the loop.
virtual void onCommitEvent(const Event &ev)=0
Queues an event and triggers event processing.
void setReady(Selectable &s)
Sets the Selectable as ready without waking the loop.
Definition EventLoop.h:143
void post(Selectable &s)
Posts the loop to run a selectable.
Definition EventLoop.h:136
virtual void onWake()=0
Triggers event processing.
virtual ~EventLoop()
Destructor.
void processEvents()
Process all queued events.
Signal & exited()
Emited when the eventloop is exited.
Definition EventLoop.h:119
EventSink()
Default Constructor.
void wake()
Triggers event processing.
Operation that runs through an event loop.
Definition Selectable.h:60
void cancel()
Cancels all operations.
void setActive(EventLoop &parent)
Sets the parent loop, so that operations can be run.
void post()
Posts this selectable to its event loop from any thread.
Interval timeout notifications.
Definition Timer.h:74
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33