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

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
parent db0ea224
...@@ -54,7 +54,7 @@ size_t SharedPromise<T>::size() { ...@@ -54,7 +54,7 @@ size_t SharedPromise<T>::size() {
} }
template <class T> template <class T>
Future<T> SharedPromise<T>::getFuture() { SemiFuture<T> SharedPromise<T>::getSemiFuture() {
std::lock_guard<std::mutex> g(mutex_); std::lock_guard<std::mutex> g(mutex_);
size_++; size_++;
if (hasValue_) { if (hasValue_) {
...@@ -64,10 +64,15 @@ Future<T> SharedPromise<T>::getFuture() { ...@@ -64,10 +64,15 @@ Future<T> SharedPromise<T>::getFuture() {
if (interruptHandler_) { if (interruptHandler_) {
promises_.back().setInterruptHandler(interruptHandler_); promises_.back().setInterruptHandler(interruptHandler_);
} }
return promises_.back().getFuture(); return promises_.back().getSemiFuture();
} }
} }
template <class T>
Future<T> SharedPromise<T>::getFuture() {
return getSemiFuture().via(&folly::InlineExecutor::instance());
}
template <class T> template <class T>
template <class E> template <class E>
typename std::enable_if<std::is_base_of<std::exception, E>::value>::type typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#pragma once #pragma once
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/executors/InlineExecutor.h>
#include <folly/futures/Promise.h> #include <folly/futures/Promise.h>
namespace folly { namespace folly {
...@@ -50,6 +51,15 @@ class SharedPromise { ...@@ -50,6 +51,15 @@ class SharedPromise {
* Return a Future tied to the shared core state. Unlike Promise::getFuture, * Return a Future tied to the shared core state. Unlike Promise::getFuture,
* this can be called an unlimited number of times per SharedPromise. * this can be called an unlimited number of times per SharedPromise.
*/ */
SemiFuture<T> 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<T> getFuture(); Future<T> getFuture();
/** Return the number of Futures associated with this SharedPromise */ /** Return the number of Futures associated with this SharedPromise */
......
...@@ -19,6 +19,24 @@ ...@@ -19,6 +19,24 @@
using namespace folly; using namespace folly;
TEST(SharedPromise, setGetSemiFuture) {
SharedPromise<int> 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<int> 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) { TEST(SharedPromise, setGet) {
SharedPromise<int> p; SharedPromise<int> p;
p.setValue(1); p.setValue(1);
......
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