Avoid EventBase depending on Baton (mutex/condvar variant).
[folly.git] / folly / io / async / EventBase.cpp
index 00aa9d80d0a0b7ea63ba31453de1afda67c4bb1f..8ec54b6753514fff2aef35e3e136d71f8ac1b256 100644 (file)
 
 #include <folly/io/async/EventBase.h>
 
-#include <folly/Baton.h>
 #include <folly/ThreadName.h>
 #include <folly/io/async/NotificationQueue.h>
 
 #include <boost/static_assert.hpp>
+#include <condition_variable>
 #include <fcntl.h>
+#include <mutex>
 #include <pthread.h>
 #include <unistd.h>
 
@@ -570,12 +571,20 @@ bool EventBase::runInEventBaseThreadAndWait(void (*fn)(void*), void* arg) {
     return false;
   }
 
-  Baton<> ready;
+  bool ready = false;
+  std::mutex m;
+  std::condition_variable cv;
   runInEventBaseThread([&] {
+      SCOPE_EXIT {
+        std::unique_lock<std::mutex> l(m);
+        ready = true;
+        l.unlock();
+        cv.notify_one();
+      };
       fn(arg);
-      ready.post();
   });
-  ready.wait();
+  std::unique_lock<std::mutex> l(m);
+  cv.wait(l, [&] { return ready; });
 
   return true;
 }
@@ -587,12 +596,20 @@ bool EventBase::runInEventBaseThreadAndWait(const Cob& fn) {
     return false;
   }
 
-  Baton<> ready;
+  bool ready = false;
+  std::mutex m;
+  std::condition_variable cv;
   runInEventBaseThread([&] {
+      SCOPE_EXIT {
+        std::unique_lock<std::mutex> l(m);
+        ready = true;
+        l.unlock();
+        cv.notify_one();
+      };
       fn();
-      ready.post();
   });
-  ready.wait();
+  std::unique_lock<std::mutex> l(m);
+  cv.wait(l, [&] { return ready; });
 
   return true;
 }