From 138b7236d534088c9c437e2249d65a7aadea1c11 Mon Sep 17 00:00:00 2001 From: Francis Ma Date: Thu, 14 Jan 2016 14:07:21 -0800 Subject: [PATCH] Decouple future and fiber for mobile Summary: folly::fibers have a bunch of dependencies that are shaky on mobiles. Decoupling it by using folly::Baton instead of folly::fibers::Baton. Should have zero effect on fbcode cases. Reviewed By: djwatson, yfeldblum Differential Revision: D2821603 fb-gh-sync-id: 0ce3472c9eedf97224e8584d25e04515396cd18e --- folly/Baton.h | 7 +++++++ folly/futures/Future-inl.h | 21 ++++++++++++++++++--- folly/futures/test/TimesTest.cpp | 1 + folly/futures/test/WaitTest.cpp | 2 ++ folly/futures/test/WhileDoTest.cpp | 1 + 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/folly/Baton.h b/folly/Baton.h index 6737efc5..56ee49a3 100644 --- a/folly/Baton.h +++ b/folly/Baton.h @@ -217,6 +217,13 @@ struct Baton { } } + /// Similar to timed_wait, but with a duration. + template + bool timed_wait(const Duration& duration) { + auto deadline = Clock::now() + duration; + return timed_wait(deadline); + } + /// Similar to wait, but doesn't block the thread if it hasn't been posted. /// /// try_wait has the following semantics: diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 3a919ff4..fd2c7153 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -21,17 +21,32 @@ #include #include #include -#include +#include #include #include #include #include #include +#if defined(__ANDROID__) || defined(__APPLE__) +#define FOLLY_FUTURE_USING_FIBER 0 +#else +#define FOLLY_FUTURE_USING_FIBER 1 +#include +#endif + namespace folly { class Timekeeper; +namespace detail { +#if FOLLY_FUTURE_USING_FIBER +typedef folly::fibers::Baton FutureBatonType; +#else +typedef folly::Baton<> FutureBatonType; +#endif +} + namespace detail { std::shared_ptr getTimekeeperSingleton(); } @@ -937,7 +952,7 @@ void waitImpl(Future& f) { // short-circuit if there's nothing to do if (f.isReady()) return; - folly::fibers::Baton baton; + FutureBatonType baton; f.setCallback_([&](const Try& t) { baton.post(); }); baton.wait(); assert(f.isReady()); @@ -950,7 +965,7 @@ void waitImpl(Future& f, Duration dur) { folly::MoveWrapper> promise; auto ret = promise->getFuture(); - auto baton = std::make_shared(); + auto baton = std::make_shared(); f.setCallback_([baton, promise](Try&& t) mutable { promise->setTry(std::move(t)); baton->post(); diff --git a/folly/futures/test/TimesTest.cpp b/folly/futures/test/TimesTest.cpp index a9c63a82..6f3e9da9 100644 --- a/folly/futures/test/TimesTest.cpp +++ b/folly/futures/test/TimesTest.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include diff --git a/folly/futures/test/WaitTest.cpp b/folly/futures/test/WaitTest.cpp index 57f2f839..2d335691 100644 --- a/folly/futures/test/WaitTest.cpp +++ b/folly/futures/test/WaitTest.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + #include #include diff --git a/folly/futures/test/WhileDoTest.cpp b/folly/futures/test/WhileDoTest.cpp index 70acf849..a34f982f 100644 --- a/folly/futures/test/WhileDoTest.cpp +++ b/folly/futures/test/WhileDoTest.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include -- 2.34.1