From a5b5328336d1d59c52bb505ed14820cdb7e14fbf Mon Sep 17 00:00:00 2001 From: Lee Howes Date: Tue, 26 Dec 2017 20:11:48 -0800 Subject: [PATCH] Add getSemiFuture to folly::SharedPromise Summary: Adds getSemiFuture functionality to folly::SharedPromise. Implements getFuture in terms of this, using folly::InlineExecutor to ensure that there is no change of behaviour. Reviewed By: yfeldblum Differential Revision: D6628723 fbshipit-source-id: 0ce7c7773b9290998ce87f84fa5d82ba957f0313 --- folly/futures/SharedPromise-inl.h | 9 +++++++-- folly/futures/SharedPromise.h | 10 ++++++++++ folly/futures/test/SharedPromiseTest.cpp | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/folly/futures/SharedPromise-inl.h b/folly/futures/SharedPromise-inl.h index 362736ec..3c9af5a6 100644 --- a/folly/futures/SharedPromise-inl.h +++ b/folly/futures/SharedPromise-inl.h @@ -54,7 +54,7 @@ size_t SharedPromise::size() { } template -Future SharedPromise::getFuture() { +SemiFuture SharedPromise::getSemiFuture() { std::lock_guard g(mutex_); size_++; if (hasValue_) { @@ -64,10 +64,15 @@ Future SharedPromise::getFuture() { if (interruptHandler_) { promises_.back().setInterruptHandler(interruptHandler_); } - return promises_.back().getFuture(); + return promises_.back().getSemiFuture(); } } +template +Future SharedPromise::getFuture() { + return getSemiFuture().via(&folly::InlineExecutor::instance()); +} + template template typename std::enable_if::value>::type diff --git a/folly/futures/SharedPromise.h b/folly/futures/SharedPromise.h index d3b363ef..70579267 100644 --- a/folly/futures/SharedPromise.h +++ b/folly/futures/SharedPromise.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include namespace folly { @@ -50,6 +51,15 @@ class SharedPromise { * Return a Future tied to the shared core state. Unlike Promise::getFuture, * this can be called an unlimited number of times per SharedPromise. */ + SemiFuture getSemiFuture(); + + /** + * Return a Future tied to the shared core state. Unlike Promise::getFuture, + * this can be called an unlimited number of times per SharedPromise. + * NOTE: This function is deprecated. Please use getSemiFuture and pass the + * appropriate executor to .via on the returned SemiFuture to get a + * valid Future where necessary. + */ Future getFuture(); /** Return the number of Futures associated with this SharedPromise */ diff --git a/folly/futures/test/SharedPromiseTest.cpp b/folly/futures/test/SharedPromiseTest.cpp index 3cb878dd..f49bdd43 100644 --- a/folly/futures/test/SharedPromiseTest.cpp +++ b/folly/futures/test/SharedPromiseTest.cpp @@ -19,6 +19,24 @@ using namespace folly; +TEST(SharedPromise, setGetSemiFuture) { + SharedPromise p; + p.setValue(1); + auto f1 = p.getSemiFuture(); + auto f2 = p.getSemiFuture(); + EXPECT_EQ(1, f1.value()); + EXPECT_EQ(1, f2.value()); +} + +TEST(SharedPromise, setGetMixed) { + SharedPromise p; + p.setValue(1); + auto f1 = p.getSemiFuture(); + auto f2 = p.getFuture(); + EXPECT_EQ(1, f1.value()); + EXPECT_EQ(1, f2.value()); +} + TEST(SharedPromise, setGet) { SharedPromise p; p.setValue(1); -- 2.34.1