core: Make global io_service and scheduler thread-local

Change-Id: I3d6b6cd3ca7a6e53b0dc0f4cae7a2f3270c7fd50
Refs: #2489
This commit is contained in:
Alexander Afanasyev
2015-02-11 21:04:29 -08:00
parent 9cfeecaa2f
commit 5d57f97fa7
5 changed files with 107 additions and 22 deletions
+13 -10
View File
@@ -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 <http://www.gnu.org/licenses/>.
**/
*/
#include "global-io.hpp"
#include <boost/thread/tss.hpp>
namespace nfd {
@@ -32,13 +34,14 @@ void
resetGlobalScheduler();
} // namespace scheduler
static shared_ptr<boost::asio::io_service> g_ioService;
static boost::thread_specific_ptr<boost::asio::io_service> g_ioService;
boost::asio::io_service&
getGlobalIoService()
{
if (!static_cast<bool>(g_ioService)) {
g_ioService = make_shared<boost::asio::io_service>();
if (g_ioService.get() == nullptr) {
g_ioService.reset(new boost::asio::io_service());
}
return *g_ioService;
}
+14 -11
View File
@@ -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 <boost/thread/tss.hpp>
namespace nfd {
namespace scheduler {
static shared_ptr<Scheduler> g_scheduler;
static boost::thread_specific_ptr<Scheduler> g_scheduler;
inline Scheduler&
Scheduler&
getGlobalScheduler()
{
if (!static_cast<bool>(g_scheduler)) {
g_scheduler = make_shared<Scheduler>(ref(getGlobalIoService()));
if (g_scheduler.get() == nullptr) {
g_scheduler.reset(new Scheduler(getGlobalIoService()));
}
return *g_scheduler;
}
+55
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include "core/global-io.hpp"
#include "tests/test-common.hpp"
#include <boost/thread.hpp>
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
+24
View File
@@ -27,7 +27,16 @@
#include "tests/test-common.hpp"
#include <boost/thread.hpp>
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
+1 -1
View File
@@ -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);