Commit 6e904443 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

Use Baton (again) in EventBase::runInEventBaseThreadAndWait

Summary:
`Baton` is more lightweight than a mutex+condition variable, and the code is much simpler. This was actually the original implementation, but the dependency had to be dropped because `Baton` was unsupported on some architectures. That is not a problem anymore.

Also reorganize the includes to follow the conventions.

Reviewed By: andriigrynenko, yfeldblum

Differential Revision: D5396631

fbshipit-source-id: d782cf271eb35723aaeb3c372e1c1dafeaaf0f0a
parent 9d23df15
...@@ -19,18 +19,19 @@ ...@@ -19,18 +19,19 @@
#endif #endif
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/io/async/VirtualEventBase.h>
#include <fcntl.h>
#include <mutex>
#include <thread>
#include <folly/Baton.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/ThreadName.h> #include <folly/ThreadName.h>
#include <folly/io/async/NotificationQueue.h> #include <folly/io/async/NotificationQueue.h>
#include <folly/io/async/VirtualEventBase.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
#include <condition_variable>
#include <fcntl.h>
#include <mutex>
#include <thread>
namespace folly { namespace folly {
/* /*
...@@ -572,22 +573,14 @@ bool EventBase::runInEventBaseThreadAndWait(FuncRef fn) { ...@@ -572,22 +573,14 @@ bool EventBase::runInEventBaseThreadAndWait(FuncRef fn) {
return false; return false;
} }
bool ready = false; Baton<> ready;
std::mutex m;
std::condition_variable cv;
runInEventBaseThread([&] { runInEventBaseThread([&] {
SCOPE_EXIT { SCOPE_EXIT {
std::unique_lock<std::mutex> l(m); ready.post();
ready = true; };
cv.notify_one(); fn();
// We cannot release the lock before notify_one, because a spurious
// wakeup in the waiting thread may lead to cv and m going out of scope
// prematurely.
};
fn();
}); });
std::unique_lock<std::mutex> l(m); ready.wait();
cv.wait(l, [&] { return ready; });
return true; return true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment