From feddc3c0545b31fa58fbb74536eeff723cbeaaa7 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Thu, 17 Jan 2019 19:06:00 +0000 Subject: [PATCH] core: use ndn-cxx scheduler::ScopedEventId refs #4698 Change-Id: I5ab56855fb22824e129fd7e3ccfb53174cf34f82 --- core/scheduler.cpp | 52 +------ core/scheduler.hpp | 59 ++------ rib/readvertise/readvertise.cpp | 4 +- rib/readvertise/readvertised-route.hpp | 8 +- rib/rib-manager.cpp | 5 +- rib/rib-manager.hpp | 4 +- rib/route.hpp | 11 +- tests/core/scheduler.t.cpp | 135 ++---------------- .../nfd-rib-readvertise-destination.t.cpp | 6 +- tools/ndn-autoconfig/main.cpp | 5 +- 10 files changed, 43 insertions(+), 246 deletions(-) diff --git a/core/scheduler.cpp b/core/scheduler.cpp index d0b0f2ba..094c188b 100644 --- a/core/scheduler.cpp +++ b/core/scheduler.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/** - * Copyright (c) 2014-2017, Regents of the University of California, +/* + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -49,59 +49,11 @@ schedule(time::nanoseconds after, const EventCallback& event) return getGlobalScheduler().scheduleEvent(after, event); } -void -cancel(const EventId& eventId) -{ - getGlobalScheduler().cancelEvent(eventId); -} - void resetGlobalScheduler() { g_scheduler.reset(); } -ScopedEventId::ScopedEventId() -{ -} - -ScopedEventId::ScopedEventId(const EventId& event) - : m_event(event) -{ -} - -ScopedEventId::ScopedEventId(ScopedEventId&& other) - : m_event(other.m_event) -{ - other.release(); -} - -ScopedEventId& -ScopedEventId::operator=(const EventId& event) -{ - if (m_event != event) { - scheduler::cancel(m_event); - m_event = event; - } - return *this; -} - -ScopedEventId::~ScopedEventId() -{ - scheduler::cancel(m_event); -} - -void -ScopedEventId::cancel() -{ - scheduler::cancel(m_event); -} - -void -ScopedEventId::release() -{ - m_event.reset(); -} - } // namespace scheduler } // namespace nfd diff --git a/core/scheduler.hpp b/core/scheduler.hpp index 491408a8..3ad448bb 100644 --- a/core/scheduler.hpp +++ b/core/scheduler.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -33,13 +33,9 @@ namespace nfd { namespace scheduler { -using ndn::Scheduler; - -/** \class nfd::scheduler::EventId - * \brief Opaque handle for a scheduled event. - */ -using ndn::EventId; - +using ndn::util::scheduler::Scheduler; +using ndn::util::scheduler::EventId; +using ndn::util::scheduler::ScopedEventId; using ndn::util::scheduler::EventCallback; /** \brief Schedule an event. @@ -49,50 +45,11 @@ schedule(time::nanoseconds after, const EventCallback& event); /** \brief Cancel a scheduled event. */ -void -cancel(const EventId& eventId); - -/** \brief Cancels an event automatically upon destruction. - */ -class ScopedEventId : noncopyable +inline void +cancel(EventId eventId) { -public: - ScopedEventId(); - - /** \brief Implicit constructor from EventId. - * \param event the event to be cancelled upon destruction - */ - ScopedEventId(const EventId& event); - - /** \brief Move constructor. - */ - ScopedEventId(ScopedEventId&& other); - - /** \brief Assigns an event. - * - * If a different event has been assigned to this instance previously, - * that event will be cancelled immediately. - */ - ScopedEventId& - operator=(const EventId& event); - - /** \brief Cancels the event. - */ - ~ScopedEventId(); - - /** \brief Cancels the event manually. - */ - void - cancel(); - - /** \brief Releases the event so that it won't be cancelled when this ScopedEventId is destructed. - */ - void - release(); - -private: - EventId m_event; -}; + eventId.cancel(); +} } // namespace scheduler } // namespace nfd diff --git a/rib/readvertise/readvertise.cpp b/rib/readvertise/readvertise.cpp index 09715eb8..4c51a034 100644 --- a/rib/readvertise/readvertise.cpp +++ b/rib/readvertise/readvertise.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -75,7 +75,7 @@ Readvertise::afterAddRoute(const RibRouteRef& ribRoute) ReadvertisedRouteContainer::iterator rrIt; bool isNew = false; - std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix, m_scheduler); + std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix); if (!isNew && rrIt->signer != action->signer) { NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId << diff --git a/rib/readvertise/readvertised-route.hpp b/rib/readvertise/readvertised-route.hpp index c2ca8480..50be0c46 100644 --- a/rib/readvertise/readvertised-route.hpp +++ b/rib/readvertise/readvertised-route.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -29,7 +29,7 @@ #include "core/common.hpp" #include -#include +#include namespace nfd { namespace rib { @@ -39,11 +39,11 @@ namespace rib { class ReadvertisedRoute : noncopyable { public: - ReadvertisedRoute(const Name& prefix, ndn::util::Scheduler& scheduler) + explicit + ReadvertisedRoute(const Name& prefix) : prefix(prefix) , nRibRoutes(0) , retryDelay(0) - , retryEvt(scheduler) { } diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp index 35bc87fe..d09420e4 100644 --- a/rib/rib-manager.cpp +++ b/rib/rib-manager.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -59,7 +59,6 @@ RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain, , m_localhostValidator(face) , m_localhopValidator(face) , m_isLocalhopEnabled(false) - , m_activeFaceFetchEvent(m_scheduler) { registerCommandHandler("register", bind(&RibManager::registerEntry, this, _2, _3, _4, _5)); @@ -139,7 +138,7 @@ RibManager::beginAddRoute(const Name& name, Route route, optional #include #include -#include +#include namespace nfd { namespace rib { diff --git a/rib/route.hpp b/rib/route.hpp index cc4ff02d..05449e22 100644 --- a/rib/route.hpp +++ b/rib/route.hpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -60,17 +60,15 @@ public: } void - setExpirationEvent(const ndn::util::scheduler::EventId& eid, ndn::util::Scheduler& scheduler) + setExpirationEvent(const ndn::util::scheduler::EventId& eid) { m_expirationEvent = eid; - m_scheduler = &scheduler; } void - cancelExpirationEvent() const + cancelExpirationEvent() { - if (m_scheduler) - m_scheduler->cancelEvent(m_expirationEvent); + m_expirationEvent.cancel(); } std::underlying_type::type @@ -104,7 +102,6 @@ public: private: ndn::util::scheduler::EventId m_expirationEvent; - ndn::util::Scheduler* m_scheduler; }; bool diff --git a/tests/core/scheduler.t.cpp b/tests/core/scheduler.t.cpp index a2ad7526..88adb038 100644 --- a/tests/core/scheduler.t.cpp +++ b/tests/core/scheduler.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ -/** - * Copyright (c) 2014-2016, Regents of the University of California, +/* + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -30,7 +30,6 @@ #include namespace nfd { - namespace scheduler { // defined in scheduler.cpp Scheduler& @@ -39,58 +38,28 @@ getGlobalScheduler(); namespace tests { -using scheduler::EventId; -using scheduler::ScopedEventId; - BOOST_FIXTURE_TEST_SUITE(TestScheduler, BaseFixture) -class SchedulerFixture : protected BaseFixture +BOOST_AUTO_TEST_CASE(ScheduleCancel) { -public: - SchedulerFixture() - : count1(0) - , count2(0) - , count3(0) - { - } + int count1 = 0, count2 = 0, count3 = 0; - void - event1() - { + scheduler::schedule(500_ms, [&] { BOOST_CHECK_EQUAL(count3, 1); ++count1; - } + }); - void - event2() - { - ++count2; - } + scheduler::EventId eid = scheduler::schedule(1_s, [&] { ++count2; }); + scheduler::cancel(eid); - void - event3() - { + scheduler::schedule(250_ms, [&] { BOOST_CHECK_EQUAL(count1, 0); ++count3; - } + }); -public: - int count1; - int count2; - int count3; -}; - -BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) -{ - scheduler::schedule(time::milliseconds(500), bind(&SchedulerFixture::event1, this)); - - EventId i = scheduler::schedule(time::seconds(1), bind(&SchedulerFixture::event2, this)); - scheduler::cancel(i); - - scheduler::schedule(time::milliseconds(250), bind(&SchedulerFixture::event3, this)); - - i = scheduler::schedule(time::milliseconds(50), bind(&SchedulerFixture::event2, this)); - scheduler::cancel(i); + { + scheduler::ScopedEventId se = scheduler::schedule(50_ms, [&] { ++count2; }); + } // se goes out of scope, canceling the event g_io.run(); @@ -99,87 +68,11 @@ BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture) BOOST_CHECK_EQUAL(count3, 1); } -BOOST_AUTO_TEST_CASE(CancelEmptyEvent) -{ - EventId i; - scheduler::cancel(i); - - // Trivial check to avoid "test case did not check any assertions" message from Boost.Test - BOOST_CHECK(true); -} - -class SelfCancelFixture : protected BaseFixture -{ -public: - void - cancelSelf() const - { - scheduler::cancel(m_selfEventId); - } - -public: - EventId m_selfEventId; -}; - -BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture) -{ - m_selfEventId = scheduler::schedule(time::milliseconds(100), - bind(&SelfCancelFixture::cancelSelf, this)); - - BOOST_REQUIRE_NO_THROW(g_io.run()); -} - -BOOST_FIXTURE_TEST_CASE(ScopedEventIdDestruct, UnitTestTimeFixture) -{ - int hit = 0; - { - ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); - } // se goes out of scope - this->advanceClocks(time::milliseconds(1), 15); - BOOST_CHECK_EQUAL(hit, 0); -} - -BOOST_FIXTURE_TEST_CASE(ScopedEventIdAssign, UnitTestTimeFixture) -{ - int hit1 = 0, hit2 = 0; - ScopedEventId se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit1; }); - se1 = scheduler::schedule(time::milliseconds(10), [&] { ++hit2; }); - this->advanceClocks(time::milliseconds(1), 15); - BOOST_CHECK_EQUAL(hit1, 0); - BOOST_CHECK_EQUAL(hit2, 1); -} - -BOOST_FIXTURE_TEST_CASE(ScopedEventIdRelease, UnitTestTimeFixture) -{ - int hit = 0; - { - ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); - se.release(); - } // se goes out of scope - this->advanceClocks(time::milliseconds(1), 15); - BOOST_CHECK_EQUAL(hit, 1); -} - -BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture) -{ - int hit = 0; - unique_ptr se2; - { - ScopedEventId se = scheduler::schedule(time::milliseconds(10), [&] { ++hit; }); - se2.reset(new ScopedEventId(std::move(se))); - } // se goes out of scope - this->advanceClocks(time::milliseconds(1), 15); - BOOST_CHECK_EQUAL(hit, 1); -} - BOOST_AUTO_TEST_CASE(ThreadLocalScheduler) { scheduler::Scheduler* s1 = &scheduler::getGlobalScheduler(); scheduler::Scheduler* s2 = nullptr; - boost::thread t([&s2] { - s2 = &scheduler::getGlobalScheduler(); - }); - + boost::thread t([&s2] { s2 = &scheduler::getGlobalScheduler(); }); t.join(); BOOST_CHECK(s1 != nullptr); diff --git a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp b/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp index 48164bd2..49677887 100644 --- a/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp +++ b/tests/rib/readvertise/nfd-rib-readvertise-destination.t.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2018, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -122,7 +122,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios) { Scenario scenario; Name prefix("/ndn/memphis/test"); - ReadvertisedRoute rr(prefix, scheduler); + ReadvertisedRoute rr(prefix); const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register"); dest.advertise(rr, successCallback, failureCallback); @@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios) { Scenario scenario; Name prefix("/ndn/memphis/test"); - ReadvertisedRoute rr(prefix, scheduler); + ReadvertisedRoute rr(prefix); const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister"); dest.withdraw(rr, successCallback, failureCallback); diff --git a/tools/ndn-autoconfig/main.cpp b/tools/ndn-autoconfig/main.cpp index 6a2a03f6..a8b2c45d 100644 --- a/tools/ndn-autoconfig/main.cpp +++ b/tools/ndn-autoconfig/main.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2017, Regents of the University of California, + * Copyright (c) 2014-2019, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, @@ -35,7 +35,6 @@ #include #include #include -#include #include // suppress warning caused by boost::program_options::parse_config_file @@ -88,7 +87,7 @@ runDaemon(Procedure& proc) }); util::Scheduler sched(proc.getIoService()); - util::scheduler::ScopedEventId runEvt(sched); + util::scheduler::ScopedEventId runEvt; auto scheduleRerun = [&] (time::nanoseconds delay) { runEvt = sched.scheduleEvent(delay, [&] { proc.runOnce(); }); };