Commit 51a23742 authored by Lee Howes's avatar Lee Howes Committed by Facebook Github Bot

Make Future<T>::delayed complete on correct executor 1/n

Summary:
Overall plan to modify Future<T>::delayed to complete on the same
executor as the input future.

1/n: Adds Future<T>::delayedUnsafe that is identical to Future<T>::delayed
but acts as a codemod target.

Reviewed By: marshallcline

Differential Revision: D8209451

fbshipit-source-id: ee9c1184de2847e142516b315140fcc6d43948c5
parent f0047766
...@@ -1747,6 +1747,16 @@ Future<T> Future<T>::delayed(Duration dur, Timekeeper* tk) { ...@@ -1747,6 +1747,16 @@ Future<T> Future<T>::delayed(Duration dur, Timekeeper* tk) {
}); });
} }
template <class T>
Future<T> Future<T>::delayedUnsafe(Duration dur, Timekeeper* tk) {
return collectAllSemiFuture(*this, futures::sleep(dur, tk))
.toUnsafeFuture()
.then([](std::tuple<Try<T>, Try<Unit>> tup) {
Try<T>& t = std::get<0>(tup);
return makeFuture<T>(std::move(t));
});
}
namespace futures { namespace futures {
namespace detail { namespace detail {
......
...@@ -939,6 +939,12 @@ class Future : private futures::detail::FutureBase<T> { ...@@ -939,6 +939,12 @@ class Future : private futures::detail::FutureBase<T> {
/// now. The optional Timekeeper is as with futures::sleep(). /// now. The optional Timekeeper is as with futures::sleep().
Future<T> delayed(Duration, Timekeeper* = nullptr); Future<T> delayed(Duration, Timekeeper* = nullptr);
/// Delay the completion of this Future for at least this duration from
/// now. The optional Timekeeper is as with futures::sleep().
/// NOTE: Deprecated
/// WARNING: Returned future may complete on Timekeeper thread.
Future<T> delayedUnsafe(Duration, Timekeeper* = nullptr);
/// Block until the future is fulfilled. Returns the value (moved out), or /// Block until the future is fulfilled. Returns the value (moved out), or
/// throws the exception. The future must not already have a callback. /// throws the exception. The future must not already have a callback.
T get(); T get();
......
...@@ -108,6 +108,14 @@ TEST(Timekeeper, futureDelayed) { ...@@ -108,6 +108,14 @@ TEST(Timekeeper, futureDelayed) {
EXPECT_GE(dur, one_ms); EXPECT_GE(dur, one_ms);
} }
TEST(Timekeeper, futureDelayedUnsafe) {
auto t1 = now();
auto dur =
makeFuture().delayedUnsafe(one_ms).then([=] { return now() - t1; }).get();
EXPECT_GE(dur, one_ms);
}
TEST(Timekeeper, futureWithinThrows) { TEST(Timekeeper, futureWithinThrows) {
Promise<int> p; Promise<int> p;
auto f = auto f =
......
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