103# ifndef TIMEOUTQUEUE_H
104# define TIMEOUTQUEUE_H
111# include <condition_variable>
114# include <functional>
188 static std::atomic<EventId>
nextId;
238 <<
"FATAL: Deleting Event " <<
this <<
" (ID " <<
event_id <<
", name: '"
239 <<
event_name <<
"') that is still In_Queue. "
240 <<
"This causes use-after-free when the worker thread accesses it. "
241 <<
"Must call cancel_event() or use cancel_delete_event() before destroying.";
Exception handling system with formatted messages for Aleph-w.
#define ah_warning(out)
Emits an unconditional warning to a stream.
Low-level time utilities using POSIX timespec.
WeightedDigraph::Node Node
Generic key-value map implemented on top of a binary search tree.
Dynamic set backed by balanced binary search trees with automatic memory management.
Base class for scheduled events.
Event(const Time &t, std::string name="")
CompletionCallback on_completed
void set_trigger_time(const Time _t)
const std::string & get_name() const
Get the event name (for debugging)
Time getAbsoluteTime() const
void set_execution_status(Execution_Status status)
~Event() noexcept override
Execution_Status
Possible states of an event in its lifecycle.
@ Executing
Currently executing EventFct()
@ Out_Queue
Not currently in any queue.
@ Canceled
Removed from queue before execution.
@ Executed
Completed execution.
@ In_Queue
Scheduled and waiting for trigger time.
@ To_Delete
Marked for cleanup.
std::atomic< Execution_Status > execution_status
Execution_Status get_execution_status() const
const Time & time_key() const
static std::atomic< EventId > nextId
std::function< void(Event *, Execution_Status)> CompletionCallback
Optional callback invoked after an event completes, is canceled, or is deleted.
Event(const long sec, const long nsec, std::string name="")
void set_completion_callback(CompletionCallback cb)
Set the completion callback (called after EventFct completes or the event is canceled)
virtual void EventFct()=0
Event handler function to be overridden.
void invoke_completion_callback()
void set_name(const std::string &name)
Set the event name.
uint64_t EventId
Type for unique event identifiers.
static constexpr EventId InvalidId
Invalid/null event ID.
EventId get_id() const
Get the unique event ID (auto-generated on construction)
Thread-safe priority queue for scheduling timed events.
std::condition_variable cond
size_t executed_count() const
Get count of events that have been executed.
void reschedule_event(const Time &trigger_time, Event *event)
Reschedule an event to a new time.
void reset_stats()
Reset statistics counters to zero.
DynMapTree< Event::EventId, Event * > event_map
size_t size() const
Get the number of pending events in the queue.
void schedule_event(const Time &trigger_time, Event *)
Schedule an event at a specific time.
~TimeoutQueue()
Destructor - shuts down the queue and joins the thread.
bool is_running() const
Check if the queue is running (not shut down)
Time next_event_time() const
Get the trigger time of the next (soonest) event.
bool cancel_by_id(Event::EventId id)
Cancel a scheduled event by its ID.
std::thread::id getThreadId() const
Get the background thread ID.
void resume()
Resume event execution after pause.
size_t canceled_count() const
Get count of events that have been canceled.
void pause()
Pause event execution (events remain scheduled but won't execute)
void cancel_delete_event(Event *&event)
Cancel and delete an event.
void shutdown()
Shut down the queue and stop the background thread.
bool wait_until_empty(int timeout_ms=0)
Block until all pending events have been executed or canceled.
void schedule_after_ms(int ms_from_now, Event *event)
Schedule an event relative to the current time.
bool is_paused() const
Check if the queue is paused.
Event * find_by_id(const Event::EventId id) const
Find a scheduled event by its ID.
BinHeapVtl< Time > prio_queue
bool cancel_event(Event *event)
Cancel a scheduled event.
size_t clear_all()
Cancel all pending events in the queue.
TimeoutQueue()
Default constructor - starts the background thread.
bool is_empty() const
Check if the queue has no pending events.
std::condition_variable emptyCondition
DynSetTree< Event * > event_registry
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sec_function > > sec(const __gmp_expr< T, U > &expr)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Heap of nodes with virtual destroyer.
Binary heap implementation using tree structure.
Dynamic key-value map based on balanced binary search trees.
Dynamic set implementations based on balanced binary search trees.