Decouple future and fiber for mobile
authorFrancis Ma <fma@fb.com>
Thu, 14 Jan 2016 22:07:21 +0000 (14:07 -0800)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Thu, 14 Jan 2016 23:20:27 +0000 (15:20 -0800)
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
folly/futures/Future-inl.h
folly/futures/test/TimesTest.cpp
folly/futures/test/WaitTest.cpp
folly/futures/test/WhileDoTest.cpp

index 6737efc52b64e1086c6643257d9daf0e1675eb51..56ee49a34c87adcac34be0b84f570345fcc3557b 100644 (file)
@@ -217,6 +217,13 @@ struct Baton {
     }
   }
 
+  /// Similar to timed_wait, but with a duration.
+  template <typename Clock = std::chrono::steady_clock, typename Duration>
+  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:
index 3a919ff489231c1b5a110cb1b2b3ad6571ec9077..fd2c71532d35951a37e20290d77873c599157109 100644 (file)
 #include <chrono>
 #include <random>
 #include <thread>
-#include <folly/experimental/fibers/Baton.h>
+#include <folly/Baton.h>
 #include <folly/Optional.h>
 #include <folly/Random.h>
 #include <folly/Traits.h>
 #include <folly/futures/detail/Core.h>
 #include <folly/futures/Timekeeper.h>
 
+#if defined(__ANDROID__) || defined(__APPLE__)
+#define FOLLY_FUTURE_USING_FIBER 0
+#else
+#define FOLLY_FUTURE_USING_FIBER 1
+#include <folly/experimental/fibers/Baton.h>
+#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<Timekeeper> getTimekeeperSingleton();
 }
@@ -937,7 +952,7 @@ void waitImpl(Future<T>& f) {
   // short-circuit if there's nothing to do
   if (f.isReady()) return;
 
-  folly::fibers::Baton baton;
+  FutureBatonType baton;
   f.setCallback_([&](const Try<T>& t) { baton.post(); });
   baton.wait();
   assert(f.isReady());
@@ -950,7 +965,7 @@ void waitImpl(Future<T>& f, Duration dur) {
 
   folly::MoveWrapper<Promise<T>> promise;
   auto ret = promise->getFuture();
-  auto baton = std::make_shared<folly::fibers::Baton>();
+  auto baton = std::make_shared<FutureBatonType>();
   f.setCallback_([baton, promise](Try<T>&& t) mutable {
     promise->setTry(std::move(t));
     baton->post();
index a9c63a8249d4df96d8e22f275c3bdf16262174bb..6f3e9da9cbf1d3461616de41a0f44d4c8cabbe2b 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <memory>
 #include <mutex>
+#include <queue>
 
 #include <gtest/gtest.h>
 #include <glog/logging.h>
index 57f2f8392a1a6d65930df0245a8088267345a754..2d33569106c03255cb6188b9f2a4cde7805dab73 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <queue>
+
 #include <gtest/gtest.h>
 
 #include <folly/futures/Future.h>
index 70acf849724cfdf3f65e97a897e653af43f90d96..a34f982f19bbcb8fad4d562281c87343535c1338 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <memory>
 #include <mutex>
+#include <queue>
 
 #include <gtest/gtest.h>
 #include <glog/logging.h>