24 #include <boost/scope_exit.hpp>
49 Scheduler::EventQueue::const_iterator
queueIt;
56 : CancelHandle([&sched, info] { sched.cancelImpl(info.lock()); })
61 EventId::operator bool() const noexcept
63 auto sp = m_info.lock();
64 return sp !=
nullptr && !sp->isExpired;
76 return os << eventId.m_info.lock();
80 Scheduler::EventQueueCompare::operator()(
const shared_ptr<EventInfo>& a,
81 const shared_ptr<EventInfo>& b)
const noexcept
83 return a->expireTime < b->expireTime;
98 BOOST_ASSERT(callback !=
nullptr);
100 auto i = m_queue.insert(make_shared<EventInfo>(after,
std::move(callback), ns3::Simulator::GetContext()));
103 if (!m_isEventExecuting && i == m_queue.begin()) {
105 this->scheduleNext();
112 Scheduler::cancelImpl(
const shared_ptr<EventInfo>& info)
114 if (info ==
nullptr || info->isExpired) {
119 if (!m_timerEvent->IsExpired()) {
120 ns3::Simulator::Remove(*m_timerEvent);
122 m_timerEvent.reset();
124 m_queue.erase(info->queueIt);
126 if (!m_isEventExecuting) {
127 this->scheduleNext();
136 if (!m_timerEvent->IsExpired()) {
137 ns3::Simulator::Remove(*m_timerEvent);
139 m_timerEvent.reset();
144 Scheduler::scheduleNext()
146 if (!m_queue.empty()) {
147 m_timerEvent = ns3::Simulator::Schedule(ns3::NanoSeconds((*m_queue.begin())->expiresFromNow().count()),
148 &Scheduler::executeEvent,
this);
153 Scheduler::executeEvent()
155 m_isEventExecuting =
true;
157 m_timerEvent.reset();
158 BOOST_SCOPE_EXIT(this_) {
159 this_->m_isEventExecuting =
false;
160 this_->scheduleNext();
161 } BOOST_SCOPE_EXIT_END
165 while (!m_queue.empty()) {
166 auto head = m_queue.begin();
167 shared_ptr<EventInfo> info = *head;
168 if (info->expireTime > now) {
173 info->isExpired =
true;
174 if (ns3::Simulator::GetContext() == info->context) {
178 ns3::Simulator::ScheduleWithContext(info->context, ns3::Seconds(0), ns3::MakeEvent(info->callback));