Using emplace_back to avoid temporary
[folly.git] / folly / wangle / concurrent / IOThreadPoolExecutor.cpp
index 2fb649d1ab4f9cbb9e9d4478484a083997eaf71b..7552cad93ced27e8b7ed33d804976baae5239c4a 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <folly/MoveWrapper.h>
 #include <glog/logging.h>
-#include <folly/io/async/EventBaseManager.h>
 
 #include <folly/detail/MemoryIdler.h>
 
@@ -65,9 +64,11 @@ class MemoryIdlerTimeout
 
 IOThreadPoolExecutor::IOThreadPoolExecutor(
     size_t numThreads,
-    std::shared_ptr<ThreadFactory> threadFactory)
+    std::shared_ptr<ThreadFactory> threadFactory,
+    EventBaseManager* ebm)
   : ThreadPoolExecutor(numThreads, std::move(threadFactory)),
-    nextThread_(0) {
+    nextThread_(0),
+    eventBaseManager_(ebm) {
   addThreads(numThreads);
   CHECK(threadList_.get().size() == numThreads);
 }
@@ -128,6 +129,10 @@ EventBase* IOThreadPoolExecutor::getEventBase(
   return nullptr;
 }
 
+EventBaseManager* IOThreadPoolExecutor::getEventBaseManager() {
+  return eventBaseManager_;
+}
+
 std::shared_ptr<ThreadPoolExecutor::Thread>
 IOThreadPoolExecutor::makeThread() {
   return std::make_shared<IOThread>(this);
@@ -135,8 +140,7 @@ IOThreadPoolExecutor::makeThread() {
 
 void IOThreadPoolExecutor::threadRun(ThreadPtr thread) {
   const auto ioThread = std::static_pointer_cast<IOThread>(thread);
-  ioThread->eventBase =
-    folly::EventBaseManager::get()->getEventBase();
+  ioThread->eventBase = eventBaseManager_->getEventBase();
   thisThread_.reset(new std::shared_ptr<IOThread>(ioThread));
 
   auto idler = new MemoryIdlerTimeout(ioThread->eventBase);
@@ -152,6 +156,9 @@ void IOThreadPoolExecutor::threadRun(ThreadPtr thread) {
     }
   }
   stoppedThreads_.add(ioThread);
+
+  ioThread->eventBase = nullptr;
+  eventBaseManager_->clearEventBase();
 }
 
 // threadListLock_ is writelocked