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);
124 callback =
event->on_completed;
150 if (event ==
nullptr)
153 Event *local = event;
158 std::lock_guard<std::mutex> lock(
mtx);
161 <<
"Event " << local <<
" not found in timeoutQueue";
216 std::lock_guard<std::mutex> lock(
mtx);
219 <<
"Event " <<
event <<
" not found in timeoutQueue";
239 std::unique_lock<std::mutex> lock(
mtx);
258 const auto sys_now = system_clock::now();
263 if (delta < system_clock::duration::zero())
264 delta = system_clock::duration::zero();
301 ah_warning(std::cerr) <<
"Uncaught exception in TimeoutQueue event execution (ID "
373 std::lock_guard<std::mutex> lock(
mtx);
379 std::lock_guard<std::mutex> lock(
mtx);
385 std::lock_guard<std::mutex> lock(
mtx);
391 std::lock_guard<std::mutex> lock(
mtx);
403 std::lock_guard<std::mutex> lock(
mtx);
411 std::unique_lock<std::mutex> lock(
mtx);
437 std::lock_guard<std::mutex> lock(
mtx);
443 std::lock_guard<std::mutex> lock(
mtx);
449 std::lock_guard<std::mutex> lock(
mtx);
456 std::lock_guard<std::mutex> lock(
mtx);
462 std::lock_guard<std::mutex> lock(
mtx);
469 std::lock_guard<std::mutex> lock(
mtx);
475 std::unique_lock<std::mutex> lock(
mtx);
495 std::lock_guard<std::mutex> lock(
mtx);
510 Event *
event =
nullptr;
512 std::lock_guard<std::mutex> lock(
mtx);
522 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)
Node * getMin()
Removes the node with the lowest priority from the heap.
Node * remove(Node *node)
Removes node from the heap.
Node * insert(Node *p) noexcept
Inserts a node into a heap.
Node * top()
Returns the node with the lowest priority according to the comparison criterion specified in the decl...
const size_t & size() const noexcept
Base class for scheduled events.
CompletionCallback on_completed
void set_execution_status(Execution_Status status)
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.
Execution_Status get_execution_status() 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.
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)
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
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
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.