|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Thread-safe priority queue for scheduling timed events. More...
#include <timeoutQueue.H>
Classes | |
| class | Event |
| Base class for scheduled events. More... | |
Public Member Functions | |
| TimeoutQueue () | |
| Default constructor - starts the background thread. | |
| ~TimeoutQueue () | |
| Destructor - shuts down the queue and joins the thread. | |
| void | schedule_event (const Time &trigger_time, Event *) |
| Schedule an event at a specific time. | |
| void | schedule_event (Event *event) |
| Schedule an event using its internal trigger time. | |
| bool | cancel_event (Event *event) |
| Cancel a scheduled event. | |
| void | reschedule_event (const Time &trigger_time, Event *event) |
| Reschedule an event to a new time. | |
| void | cancel_delete_event (Event *&event) |
| Cancel and delete an event. | |
| void | shutdown () |
| Shut down the queue and stop the background thread. | |
| std::thread::id | getThreadId () const |
| Get the background thread ID. | |
| size_t | size () const |
| Get the number of pending events in the queue. | |
| bool | is_empty () const |
| Check if the queue has no pending events. | |
| bool | is_running () const |
| Check if the queue is running (not shut down) | |
| void | schedule_after_ms (int ms_from_now, Event *event) |
| Schedule an event relative to the current time. | |
| Time | next_event_time () const |
| Get the trigger time of the next (soonest) event. | |
| size_t | clear_all () |
| Cancel all pending events in the queue. | |
| size_t | executed_count () const |
| Get count of events that have been executed. | |
| size_t | canceled_count () const |
| Get count of events that have been canceled. | |
| void | reset_stats () |
| Reset statistics counters to zero. | |
| void | pause () |
| Pause event execution (events remain scheduled but won't execute) | |
| void | resume () |
| Resume event execution after pause. | |
| bool | is_paused () const |
| Check if the queue is paused. | |
| bool | wait_until_empty (int timeout_ms=0) |
| Block until all pending events have been executed or canceled. | |
| Event * | find_by_id (const Event::EventId id) const |
| Find a scheduled event by its ID. | |
| bool | cancel_by_id (Event::EventId id) |
| Cancel a scheduled event by its ID. | |
Private Member Functions | |
| void | triggerEvent () |
| void | shutdown_locked () |
Private Attributes | |
| BinHeapVtl< Time > | prio_queue |
| DynMapTree< Event::EventId, Event * > | event_map |
| DynSetTree< Event * > | event_registry |
| std::mutex | mtx |
| std::condition_variable | cond |
| std::thread | workerThread |
| bool | isShutdown |
| bool | isPaused = false |
| size_t | executedCount = 0 |
| size_t | canceledCount = 0 |
| std::condition_variable | emptyCondition |
Thread-safe priority queue for scheduling timed events.
Manages a collection of events that should be triggered at specific absolute times. A background thread monitors the queue and executes events when their scheduled time arrives.
All public methods are thread-safe and can be called from any thread. The background thread handles event execution. *
Definition at line 140 of file timeoutQueue.H.
| TimeoutQueue::TimeoutQueue | ( | ) |
Default constructor - starts the background thread.
Definition at line 48 of file timeoutQueue.C.
References triggerEvent(), and workerThread.
| TimeoutQueue::~TimeoutQueue | ( | ) |
Destructor - shuts down the queue and joins the thread.
Definition at line 53 of file timeoutQueue.C.
References ah_warning, isShutdown, Aleph::maps(), mtx, shutdown_locked(), and workerThread.
| bool TimeoutQueue::cancel_by_id | ( | Event::EventId | id | ) |
Cancel a scheduled event by its ID.
| id | The event ID to cancel |
Definition at line 501 of file timeoutQueue.C.
References TimeoutQueue::Event::Canceled, canceledCount, cond, emptyCondition, event_map, event_registry, TimeoutQueue::Event::In_Queue, TimeoutQueue::Event::InvalidId, Aleph::maps(), mtx, prio_queue, Aleph::GenBinHeap< NodeType, Key, Compare >::remove(), and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
Cancel and delete an event.
If the event is currently executing, its status is set to To_Delete and the worker thread will delete it after EventFct() returns. Otherwise, the event is deleted immediately before invoking the completion callback.
The completion callback receives nullptr as the event pointer and Deleted as the status, since the event has already been destroyed.
| event | Pointer to event (set to nullptr after deletion). No exception is thrown for the nullptr argument (early return). |
Definition at line 145 of file timeoutQueue.C.
References ah_invalid_argument_unless, canceledCount, cond, TimeoutQueue::Event::Deleted, emptyCondition, event_map, event_registry, TimeoutQueue::Event::Executing, TimeoutQueue::Event::In_Queue, Aleph::maps(), mtx, prio_queue, Aleph::DynList< T >::remove(), Aleph::GenBinHeap< NodeType, Key, Compare >::remove(), Aleph::GenBinHeap< NodeType, Key, Compare >::size(), and TimeoutQueue::Event::To_Delete.
Referenced by demo_cancel_delete(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
| bool TimeoutQueue::cancel_event | ( | TimeoutQueue::Event * | event | ) |
Cancel a scheduled event.
| event | Event to cancel |
Definition at line 106 of file timeoutQueue.C.
References ah_invalid_argument_if, ah_invalid_argument_unless, TimeoutQueue::Event::Canceled, canceledCount, cond, emptyCondition, event_map, event_registry, TimeoutQueue::Event::In_Queue, Aleph::maps(), mtx, prio_queue, Aleph::GenBinHeap< NodeType, Key, Compare >::remove(), and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
Referenced by demo_cancellation(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
| size_t TimeoutQueue::canceled_count | ( | ) | const |
Get count of events that have been canceled.
Definition at line 438 of file timeoutQueue.C.
References canceledCount, and mtx.
| size_t TimeoutQueue::clear_all | ( | ) |
Cancel all pending events in the queue.
Definition at line 406 of file timeoutQueue.C.
References TimeoutQueue::Event::Canceled, canceledCount, cond, Aleph::count(), emptyCondition, event_map, event_registry, Aleph::GenBinHeap< NodeType, Key, Compare >::getMin(), Aleph::maps(), mtx, TimeoutQueue::Event::on_completed, prio_queue, and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
| size_t TimeoutQueue::executed_count | ( | ) | const |
Get count of events that have been executed.
Definition at line 432 of file timeoutQueue.C.
References executedCount, and mtx.
| TimeoutQueue::Event * TimeoutQueue::find_by_id | ( | const Event::EventId | id | ) | const |
Find a scheduled event by its ID.
| id | The event ID to search for |
Definition at line 487 of file timeoutQueue.C.
References event_map, TimeoutQueue::Event::In_Queue, TimeoutQueue::Event::InvalidId, Aleph::maps(), and mtx.
Referenced by TEST().
|
inline |
Get the background thread ID.
Definition at line 371 of file timeoutQueue.H.
References workerThread.
| bool TimeoutQueue::is_empty | ( | ) | const |
Check if the queue has no pending events.
Definition at line 380 of file timeoutQueue.C.
References mtx, prio_queue, and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
| bool TimeoutQueue::is_paused | ( | ) | const |
| bool TimeoutQueue::is_running | ( | ) | const |
Check if the queue is running (not shut down)
Definition at line 386 of file timeoutQueue.C.
References isShutdown, Aleph::maps(), and mtx.
| Time TimeoutQueue::next_event_time | ( | ) | const |
Get the trigger time of the next (soonest) event.
Definition at line 398 of file timeoutQueue.C.
References mtx, prio_queue, Aleph::GenBinHeap< NodeType, Key, Compare >::size(), and Aleph::GenBinHeap< NodeType, Key, Compare >::top().
Referenced by TEST().
| void TimeoutQueue::pause | ( | ) |
Pause event execution (events remain scheduled but won't execute)
Definition at line 451 of file timeoutQueue.C.
Referenced by TEST().
| void TimeoutQueue::reschedule_event | ( | const Time & | trigger_time, |
| TimeoutQueue::Event * | event | ||
| ) |
Reschedule an event to a new time.
| trigger_time | New absolute trigger time |
| event | Event to reschedule * |
Definition at line 207 of file timeoutQueue.C.
References ah_invalid_argument_if, ah_invalid_argument_unless, cond, event_map, event_registry, TimeoutQueue::Event::In_Queue, Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), isShutdown, Aleph::maps(), mtx, prio_queue, and Aleph::GenBinHeap< NodeType, Key, Compare >::remove().
Referenced by demo_rescheduling(), PeriodicEvent::EventFct(), ReschedulingEvent::EventFct(), TEST(), TEST(), TEST(), and TEST().
| void TimeoutQueue::reset_stats | ( | ) |
Reset statistics counters to zero.
Definition at line 444 of file timeoutQueue.C.
References canceledCount, executedCount, and mtx.
| void TimeoutQueue::resume | ( | ) |
Schedule an event relative to the current time.
| ms_from_now | Milliseconds from now to trigger |
| event | Event to schedule |
Definition at line 392 of file timeoutQueue.C.
References Aleph::maps(), read_current_time(), schedule_event(), and time_plus_msec().
Referenced by TEST().
| void TimeoutQueue::schedule_event | ( | const Time & | trigger_time, |
| TimeoutQueue::Event * | event | ||
| ) |
Schedule an event at a specific time.
| trigger_time | Absolute trigger time |
| event | Event to schedule * |
Definition at line 71 of file timeoutQueue.C.
References ah_invalid_argument_if, Aleph::maps(), and schedule_event().
Referenced by demo_callback_event(), demo_cancel_delete(), demo_cancellation(), demo_event_status(), demo_periodic_event(), demo_priority_ordering(), demo_rescheduling(), demo_simple_event(), schedule_after_ms(), schedule_event(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
| void TimeoutQueue::schedule_event | ( | TimeoutQueue::Event * | event | ) |
Schedule an event using its internal trigger time.
| event | Event to schedule (uses event's getAbsoluteTime()) * |
Definition at line 81 of file timeoutQueue.C.
References ah_domain_error_if, ah_invalid_argument_if, cond, event_map, event_registry, TimeoutQueue::Event::In_Queue, Aleph::GenBinHeap< NodeType, Key, Compare >::insert(), isShutdown, Aleph::maps(), mtx, NSEC, and prio_queue.
| void TimeoutQueue::shutdown | ( | ) |
Shut down the queue and stop the background thread.
Definition at line 368 of file timeoutQueue.C.
References mtx, and shutdown_locked().
Referenced by main(), TimeoutQueueEnvironment::TearDown(), and TEST().
|
inlineprivate |
Definition at line 296 of file timeoutQueue.H.
References cond, and isShutdown.
Referenced by ~TimeoutQueue(), and shutdown().
| size_t TimeoutQueue::size | ( | ) | const |
Get the number of pending events in the queue.
Definition at line 374 of file timeoutQueue.C.
References mtx, prio_queue, and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
|
private |
Definition at line 234 of file timeoutQueue.C.
References ah_warning, TimeoutQueue::Event::Canceled, canceledCount, cond, TimeoutQueue::Event::Deleted, emptyCondition, event_map, event_registry, TimeoutQueue::Event::Executed, executedCount, TimeoutQueue::Event::Executing, Aleph::GenBinHeap< NodeType, Key, Compare >::getMin(), TimeoutQueue::Event::In_Queue, isPaused, isShutdown, Aleph::maps(), mtx, Aleph::next(), TimeoutQueue::Event::on_completed, prio_queue, Aleph::DynList< T >::remove(), Aleph::GenBinHeap< NodeType, Key, Compare >::size(), timespec_to_timepoint(), TimeoutQueue::Event::To_Delete, and Aleph::GenBinHeap< NodeType, Key, Compare >::top().
Referenced by TimeoutQueue().
Block until all pending events have been executed or canceled.
| timeout_ms | Maximum time to wait in milliseconds (0 = wait forever) |
Definition at line 470 of file timeoutQueue.C.
References emptyCondition, isShutdown, Aleph::maps(), mtx, prio_queue, and Aleph::GenBinHeap< NodeType, Key, Compare >::size().
|
private |
Definition at line 289 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), canceled_count(), clear_all(), reset_stats(), and triggerEvent().
|
private |
Definition at line 282 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), clear_all(), reschedule_event(), resume(), schedule_event(), shutdown_locked(), and triggerEvent().
|
private |
Definition at line 291 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), clear_all(), triggerEvent(), and wait_until_empty().
|
private |
Definition at line 278 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), clear_all(), find_by_id(), reschedule_event(), schedule_event(), and triggerEvent().
|
private |
Definition at line 279 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), clear_all(), reschedule_event(), schedule_event(), and triggerEvent().
|
private |
Definition at line 288 of file timeoutQueue.H.
Referenced by executed_count(), reset_stats(), and triggerEvent().
Definition at line 286 of file timeoutQueue.H.
Referenced by is_paused(), pause(), resume(), and triggerEvent().
|
private |
Definition at line 285 of file timeoutQueue.H.
Referenced by ~TimeoutQueue(), is_running(), reschedule_event(), schedule_event(), shutdown_locked(), triggerEvent(), and wait_until_empty().
|
mutableprivate |
Definition at line 281 of file timeoutQueue.H.
Referenced by ~TimeoutQueue(), cancel_by_id(), cancel_delete_event(), cancel_event(), canceled_count(), clear_all(), executed_count(), find_by_id(), is_empty(), is_paused(), is_running(), next_event_time(), pause(), reschedule_event(), reset_stats(), resume(), schedule_event(), shutdown(), size(), triggerEvent(), and wait_until_empty().
|
private |
Definition at line 277 of file timeoutQueue.H.
Referenced by cancel_by_id(), cancel_delete_event(), cancel_event(), clear_all(), is_empty(), next_event_time(), reschedule_event(), schedule_event(), size(), triggerEvent(), and wait_until_empty().
|
private |
Definition at line 283 of file timeoutQueue.H.
Referenced by TimeoutQueue(), ~TimeoutQueue(), and getThreadId().