Commit 138b7236 authored by Francis Ma's avatar Francis Ma Committed by facebook-github-bot-4

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
parent 76dabfa2
...@@ -217,6 +217,13 @@ struct Baton { ...@@ -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. /// Similar to wait, but doesn't block the thread if it hasn't been posted.
/// ///
/// try_wait has the following semantics: /// try_wait has the following semantics:
......
...@@ -21,17 +21,32 @@ ...@@ -21,17 +21,32 @@
#include <chrono> #include <chrono>
#include <random> #include <random>
#include <thread> #include <thread>
#include <folly/experimental/fibers/Baton.h> #include <folly/Baton.h>
#include <folly/Optional.h> #include <folly/Optional.h>
#include <folly/Random.h> #include <folly/Random.h>
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/futures/detail/Core.h> #include <folly/futures/detail/Core.h>
#include <folly/futures/Timekeeper.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 { namespace folly {
class Timekeeper; class Timekeeper;
namespace detail {
#if FOLLY_FUTURE_USING_FIBER
typedef folly::fibers::Baton FutureBatonType;
#else
typedef folly::Baton<> FutureBatonType;
#endif
}
namespace detail { namespace detail {
std::shared_ptr<Timekeeper> getTimekeeperSingleton(); std::shared_ptr<Timekeeper> getTimekeeperSingleton();
} }
...@@ -937,7 +952,7 @@ void waitImpl(Future<T>& f) { ...@@ -937,7 +952,7 @@ void waitImpl(Future<T>& f) {
// short-circuit if there's nothing to do // short-circuit if there's nothing to do
if (f.isReady()) return; if (f.isReady()) return;
folly::fibers::Baton baton; FutureBatonType baton;
f.setCallback_([&](const Try<T>& t) { baton.post(); }); f.setCallback_([&](const Try<T>& t) { baton.post(); });
baton.wait(); baton.wait();
assert(f.isReady()); assert(f.isReady());
...@@ -950,7 +965,7 @@ void waitImpl(Future<T>& f, Duration dur) { ...@@ -950,7 +965,7 @@ void waitImpl(Future<T>& f, Duration dur) {
folly::MoveWrapper<Promise<T>> promise; folly::MoveWrapper<Promise<T>> promise;
auto ret = promise->getFuture(); 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 { f.setCallback_([baton, promise](Try<T>&& t) mutable {
promise->setTry(std::move(t)); promise->setTry(std::move(t));
baton->post(); baton->post();
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <queue>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <glog/logging.h> #include <glog/logging.h>
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
#include <queue>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <folly/futures/Future.h> #include <folly/futures/Future.h>
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <queue>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <glog/logging.h> #include <glog/logging.h>
......
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