move MemoryMapping, Shell, ThreadId, ThreadName, and VersionCheck to system/
[folly.git] / folly / io / async / ScopedEventBaseThread.cpp
index 9f14b9186f2e3c3813000be7d969b77ce00371b1..7ffaae6270d2d9f55a689653a6793e45900dcf4b 100644 (file)
 
 #include <folly/Function.h>
 #include <folly/Range.h>
-#include <folly/ThreadName.h>
 #include <folly/io/async/EventBaseManager.h>
+#include <folly/system/ThreadName.h>
 
 using namespace std;
 
 namespace folly {
 
-static void run(EventBaseManager* ebm, EventBase* eb, const StringPiece& name) {
+static void run(
+    EventBaseManager* ebm,
+    EventBase* eb,
+    folly::Baton<>* stop,
+    const StringPiece& name) {
   if (name.size()) {
     folly::setThreadName(name);
   }
@@ -38,6 +42,8 @@ static void run(EventBaseManager* ebm, EventBase* eb, const StringPiece& name) {
   // must destruct in io thread for on-destruction callbacks
   EventBase::StackFunctionLoopCallback cb([=] { ebm->clearEventBase(); });
   eb->runOnDestruction(&cb);
+  // wait until terminateLoopSoon() is complete
+  stop->wait();
   eb->~EventBase();
 }
 
@@ -55,12 +61,13 @@ ScopedEventBaseThread::ScopedEventBaseThread(
     const StringPiece& name)
     : ebm_(ebm ? ebm : EventBaseManager::get()) {
   new (&eb_) EventBase();
-  th_ = thread(run, ebm_, &eb_, name);
+  th_ = thread(run, ebm_, &eb_, &stop_, name);
   eb_.waitUntilRunning();
 }
 
 ScopedEventBaseThread::~ScopedEventBaseThread() {
   eb_.terminateLoopSoon();
+  stop_.post();
   th_.join();
 }