future / fiber integration
authorDave Watson <davejwatson@fb.com>
Wed, 22 Apr 2015 16:02:38 +0000 (09:02 -0700)
committerAlecs King <int@fb.com>
Mon, 27 Apr 2015 23:50:03 +0000 (16:50 -0700)
Summary:
make future::wait() use fiber's baton, so wait works in threads or in fibers.

Much cleaner than making a new FiberRequest type in thrift

Test Plan: tests

Reviewed By: andrii@fb.com

Subscribers: doug, alandau, bmatheny, mshneer, andrii, folly-diffs@, yitingli, yfeldblum, jsedgwick, chalfant

FB internal diff: D1996283

Signature: t1:1996283:1429144165:da5dc6b1f2a053a45efd39877e79169e3fba810c

folly/futures/Future-inl.h
folly/futures/test/FutureTest.cpp

index f141b268b431b7cba9acbe9816f4b0afca215b74..a4ec35f09284624737b0640ca2a29f1f8c92ed5c 100644 (file)
@@ -19,7 +19,7 @@
 #include <chrono>
 #include <thread>
 
-#include <folly/Baton.h>
+#include <folly/experimental/fibers/Baton.h>
 #include <folly/Optional.h>
 #include <folly/futures/detail/Core.h>
 #include <folly/futures/Timekeeper.h>
@@ -896,7 +896,7 @@ void waitImpl(Future<T>& f) {
   // short-circuit if there's nothing to do
   if (f.isReady()) return;
 
-  Baton<> baton;
+  folly::fibers::Baton baton;
   f = f.then([&](Try<T> t) {
     baton.post();
     return makeFuture(std::move(t));
@@ -916,7 +916,7 @@ void waitImpl(Future<T>& f, Duration dur) {
   // short-circuit if there's nothing to do
   if (f.isReady()) return;
 
-  auto baton = std::make_shared<Baton<>>();
+  auto baton = std::make_shared<folly::fibers::Baton>();
   f = f.then([baton](Try<T> t) {
     baton->post();
     return makeFuture(std::move(t));
@@ -925,7 +925,7 @@ void waitImpl(Future<T>& f, Duration dur) {
   // Let's preserve the invariant that if we did not timeout (timed_wait returns
   // true), then the returned Future is complete when it is returned to the
   // caller. We need to wait out the race for that Future to complete.
-  if (baton->timed_wait(std::chrono::system_clock::now() + dur)) {
+  if (baton->timed_wait(dur)) {
     while (!f.isReady()) {
       std::this_thread::yield();
     }
index f252925346ea3ccdb7de58ee9cdf0da1555a3a17..00a56be6d56aa93529f29fd62bb984db580d7186 100644 (file)
@@ -29,6 +29,7 @@
 #include <folly/futures/ManualExecutor.h>
 #include <folly/futures/DrivableExecutor.h>
 #include <folly/dynamic.h>
+#include <folly/Baton.h>
 #include <folly/MPMCQueue.h>
 
 #include <folly/io/async/EventBase.h>