core: Make global io_service and scheduler thread-local

Change-Id: I3d6b6cd3ca7a6e53b0dc0f4cae7a2f3270c7fd50
Refs: #2489
diff --git a/core/global-io.cpp b/core/global-io.cpp
index f8d8270..48566f6 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 <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "global-io.hpp"
+#include <boost/thread/tss.hpp>
 
 namespace nfd {
 
@@ -32,13 +34,14 @@
 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;
 }
diff --git a/core/scheduler.cpp b/core/scheduler.cpp
index 0979f97..177045f 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 <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;
 }
 
diff --git a/tests/core/global-io.cpp b/tests/core/global-io.cpp
new file mode 100644
index 0000000..7928056
--- /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 <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
diff --git a/tests/core/scheduler.cpp b/tests/core/scheduler.cpp
index dcafac2..15c02a9 100644
--- a/tests/core/scheduler.cpp
+++ b/tests/core/scheduler.cpp
@@ -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_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 8133b93..955266b 100644
--- a/wscript
+++ b/wscript
@@ -102,7 +102,7 @@
 
     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);