From 5d57f97fa77bd62f6e2d8e33a6cf83654144719a Mon Sep 17 00:00:00 2001 From: Alexander Afanasyev Date: Wed, 11 Feb 2015 21:04:29 -0800 Subject: [PATCH] core: Make global io_service and scheduler thread-local Change-Id: I3d6b6cd3ca7a6e53b0dc0f4cae7a2f3270c7fd50 Refs: #2489 --- core/global-io.cpp | 23 +++++++++-------- core/scheduler.cpp | 25 ++++++++++-------- tests/core/global-io.cpp | 55 ++++++++++++++++++++++++++++++++++++++++ tests/core/scheduler.cpp | 24 ++++++++++++++++++ wscript | 2 +- 5 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 tests/core/global-io.cpp diff --git a/core/global-io.cpp b/core/global-io.cpp index f8d82701..48566f6a 100644 --- a/core/global-io.cpp +++ b/core/global-io.cpp @@ -1,11 +1,12 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014 Regents of the University of California, - * Arizona Board of Regents, - * Colorado State University, - * University Pierre & Marie Curie, Sorbonne University, - * Washington University in St. Louis, - * Beijing Institute of Technology + * Copyright (c) 2014-2015, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. * * This file is part of NFD (Named Data Networking Forwarding Daemon). * See AUTHORS.md for complete list of NFD authors and contributors. @@ -20,9 +21,10 @@ * * You should have received a copy of the GNU General Public License along with * NFD, e.g., in COPYING.md file. If not, see . - **/ + */ #include "global-io.hpp" +#include namespace nfd { @@ -32,13 +34,14 @@ void resetGlobalScheduler(); } // namespace scheduler -static shared_ptr g_ioService; + +static boost::thread_specific_ptr g_ioService; boost::asio::io_service& getGlobalIoService() { - if (!static_cast(g_ioService)) { - g_ioService = make_shared(); + if (g_ioService.get() == nullptr) { + g_ioService.reset(new boost::asio::io_service()); } return *g_ioService; } diff --git a/core/scheduler.cpp b/core/scheduler.cpp index 0979f971..177045f4 100644 --- a/core/scheduler.cpp +++ b/core/scheduler.cpp @@ -1,12 +1,12 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** - * Copyright (c) 2014, Regents of the University of California, - * Arizona Board of Regents, - * Colorado State University, - * University Pierre & Marie Curie, Sorbonne University, - * Washington University in St. Louis, - * Beijing Institute of Technology, - * The University of Memphis + * Copyright (c) 2014-2015, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. * * This file is part of NFD (Named Data Networking Forwarding Daemon). * See AUTHORS.md for complete list of NFD authors and contributors. @@ -26,17 +26,20 @@ #include "scheduler.hpp" #include "global-io.hpp" +#include + namespace nfd { namespace scheduler { -static shared_ptr g_scheduler; +static boost::thread_specific_ptr g_scheduler; -inline Scheduler& +Scheduler& getGlobalScheduler() { - if (!static_cast(g_scheduler)) { - g_scheduler = make_shared(ref(getGlobalIoService())); + if (g_scheduler.get() == nullptr) { + g_scheduler.reset(new Scheduler(getGlobalIoService())); } + return *g_scheduler; } diff --git a/tests/core/global-io.cpp b/tests/core/global-io.cpp new file mode 100644 index 00000000..7928056c --- /dev/null +++ b/tests/core/global-io.cpp @@ -0,0 +1,55 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/** + * Copyright (c) 2014-2015, Regents of the University of California, + * Arizona Board of Regents, + * Colorado State University, + * University Pierre & Marie Curie, Sorbonne University, + * Washington University in St. Louis, + * Beijing Institute of Technology, + * The University of Memphis. + * + * This file is part of NFD (Named Data Networking Forwarding Daemon). + * See AUTHORS.md for complete list of NFD authors and contributors. + * + * NFD is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * NFD 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * NFD, e.g., in COPYING.md file. If not, see . + */ + +#include "core/global-io.hpp" + +#include "tests/test-common.hpp" + +#include + +namespace nfd { +namespace tests { + +BOOST_FIXTURE_TEST_SUITE(CoreGlobalIo, BaseFixture) + +BOOST_AUTO_TEST_CASE(ThreadLocalGlobalIoService) +{ + boost::asio::io_service* s1 = &getGlobalIoService(); + boost::asio::io_service* s2 = nullptr; + boost::thread t([&s2] { + s2 = &getGlobalIoService(); + }); + + t.join(); + + BOOST_CHECK(s1 != nullptr); + BOOST_CHECK(s2 != nullptr); + BOOST_CHECK(s1 != s2); +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace tests +} // namespace nfd diff --git a/tests/core/scheduler.cpp b/tests/core/scheduler.cpp index dcafac21..15c02a94 100644 --- a/tests/core/scheduler.cpp +++ b/tests/core/scheduler.cpp @@ -27,7 +27,16 @@ #include "tests/test-common.hpp" +#include + namespace nfd { + +namespace scheduler { +// defined in scheduler.cpp +Scheduler& +getGlobalScheduler(); +} // namespace scheduler + namespace tests { using scheduler::EventId; @@ -158,6 +167,21 @@ BOOST_FIXTURE_TEST_CASE(ScopedEventIdMove, UnitTestTimeFixture) 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(); + }); + + t.join(); + + BOOST_CHECK(s1 != nullptr); + BOOST_CHECK(s2 != nullptr); + BOOST_CHECK(s1 != s2); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace tests diff --git a/wscript b/wscript index 8133b935..955266bb 100644 --- a/wscript +++ b/wscript @@ -102,7 +102,7 @@ main(int, char**) conf.check_cxx(header_name='ifaddrs.h', mandatory=False) - boost_libs = 'system chrono program_options random' + boost_libs = 'system chrono program_options random thread' if conf.options.with_tests: conf.env['WITH_TESTS'] = 1 conf.define('WITH_TESTS', 1);