diff --git a/core/global-io.cpp b/core/global-io.cpp index bd73244d..f03d13cb 100644 --- a/core/global-io.cpp +++ b/core/global-io.cpp @@ -35,6 +35,7 @@ resetGlobalScheduler(); } // namespace scheduler static boost::thread_specific_ptr g_ioService; +static boost::asio::io_service* g_mainIoService = nullptr; static boost::asio::io_service* g_ribIoService = nullptr; boost::asio::io_service& @@ -53,12 +54,25 @@ resetGlobalIoService() g_ioService.reset(); } +void +setMainIoService(boost::asio::io_service* mainIo) +{ + g_mainIoService = mainIo; +} + void setRibIoService(boost::asio::io_service* ribIo) { g_ribIoService = ribIo; } +boost::asio::io_service& +getMainIoService() +{ + BOOST_ASSERT(g_mainIoService != nullptr); + return *g_mainIoService; +} + boost::asio::io_service& getRibIoService() { @@ -66,6 +80,12 @@ getRibIoService() return *g_ribIoService; } +void +runOnMainIoService(const std::function& f) +{ + getMainIoService().post(f); +} + void runOnRibIoService(const std::function& f) { diff --git a/core/global-io.hpp b/core/global-io.hpp index ab0e17c8..0fb2fbe2 100644 --- a/core/global-io.hpp +++ b/core/global-io.hpp @@ -34,14 +34,25 @@ namespace nfd { boost::asio::io_service& getGlobalIoService(); +void +setMainIoService(boost::asio::io_service* mainIo); + void setRibIoService(boost::asio::io_service* ribIo); +/** \brief run a function on the main io_service instance + */ +void +runOnMainIoService(const std::function& f); + /** \brief run a function on the RIB io_service instance */ void runOnRibIoService(const std::function& f); +boost::asio::io_service& +getMainIoService(); + boost::asio::io_service& getRibIoService(); diff --git a/daemon/main.cpp b/daemon/main.cpp index 448b1050..f6d482d1 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -105,6 +105,7 @@ public: std::atomic_int retval(0); boost::asio::io_service* const mainIo = &getGlobalIoService(); + setMainIoService(mainIo); boost::asio::io_service* ribIo = nullptr; // Mutex and conditional variable to implement synchronization between main and RIB manager diff --git a/tests/core/global-io.t.cpp b/tests/core/global-io.t.cpp index 8ae91260..5aa3bc6b 100644 --- a/tests/core/global-io.t.cpp +++ b/tests/core/global-io.t.cpp @@ -51,20 +51,31 @@ BOOST_AUTO_TEST_CASE(ThreadLocalGlobalIoService) BOOST_CHECK(s1 != s2); } -BOOST_FIXTURE_TEST_CASE(RibIoService, RibIoFixture) +BOOST_FIXTURE_TEST_CASE(MainRibIoService, RibIoFixture) { boost::asio::io_service* mainIo = &g_io; boost::asio::io_service* ribIo = g_ribIo; BOOST_CHECK(mainIo != ribIo); BOOST_CHECK(&getGlobalIoService() == mainIo); + BOOST_CHECK(&getMainIoService() == mainIo); BOOST_CHECK(&getRibIoService() == ribIo); auto mainThreadId = boost::this_thread::get_id(); runOnRibIoService([&] { BOOST_CHECK(mainThreadId != boost::this_thread::get_id()); - BOOST_CHECK(&getRibIoService() == ribIo); BOOST_CHECK(&getGlobalIoService() == ribIo); + BOOST_CHECK(&getMainIoService() == mainIo); + BOOST_CHECK(&getRibIoService() == ribIo); + }); + + runOnRibIoService([&] { + runOnMainIoService([&] { + BOOST_CHECK(mainThreadId == boost::this_thread::get_id()); + BOOST_CHECK(&getGlobalIoService() == mainIo); + BOOST_CHECK(&getMainIoService() == mainIo); + BOOST_CHECK(&getRibIoService() == ribIo); + }); }); } @@ -80,10 +91,10 @@ BOOST_FIXTURE_TEST_CASE(PollInAllThreads, RibIoFixture) hasRibRun = false; bool hasMainRun = false; - g_io.post([&] { - hasMainRun = true; - runOnRibIoService([&] { hasRibRun = true; }); - }); + runOnMainIoService([&] { + hasMainRun = true; + runOnRibIoService([&] { hasRibRun = true; }); + }); BOOST_CHECK_EQUAL(hasMainRun, false); BOOST_CHECK_EQUAL(hasRibRun, false); @@ -105,9 +116,9 @@ BOOST_FIXTURE_TEST_CASE(AdvanceClocks, RibIoTimeFixture) hasRibRun = false; bool hasMainRun = false; scheduler::schedule(250_ms, [&] { - hasMainRun = true; - runOnRibIoService([&] { hasRibRun = true; }); - }); + hasMainRun = true; + runOnRibIoService([&] { hasRibRun = true; }); + }); BOOST_CHECK_EQUAL(hasMainRun, false); BOOST_CHECK_EQUAL(hasRibRun, false); diff --git a/tests/rib-io-fixture.cpp b/tests/rib-io-fixture.cpp index ef228d5d..d058851c 100644 --- a/tests/rib-io-fixture.cpp +++ b/tests/rib-io-fixture.cpp @@ -35,6 +35,9 @@ RibIoFixture::RibIoFixture() std::mutex m; std::condition_variable cv; + g_mainIo = &getGlobalIoService(); + setMainIoService(g_mainIo); + g_ribThread = boost::thread([&] { { std::lock_guard lock(m); diff --git a/tests/rib-io-fixture.hpp b/tests/rib-io-fixture.hpp index e89ba440..ec147d55 100644 --- a/tests/rib-io-fixture.hpp +++ b/tests/rib-io-fixture.hpp @@ -56,6 +56,10 @@ protected: poll(); protected: + /** \brief pointer to global main io_service + */ + boost::asio::io_service* g_mainIo = nullptr; + /** \brief pointer to global RIB io_service */ boost::asio::io_service* g_ribIo = nullptr;