24 #include <boost/scope_exit.hpp> 56 : CancelHandle([&sched, info] { sched.cancelImpl(info.lock()); })
57 , m_info(std::move(info))
61 EventId::operator bool() const noexcept
63 auto sp = m_info.lock();
64 return sp !=
nullptr && !sp->isExpired;
70 return (!*
this && !other) ||
71 !(m_info.owner_before(other.m_info) || other.m_info.owner_before(m_info));
83 return os << eventId.m_info.lock();
89 return a->expireTime < b->expireTime;
93 : m_isEventExecuting(false)
105 BOOST_ASSERT(callback !=
nullptr);
110 if (!m_isEventExecuting && i == m_queue.begin()) {
112 this->scheduleNext();
119 Scheduler::cancelImpl(
const shared_ptr<EventInfo>& info)
121 if (info ==
nullptr || info->isExpired) {
125 if (info->queueIt == m_queue.begin()) {
127 if (!m_timerEvent->IsExpired()) {
128 ns3::Simulator::Remove(*m_timerEvent);
130 m_timerEvent.reset();
133 m_queue.erase(info->queueIt);
135 if (!m_isEventExecuting) {
136 this->scheduleNext();
145 if (!m_timerEvent->IsExpired()) {
146 ns3::Simulator::Remove(*m_timerEvent);
148 m_timerEvent.reset();
153 Scheduler::scheduleNext()
155 if (!m_queue.empty()) {
156 m_timerEvent = ns3::Simulator::Schedule(ns3::NanoSeconds((*m_queue.begin())->expiresFromNow().count()),
157 &Scheduler::executeEvent,
this);
162 Scheduler::executeEvent()
164 m_isEventExecuting =
true;
166 m_timerEvent.reset();
167 BOOST_SCOPE_EXIT(this_) {
168 this_->m_isEventExecuting =
false;
169 this_->scheduleNext();
170 } BOOST_SCOPE_EXIT_END
174 while (!m_queue.empty()) {
175 auto head = m_queue.begin();
176 shared_ptr<EventInfo> info = *head;
177 if (info->expireTime > now) {
182 info->isExpired =
true;
void reset() noexcept
Clear this EventId without canceling.
Copyright (c) 2011-2015 Regents of the University of California.
EventId() noexcept=default
Constructs an empty EventId.
static time_point now() noexcept
EventQueue::const_iterator queueIt
Scheduler(DummyIoService &ioService)
EventId scheduleEvent(time::nanoseconds after, const EventCallback &callback)
Schedule a one-time event after the specified delay.
void cancelAllEvents()
Cancel all scheduled events.
Table::const_iterator iterator
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
EventInfo(time::nanoseconds after, const EventCallback &callback)
Stores internal information about a scheduled event.
time::nanoseconds expiresFromNow() const
A handle of scheduled event.
bool operator==(const EventId &other) const noexcept
Determine whether this and other refer to the same event, or are both empty/expired/cancelled.
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
time::steady_clock::TimePoint expireTime
bool operator()(const shared_ptr< EventInfo > &a, const shared_ptr< EventInfo > &b) const noexcept