ScopedEventBaseThread should support an EventBaseManager context.
[folly.git] / folly / io / async / ScopedEventBaseThread.cpp
index 29a44592729f55289b6bf95c8243888bf84a38a7..7821d17baf34495fdc242f8fcf4111396341a81b 100644 (file)
@@ -23,12 +23,29 @@ using namespace std;
 
 namespace folly {
 
-ScopedEventBaseThread::ScopedEventBaseThread(bool autostart) {
+static void run(EventBaseManager* ebm, EventBase* eb) {
+  if (ebm) {
+    ebm->setEventBase(eb, false);
+  }
+  CHECK_NOTNULL(eb)->loopForever();
+  if (ebm) {
+    ebm->clearEventBase();
+  }
+}
+
+ScopedEventBaseThread::ScopedEventBaseThread(
+    bool autostart,
+    EventBaseManager* ebm) :
+  ebm_(ebm) {
   if (autostart) {
     start();
   }
 }
 
+ScopedEventBaseThread::ScopedEventBaseThread(
+    EventBaseManager* ebm) :
+  ScopedEventBaseThread(true, ebm) {}
+
 ScopedEventBaseThread::~ScopedEventBaseThread() {
   stop();
 }
@@ -44,7 +61,7 @@ void ScopedEventBaseThread::start() {
     return;
   }
   eventBase_ = make_unique<EventBase>();
-  thread_ = make_unique<thread>(&EventBase::loopForever, &*eventBase_);
+  thread_ = make_unique<thread>(run, ebm_, eventBase_.get());
   eventBase_->waitUntilRunning();
 }