Summary:
[Folly] EventBase::runImediatelyOrRunInEventBaseThreadAndWait.
When you have code that you need to run in the event loop and wait for it to finish, and you yourself might be in the event loop but might be in another thread instead.
Test Plan:
Unit tests:
* `folly/io/async/test/EventBaseTest.cpp`.
Reviewed By: davejwatson@fb.com
Subscribers: folly-diffs@, yfeldblum, chalfant, agallagher, njormrod, ldbrandy, brettp, dougw
FB internal diff:
D1953110
Signature: t1:
1953110:
1427751076:
ad2c9f4d5b65ef952cba94b06be887672ee44583
return true;
}
+bool EventBase::runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn) {
+ if (inRunningEventBaseThread()) {
+ fn();
+ return true;
+ } else {
+ return runInEventBaseThreadAndWait(fn);
+ }
+}
+
void EventBase::runAfterDelay(const Cob& cob,
int milliseconds,
TimeoutManager::InternalEnum in) {
*/
bool runInEventBaseThreadAndWait(const Cob& fn);
+ /*
+ * Like runInEventBaseThreadAndWait, except if the caller is already in the
+ * event base thread, the functor is simply run inline.
+ */
+ template<typename T>
+ bool runImmediatelyOrRunInEventBaseThreadAndWait(void (*fn)(T*), T* arg) {
+ return runImmediatelyOrRunInEventBaseThreadAndWait(
+ reinterpret_cast<void (*)(void*)>(fn), reinterpret_cast<void*>(arg));
+ }
+
+ /*
+ * Like runInEventBaseThreadAndWait, except if the caller is already in the
+ * event base thread, the functor is simply run inline.
+ */
+ bool runImmediatelyOrRunInEventBaseThreadAndWait(
+ void (*fn)(void*), void* arg) {
+ return runImmediatelyOrRunInEventBaseThreadAndWait(std::bind(fn, arg));
+ }
+
+ /*
+ * Like runInEventBaseThreadAndWait, except if the caller is already in the
+ * event base thread, the functor is simply run inline.
+ */
+bool runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn);
+
/**
* Runs the given Cob at some time after the specified number of
* milliseconds. (No guarantees exactly when.)
// This test simulates some calls, and verifies that the waiting happens by
// triggering what otherwise would be race conditions, and trying to detect
// whether any of the race conditions happened.
-TEST(EventBaseTest, RunInEventLoopThreadAndWait) {
+TEST(EventBaseTest, RunInEventBaseThreadAndWait) {
const size_t c = 256;
vector<unique_ptr<atomic<size_t>>> atoms(c);
for (size_t i = 0; i < c; ++i) {
EXPECT_EQ(c, sum);
}
+TEST(EventBaseTest, RunImmediatelyOrRunInEventBaseThreadAndWaitCross) {
+ EventBase eb;
+ thread th(&EventBase::loopForever, &eb);
+ SCOPE_EXIT {
+ eb.terminateLoopSoon();
+ th.join();
+ };
+ auto mutated = false;
+ eb.runImmediatelyOrRunInEventBaseThreadAndWait([&] {
+ mutated = true;
+ });
+ EXPECT_TRUE(mutated);
+}
+
+TEST(EventBaseTest, RunImmediatelyOrRunInEventBaseThreadAndWaitWithin) {
+ EventBase eb;
+ thread th(&EventBase::loopForever, &eb);
+ SCOPE_EXIT {
+ eb.terminateLoopSoon();
+ th.join();
+ };
+ eb.runInEventBaseThreadAndWait([&] {
+ auto mutated = false;
+ eb.runImmediatelyOrRunInEventBaseThreadAndWait([&] {
+ mutated = true;
+ });
+ EXPECT_TRUE(mutated);
+ });
+}
+
///////////////////////////////////////////////////////////////////////////
// Tests for runInLoop()
///////////////////////////////////////////////////////////////////////////