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(std::make_shared<EventInfo>(after,
std::move(callback), ns3::Simulator::GetContext()));
103 if (!m_isEventExecuting && i == m_queue.begin()) {
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) {
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));
Copyright (c) 2011-2015 Regents of the University of California.
void reset() noexcept
Clear this EventId without canceling.
std::function< void()> EventCallback
Function to be invoked when a scheduled event expires.
static time_point now() noexcept
EventId schedule(time::nanoseconds after, EventCallback callback)
Schedule a one-time event after the specified delay.
A handle for a scheduled event.
Scheduler(DummyIoService &ioService)
EventId() noexcept=default
Constructs an empty EventId.
std::ostream & operator<<(std::ostream &os, const EventId &eventId)
#define NDN_CXX_NODISCARD
EventInfo(time::nanoseconds after, EventCallback &&cb, uint32_t context)
Generic time-based scheduler.
Stores internal information about a scheduled event.
Scheduler::EventQueue::const_iterator queueIt
time::steady_clock::TimePoint expireTime
NDN_CXX_NODISCARD time::nanoseconds expiresFromNow() const
boost::chrono::nanoseconds nanoseconds
void cancelAllEvents()
Cancel all scheduled events.