SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
|
Thread-safe unified queue for messages and function tasks with timed execution. More...
#include <EventQueue.h>
Classes | |
struct | QueueItem |
Unified queue item supporting both messages and function tasks. More... | |
Public Types | |
enum class | QueueItemType { MESSAGE , FUNCTION } |
Type discriminator for queue items. More... | |
Public Member Functions | |
EventQueue () | |
Constructor - initializes empty queue. | |
~EventQueue () | |
Destructor - cleanup remaining items. | |
bool | enqueueMessage (const std::shared_ptr< Message > &message, int64_t whenUs) |
Enqueue traditional message for timed execution. | |
std::shared_ptr< Message > | poll () |
Poll for next ready message (legacy compatibility) | |
bool | isQuit () |
Check if quit message was received. | |
template<typename F , typename... Args> | |
auto | enqueueFunction (F &&func, Args &&... args) -> std::future< decltype(func(args...))> |
Enqueue function for immediate asynchronous execution. | |
template<typename F , typename... Args> | |
auto | enqueueFunctionDelayed (int64_t delayMs, F &&func, Args &&... args) -> std::future< decltype(func(args...))> |
Enqueue function for delayed asynchronous execution. | |
template<typename T > | |
Promise< T > | enqueuePromise () |
Create and enqueue promise for manual resolution. | |
std::optional< QueueItem > | pollNext () |
Poll for next ready queue item (unified interface) | |
void | quit () |
Request queue shutdown. | |
bool | hasMessage (const std::shared_ptr< Handler > &h, int32_t what, void *obj) |
Check for specific message in queue. | |
int64_t | uptimeMicros () |
Get current system uptime in microseconds. | |
Thread-safe unified queue for messages and function tasks with timed execution.
EventQueue provides a unified interface for queuing both traditional messages and modern function tasks with support for immediate and delayed execution. Key features:
The queue maintains chronological order for timed items while supporting immediate execution for urgent tasks. Uses type erasure to store different function signatures uniformly while preserving type safety at the API level.
Architecture:
Definition at line 85 of file EventQueue.h.
|
strong |
Type discriminator for queue items.
Identifies whether a queue item contains a traditional message or a modern function task for proper handling during polling.
Enumerator | |
---|---|
MESSAGE | Traditional message with handler. |
FUNCTION | Function task with std::packaged_task. |
Definition at line 95 of file EventQueue.h.
swt::EventQueue::EventQueue | ( | ) |
Constructor - initializes empty queue.
Creates an empty event queue with initialized mutex and condition variable for thread-safe operations.
Definition at line 13 of file EventQueue.cpp.
swt::EventQueue::~EventQueue | ( | ) |
Destructor - cleanup remaining items.
Cleans up any remaining messages and function tasks in the queue. Note that pending futures may become invalid after destruction.
Definition at line 19 of file EventQueue.cpp.
auto swt::EventQueue::enqueueFunction | ( | F && | func, |
Args &&... | args | ||
) | -> std::future< decltype(func(args...))> |
Enqueue function for immediate asynchronous execution.
F | Function type (auto-deduced) |
Args | Variadic argument types (auto-deduced) |
func | Callable object to execute asynchronously |
args | Arguments to forward to the function |
Enqueues a function for immediate execution at the front of the queue. Uses perfect forwarding to preserve argument types and std::packaged_task for type-safe result retrieval through futures.
auto swt::EventQueue::enqueueFunctionDelayed | ( | int64_t | delayMs, |
F && | func, | ||
Args &&... | args | ||
) | -> std::future< decltype(func(args...))> |
Enqueue function for delayed asynchronous execution.
F | Function type (auto-deduced) |
Args | Variadic argument types (auto-deduced) |
delayMs | Delay in milliseconds before execution |
func | Callable object to execute asynchronously |
args | Arguments to forward to the function |
Enqueues a function for delayed execution, maintaining chronological order with other timed items. Uses binary search insertion for efficient ordering without full queue sorting.
bool swt::EventQueue::enqueueMessage | ( | const std::shared_ptr< Message > & | message, |
int64_t | whenUs | ||
) |
Enqueue traditional message for timed execution.
message | Shared pointer to message object |
whenUs | Absolute execution time in microseconds |
Enqueues a traditional message for execution at the specified time. Messages are inserted in chronological order using binary search for efficient ordering without full sorting.
Definition at line 31 of file EventQueue.cpp.
template swt::Promise< void > swt::EventQueue::enqueuePromise< void > | ( | ) |
Create and enqueue promise for manual resolution.
T | Value type for the promise |
Creates a new promise that can be resolved manually from any thread. The promise callbacks will execute in the event loop thread when the promise is resolved or rejected.
bool swt::EventQueue::hasMessage | ( | const std::shared_ptr< Handler > & | h, |
int32_t | what, | ||
void * | obj | ||
) |
Check for specific message in queue.
Legacy method for checking if a specific message type is queued. Used for message deduplication and state checking.
Definition at line 152 of file EventQueue.cpp.
References MESSAGE.
bool swt::EventQueue::isQuit | ( | ) |
Check if quit message was received.
Legacy method for checking quit state in message-based loops.
Definition at line 52 of file EventQueue.cpp.
std::shared_ptr< Message > swt::EventQueue::poll | ( | ) |
Poll for next ready message (legacy compatibility)
Legacy method that polls only for messages, ignoring function tasks. Provided for backward compatibility with existing message-based code.
Definition at line 142 of file EventQueue.cpp.
References MESSAGE, and pollNext().
std::optional< EventQueue::QueueItem > swt::EventQueue::pollNext | ( | ) |
Poll for next ready queue item (unified interface)
Unified polling method that returns the next ready item regardless of type (message or function). Checks timing and only returns items whose execution time has arrived. Blocks until an item is ready or the queue is quit.
Definition at line 56 of file EventQueue.cpp.
References uptimeMicros().
Referenced by poll().
void swt::EventQueue::quit | ( | ) |
Request queue shutdown.
Signals the queue to stop processing and causes polling methods to return null/empty to exit event loop gracefully.
Definition at line 170 of file EventQueue.cpp.
int64_t swt::EventQueue::uptimeMicros | ( | ) |
Get current system uptime in microseconds.
Utility method providing high-precision timing for queue scheduling. Uses steady_clock to avoid issues with system time adjustments.
Definition at line 179 of file EventQueue.cpp.
Referenced by pollNext().