From: Yedidya Feldblum <yfeldblum@fb.com>
Date: Fri, 6 Feb 2015 19:37:36 +0000 (-0800)
Subject: Dedupe the implementations of EventBase::runInEventBaseThreadAndWait.
X-Git-Tag: v0.25.0~13
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=777002cbfacc305ac58b31e8abe64ed1f032ebbc;p=folly.git

Dedupe the implementations of EventBase::runInEventBaseThreadAndWait.

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: D1810764, D1823407
---

diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp
index 8ec54b67..2f38749b 100644
--- a/folly/io/async/EventBase.cpp
+++ b/folly/io/async/EventBase.cpp
@@ -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 "
diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h
index 3cdfd2dd..390ee243 100644
--- a/folly/io/async/EventBase.h
+++ b/folly/io/async/EventBase.h
@@ -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