Files
ndn-cxx/ndn-cxx/util/scheduler.cpp
T
Davide Pesavento 2f46d6543e Avoid deprecated Boost.Asio interfaces
Change-Id: I00d285893ff61619f49dff8a8a55d0d0e2c309a7
2023-11-10 17:40:43 -05:00

155 lines
3.7 KiB
C++

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2023 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "ndn-cxx/util/scheduler.hpp"
#include "ndn-cxx/util/impl/steady-timer.hpp"
#include "ndn-cxx/util/scope.hpp"
namespace ndn::scheduler {
/**
* \brief Stores internal information about a scheduled event.
*/
struct EventInfo : noncopyable
{
EventInfo(time::nanoseconds after, EventCallback&& cb)
: expiry(time::steady_clock::now() + after)
, callback(std::move(cb))
{
}
time::steady_clock::time_point expiry;
EventCallback callback;
Scheduler::EventQueue::const_iterator queueIt;
bool isExpired = false;
};
EventId::EventId(Scheduler& sched, weak_ptr<EventInfo> info)
: CancelHandle([&sched, info] { sched.cancelImpl(info.lock()); })
, m_info(std::move(info))
{
}
EventId::operator bool() const noexcept
{
auto sp = m_info.lock();
return sp != nullptr && !sp->isExpired;
}
void
EventId::reset() noexcept
{
*this = {};
}
bool
Scheduler::EventQueueCompare::operator()(const shared_ptr<EventInfo>& a,
const shared_ptr<EventInfo>& b) const noexcept
{
return a->expiry < b->expiry;
}
Scheduler::Scheduler(boost::asio::io_context& ioCtx)
: m_timer(make_unique<detail::SteadyTimer>(ioCtx))
{
}
Scheduler::~Scheduler() = default;
EventId
Scheduler::schedule(time::nanoseconds after, EventCallback callback)
{
BOOST_ASSERT(callback != nullptr);
auto i = m_queue.insert(std::make_shared<EventInfo>(after, std::move(callback)));
(*i)->queueIt = i;
if (!m_isEventExecuting && i == m_queue.begin()) {
// the new event is the first one to expire
scheduleNext();
}
return EventId(*this, *i);
}
void
Scheduler::cancelImpl(const shared_ptr<EventInfo>& info)
{
if (info == nullptr || info->isExpired) {
return;
}
if (info->queueIt == m_queue.begin()) {
m_timer->cancel();
}
m_queue.erase(info->queueIt);
if (!m_isEventExecuting) {
scheduleNext();
}
}
void
Scheduler::cancelAllEvents()
{
m_queue.clear();
m_timer->cancel();
}
void
Scheduler::scheduleNext()
{
if (!m_queue.empty()) {
m_timer->expires_at((*m_queue.begin())->expiry);
m_timer->async_wait([this] (const auto& error) { executeEvent(error); });
}
}
void
Scheduler::executeEvent(const boost::system::error_code& error)
{
if (error) { // e.g., cancelled
return;
}
auto guard = make_scope_exit([this] {
m_isEventExecuting = false;
scheduleNext();
});
m_isEventExecuting = true;
// process all expired events
auto now = time::steady_clock::now();
while (!m_queue.empty()) {
auto head = m_queue.begin();
shared_ptr<EventInfo> info = *head;
if (info->expiry > now) {
break;
}
m_queue.erase(head);
info->isExpired = true;
info->callback();
}
}
} // namespace ndn::scheduler