From 777002cbfacc305ac58b31e8abe64ed1f032ebbc Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Fri, 6 Feb 2015 11:37:36 -0800 Subject: [PATCH] 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 --- folly/io/async/EventBase.cpp | 25 ------------------------- folly/io/async/EventBase.h | 4 +++- 2 files changed, 3 insertions(+), 26 deletions(-) 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 l(m); - ready = true; - l.unlock(); - cv.notify_one(); - }; - fn(arg); - }); - std::unique_lock 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 -- 2.34.1