36using namespace std::chrono;
55 std::lock_guard<std::mutex> lock(
mtx);
60 <<
"TimeoutQueue destructor called without prior shutdown(). "
61 <<
"Invoking shutdown() automatically." << std::endl;
86 <<
"event nsec out of range: " <<
event->time_key().tv_nsec;
88 std::lock_guard<std::mutex> lock(
mtx);
93 <<
"Event has already been inserted in timeoutQueue";
113 std::lock_guard<std::mutex> lock(
mtx);
116 <<
"Event " <<
event <<
" not found in timeoutQueue";
121 callback =
event->on_completed;
147 if (
event ==
nullptr)
155 std::lock_guard<std::mutex> lock(
mtx);
158 <<
"Event " <<
local <<
" not found in timeoutQueue";
186 callback =
local->on_completed;
213 std::lock_guard<std::mutex> lock(
mtx);
216 <<
"Event " <<
event <<
" not found in timeoutQueue";
236 std::unique_lock<std::mutex> lock(
mtx);
255 const auto sys_now = system_clock::now();
260 if (delta < system_clock::duration::zero())
261 delta = system_clock::duration::zero();
298 ah_warning(std::cerr) <<
"Uncaught exception in TimeoutQueue event execution (ID "
370 std::lock_guard<std::mutex> lock(
mtx);
376 std::lock_guard<std::mutex> lock(
mtx);
382 std::lock_guard<std::mutex> lock(
mtx);
388 std::lock_guard<std::mutex> lock(
mtx);
400 std::lock_guard<std::mutex> lock(
mtx);
408 std::unique_lock<std::mutex> lock(
mtx);
434 std::lock_guard<std::mutex> lock(
mtx);
440 std::lock_guard<std::mutex> lock(
mtx);
446 std::lock_guard<std::mutex> lock(
mtx);
453 std::lock_guard<std::mutex> lock(
mtx);
459 std::lock_guard<std::mutex> lock(
mtx);
466 std::lock_guard<std::mutex> lock(
mtx);
472 std::unique_lock<std::mutex> lock(
mtx);
492 std::lock_guard<std::mutex> lock(
mtx);
507 Event *
event =
nullptr;
509 std::lock_guard<std::mutex> lock(
mtx);
519 callback =
event->on_completed;
Exception handling system with formatted messages for Aleph-w.
#define ah_warning(out)
Emits an unconditional warning to a stream.
#define ah_invalid_argument_unless(C)
Throws std::invalid_argument if condition does NOT hold.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Time time_plus_msec(const Time ¤t_time, const int &msec)
T remove()
Remove the first item of the list.
Node * getMin()
Elimina del heap el nodo de menor prioridad.
Node * remove(Node *node)
Elimina del heap el nodo node.
Node * insert(Node *p) noexcept
Inserta un nodo en un heap.
Node * top()
Retorna el nodo con menor prioridad según el criterio de comparación especificado en la declaración.
const size_t & size() const noexcept
Base class for scheduled events.
CompletionCallback on_completed
Execution_Status
Possible states of an event in its lifecycle.
@ Executing
Currently executing EventFct()
@ Canceled
Removed from queue before execution.
@ Executed
Completed execution.
@ In_Queue
Scheduled and waiting for trigger time.
@ To_Delete
Marked for cleanup.
static std::atomic< EventId > nextId
std::function< void(Event *, Execution_Status)> CompletionCallback
Optional callback invoked after an event completes, is canceled, or is deleted.
uint64_t EventId
Type for unique event identifiers.
static constexpr EventId InvalidId
Invalid/null event ID.
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.
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
void next()
Advance all underlying iterators (bounds-checked).
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
static auto timespec_to_timepoint(const Time &t)
Priority queue for scheduling timed events.