Avoid EventBase depending on Baton (mutex/condvar variant).
authorYedidya Feldblum <yfeldblum@fb.com>
Wed, 4 Feb 2015 23:10:51 +0000 (15:10 -0800)
committerSara Golemon <sgolemon@fb.com>
Wed, 11 Feb 2015 02:01:59 +0000 (18:01 -0800)
Summary: [Folly] Avoid EventBase depending on Baton (mutex/condvar variant).

Test Plan:
Unit tests:
* folly/io/async/test/EventBaseTest.cpp

Reviewed By: subodh@fb.com

Subscribers: trunkagent, fma, folly-diffs@, yfeldblum, dougw, brettp

FB internal diff: D1823407

Signature: t1:1823407:1423088450:71ae1673ed8067103e2aaa1ac9b239eae9ebe9de

Blame Revision: D1810764

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;
 }