Dedupe the implementations of EventBase::runInEventBaseThreadAndWait.
authorYedidya Feldblum <yfeldblum@fb.com>
Fri, 6 Feb 2015 19:37:36 +0000 (11:37 -0800)
committerSara Golemon <sgolemon@fb.com>
Wed, 11 Feb 2015 02:01:59 +0000 (18:01 -0800)
Summary: [Folly] Dedupe the implementations of EventBase::runInEventBaseThreadAndWait.

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

Reviewed By: subodh@fb.com

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

FB internal diff: D1826291

Signature: t1:1826291:1423225534:42264d8dcc8adec6b90ac8a3d6ce1f4b98f29297

Blame Revision: D1810764D1823407

folly/io/async/EventBase.cpp
folly/io/async/EventBase.h

index 8ec54b6753514fff2aef35e3e136d71f8ac1b256..2f38749bf6856f8b946b31df01229148f4aa852f 100644 (file)
@@ -564,31 +564,6 @@ bool EventBase::runInEventBaseThread(const Cob& fn) {
   return true;
 }
 
-bool EventBase::runInEventBaseThreadAndWait(void (*fn)(void*), void* arg) {
-  if (inRunningEventBaseThread()) {
-    LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
-               << "allowed";
-    return false;
-  }
-
-  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);
-  });
-  std::unique_lock<std::mutex> l(m);
-  cv.wait(l, [&] { return ready; });
-
-  return true;
-}
-
 bool EventBase::runInEventBaseThreadAndWait(const Cob& fn) {
   if (inRunningEventBaseThread()) {
     LOG(ERROR) << "EventBase " << this << ": Waiting in the event loop is not "
index 3cdfd2ddc1d15426515e04737dae104568319999..390ee243c82f8d2c1698e5d36c3651e25462bc3a 100644 (file)
@@ -360,7 +360,9 @@ class EventBase : private boost::noncopyable,
    * Like runInEventBaseThread, but the caller waits for the callback to be
    * executed.
    */
-  bool runInEventBaseThreadAndWait(void (*fn)(void*), void* arg);
+  bool runInEventBaseThreadAndWait(void (*fn)(void*), void* arg) {
+    return runInEventBaseThreadAndWait(std::bind(fn, arg));
+  }
 
   /*
    * Like runInEventBaseThread, but the caller waits for the callback to be