Commit a0a9fc45 authored by James Sedgwick's avatar James Sedgwick Committed by Alecs King

fulfil -> setWith, fulfilTry -> setTry

Summary: title

Test Plan: tests

Reviewed By: hans@fb.com

Subscribers: cgist, cold-storage-diffs@, fugalh, atlas2-eng@, zhuohuang, folly-diffs@, jsedgwick, yfeldblum, chalfant, andrii

FB internal diff: D1994472

Tasks: 6768508

Signature: t1:1994472:1429117362:218c4fac3c88fcc8d37dc22ff8fe4135a73ec5d5
parent fa78ffbc
...@@ -53,11 +53,11 @@ Promise<T>::~Promise() { ...@@ -53,11 +53,11 @@ Promise<T>::~Promise() {
template <class T> template <class T>
void Promise<T>::setException(folly::exception_wrapper e) { void Promise<T>::setException(folly::exception_wrapper e) {
fulfilTry(folly::Try<T>(e)); setTry(folly::Try<T>(e));
} }
template <class T> template <class T>
void Promise<T>::fulfilTry(folly::Try<T>&& t) { void Promise<T>::setTry(folly::Try<T>&& t) {
throwIfFulfilled(); throwIfFulfilled();
*value_ = std::move(t); *value_ = std::move(t);
...@@ -73,7 +73,7 @@ void Promise<T>::setValue(M&& v) { ...@@ -73,7 +73,7 @@ void Promise<T>::setValue(M&& v) {
static_assert(!std::is_same<T, void>::value, static_assert(!std::is_same<T, void>::value,
"Use setValue() instead"); "Use setValue() instead");
fulfilTry(folly::Try<T>(std::forward<M>(v))); setTry(folly::Try<T>(std::forward<M>(v)));
} }
template <class T> template <class T>
...@@ -81,13 +81,13 @@ void Promise<T>::setValue() { ...@@ -81,13 +81,13 @@ void Promise<T>::setValue() {
static_assert(std::is_same<T, void>::value, static_assert(std::is_same<T, void>::value,
"Use setValue(value) instead"); "Use setValue(value) instead");
fulfilTry(folly::Try<void>()); setTry(folly::Try<void>());
} }
template <class T> template <class T>
template <class F> template <class F>
void Promise<T>::fulfil(F&& func) { void Promise<T>::setWith(F&& func) {
fulfilTry(makeTryFunction(std::forward<F>(func))); setTry(makeTryFunction(std::forward<F>(func)));
} }
}} }}
...@@ -41,7 +41,7 @@ class Promise { ...@@ -41,7 +41,7 @@ class Promise {
Promise(Promise&&) noexcept; Promise(Promise&&) noexcept;
Promise& operator=(Promise&&); Promise& operator=(Promise&&);
/** Fulfil this promise (only for Promise<void>) */ /** Fulfill this promise (only for Promise<void>) */
void setValue(); void setValue();
/** Set the value (use perfect forwarding for both move and copy) */ /** Set the value (use perfect forwarding for both move and copy) */
...@@ -53,18 +53,18 @@ class Promise { ...@@ -53,18 +53,18 @@ class Promise {
* *
* @param t * @param t
*/ */
void fulfilTry(folly::Try<T>&& t); void setTry(folly::Try<T>&& t);
/** Fulfil this promise with the result of a function that takes no /** Fulfill this promise with the result of a function that takes no
arguments and returns something implicitly convertible to T. arguments and returns something implicitly convertible to T.
Captures exceptions. e.g. Captures exceptions. e.g.
p.fulfil([] { do something that may throw; return a T; }); p.setWith([] { do something that may throw; return a T; });
*/ */
template <class F> template <class F>
void fulfil(F&& func); void setWith(F&& func);
/** Fulfil the Promise with an exception_wrapper, e.g. /** Fulfill the Promise with an exception_wrapper, e.g.
auto ew = folly::try_and_catch<std::exception>([]{ ... }); auto ew = folly::try_and_catch<std::exception>([]{ ... });
if (ew) { if (ew) {
p.setException(std::move(ew)); p.setException(std::move(ew));
......
...@@ -171,7 +171,7 @@ Future<T>::thenImplementation(F func, detail::argResult<isTry, F, Args...>) { ...@@ -171,7 +171,7 @@ Future<T>::thenImplementation(F func, detail::argResult<isTry, F, Args...>) {
if (!isTry && t.hasException()) { if (!isTry && t.hasException()) {
p->setException(std::move(t.exception())); p->setException(std::move(t.exception()));
} else { } else {
p->fulfil([&]() { p->setWith([&]() {
return (*funcm)(t.template get<isTry, Args>()...); return (*funcm)(t.template get<isTry, Args>()...);
}); });
} }
...@@ -210,7 +210,7 @@ Future<T>::thenImplementation(F func, detail::argResult<isTry, F, Args...>) { ...@@ -210,7 +210,7 @@ Future<T>::thenImplementation(F func, detail::argResult<isTry, F, Args...>) {
auto f2 = (*funcm)(t.template get<isTry, Args>()...); auto f2 = (*funcm)(t.template get<isTry, Args>()...);
// that didn't throw, now we can steal p // that didn't throw, now we can steal p
f2.setCallback_([p](Try<B>&& b) mutable { f2.setCallback_([p](Try<B>&& b) mutable {
p->fulfilTry(std::move(b)); p->setTry(std::move(b));
}); });
} catch (const std::exception& e) { } catch (const std::exception& e) {
p->setException(exception_wrapper(std::current_exception(), e)); p->setException(exception_wrapper(std::current_exception(), e));
...@@ -259,11 +259,11 @@ Future<T>::onError(F&& func) { ...@@ -259,11 +259,11 @@ Future<T>::onError(F&& func) {
auto funcm = folly::makeMoveWrapper(std::move(func)); auto funcm = folly::makeMoveWrapper(std::move(func));
setCallback_([pm, funcm](Try<T>&& t) mutable { setCallback_([pm, funcm](Try<T>&& t) mutable {
if (!t.template withException<Exn>([&] (Exn& e) { if (!t.template withException<Exn>([&] (Exn& e) {
pm->fulfil([&]{ pm->setWith([&]{
return (*funcm)(e); return (*funcm)(e);
}); });
})) { })) {
pm->fulfilTry(std::move(t)); pm->setTry(std::move(t));
} }
}); });
...@@ -292,7 +292,7 @@ Future<T>::onError(F&& func) { ...@@ -292,7 +292,7 @@ Future<T>::onError(F&& func) {
try { try {
auto f2 = (*funcm)(e); auto f2 = (*funcm)(e);
f2.setCallback_([pm](Try<T>&& t2) mutable { f2.setCallback_([pm](Try<T>&& t2) mutable {
pm->fulfilTry(std::move(t2)); pm->setTry(std::move(t2));
}); });
} catch (const std::exception& e2) { } catch (const std::exception& e2) {
pm->setException(exception_wrapper(std::current_exception(), e2)); pm->setException(exception_wrapper(std::current_exception(), e2));
...@@ -300,7 +300,7 @@ Future<T>::onError(F&& func) { ...@@ -300,7 +300,7 @@ Future<T>::onError(F&& func) {
pm->setException(exception_wrapper(std::current_exception())); pm->setException(exception_wrapper(std::current_exception()));
} }
})) { })) {
pm->fulfilTry(std::move(t)); pm->setTry(std::move(t));
} }
}); });
...@@ -436,7 +436,7 @@ inline Future<T> Future<T>::via(Executor* executor) & { ...@@ -436,7 +436,7 @@ inline Future<T> Future<T>::via(Executor* executor) & {
MoveWrapper<Promise<T>> p; MoveWrapper<Promise<T>> p;
auto f = p->getFuture(); auto f = p->getFuture();
then([p](Try<T>&& t) mutable { p->fulfilTry(std::move(t)); }); then([p](Try<T>&& t) mutable { p->setTry(std::move(t)); });
return std::move(f).via(executor); return std::move(f).via(executor);
} }
...@@ -473,7 +473,7 @@ auto makeFutureTry( ...@@ -473,7 +473,7 @@ auto makeFutureTry(
typename std::enable_if<!std::is_reference<F>::value, bool>::type sdf) typename std::enable_if<!std::is_reference<F>::value, bool>::type sdf)
-> Future<decltype(func())> { -> Future<decltype(func())> {
Promise<decltype(func())> p; Promise<decltype(func())> p;
p.fulfil( p.setWith(
[&func]() { [&func]() {
return (func)(); return (func)();
}); });
...@@ -512,7 +512,7 @@ makeFuture(E const& e) { ...@@ -512,7 +512,7 @@ makeFuture(E const& e) {
template <class T> template <class T>
Future<T> makeFuture(Try<T>&& t) { Future<T> makeFuture(Try<T>&& t) {
Promise<typename std::decay<T>::type> p; Promise<typename std::decay<T>::type> p;
p.fulfilTry(std::move(t)); p.setTry(std::move(t));
return p.getFuture(); return p.getFuture();
} }
...@@ -627,7 +627,7 @@ whenN(InputIterator first, InputIterator last, size_t n) { ...@@ -627,7 +627,7 @@ whenN(InputIterator first, InputIterator last, size_t n) {
ctx->completed = 0; ctx->completed = 0;
// for each completed Future, increase count and add to vector, until we // for each completed Future, increase count and add to vector, until we
// have n completed futures at which point we fulfil our Promise with the // have n completed futures at which point we fulfill our Promise with the
// vector // vector
auto it = first; auto it = first;
size_t i = 0; size_t i = 0;
...@@ -639,7 +639,7 @@ whenN(InputIterator first, InputIterator last, size_t n) { ...@@ -639,7 +639,7 @@ whenN(InputIterator first, InputIterator last, size_t n) {
assert(ctx->v.size() < n); assert(ctx->v.size() < n);
v.push_back(std::make_pair(i, std::move(t))); v.push_back(std::make_pair(i, std::move(t)));
if (c == n) { if (c == n) {
ctx->p.fulfilTry(Try<V>(std::move(v))); ctx->p.setTry(Try<V>(std::move(v)));
} }
} }
}); });
...@@ -736,7 +736,7 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) { ...@@ -736,7 +736,7 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
this->then([ctx](Try<T>&& t) { this->then([ctx](Try<T>&& t) {
if (ctx->token.exchange(true) == false) { if (ctx->token.exchange(true) == false) {
ctx->promise.fulfilTry(std::move(t)); ctx->promise.setTry(std::move(t));
} }
}); });
...@@ -923,7 +923,7 @@ namespace futures { ...@@ -923,7 +923,7 @@ namespace futures {
MoveWrapper<Promise<A>> pw; MoveWrapper<Promise<A>> pw;
MoveWrapper<Future<Z>> fw(chainHelper<Z>(pw->getFuture(), fns...)); MoveWrapper<Future<Z>> fw(chainHelper<Z>(pw->getFuture(), fns...));
return [=](Try<A> t) mutable { return [=](Try<A> t) mutable {
pw->fulfilTry(std::move(t)); pw->setTry(std::move(t));
return std::move(*fw); return std::move(*fw);
}; };
} }
......
...@@ -109,7 +109,7 @@ void Promise<T>::setInterruptHandler( ...@@ -109,7 +109,7 @@ void Promise<T>::setInterruptHandler(
} }
template <class T> template <class T>
void Promise<T>::fulfilTry(Try<T> t) { void Promise<T>::setTry(Try<T> t) {
throwIfFulfilled(); throwIfFulfilled();
core_->setResult(std::move(t)); core_->setResult(std::move(t));
} }
...@@ -120,7 +120,7 @@ void Promise<T>::setValue(M&& v) { ...@@ -120,7 +120,7 @@ void Promise<T>::setValue(M&& v) {
static_assert(!std::is_same<T, void>::value, static_assert(!std::is_same<T, void>::value,
"Use setValue() instead"); "Use setValue() instead");
fulfilTry(Try<T>(std::forward<M>(v))); setTry(Try<T>(std::forward<M>(v)));
} }
template <class T> template <class T>
...@@ -128,14 +128,14 @@ void Promise<T>::setValue() { ...@@ -128,14 +128,14 @@ void Promise<T>::setValue() {
static_assert(std::is_same<T, void>::value, static_assert(std::is_same<T, void>::value,
"Use setValue(value) instead"); "Use setValue(value) instead");
fulfilTry(Try<void>()); setTry(Try<void>());
} }
template <class T> template <class T>
template <class F> template <class F>
void Promise<T>::fulfil(F&& func) { void Promise<T>::setWith(F&& func) {
throwIfFulfilled(); throwIfFulfilled();
fulfilTry(makeTryFunction(std::forward<F>(func))); setTry(makeTryFunction(std::forward<F>(func)));
} }
} }
...@@ -43,10 +43,10 @@ public: ...@@ -43,10 +43,10 @@ public:
once, thereafter Future already retrieved exception will be raised. */ once, thereafter Future already retrieved exception will be raised. */
Future<T> getFuture(); Future<T> getFuture();
/** Fulfil the Promise with an exception_wrapper */ /** Fulfill the Promise with an exception_wrapper */
void setException(exception_wrapper ew); void setException(exception_wrapper ew);
/** Fulfil the Promise with an exception_ptr, e.g. /** Fulfill the Promise with an exception_ptr, e.g.
try { try {
... ...
} catch (...) { } catch (...) {
...@@ -55,7 +55,7 @@ public: ...@@ -55,7 +55,7 @@ public:
*/ */
void setException(std::exception_ptr const&) DEPRECATED; void setException(std::exception_ptr const&) DEPRECATED;
/** Fulfil the Promise with an exception type E, which can be passed to /** Fulfill the Promise with an exception type E, which can be passed to
std::make_exception_ptr(). Useful for originating exceptions. If you std::make_exception_ptr(). Useful for originating exceptions. If you
caught an exception the exception_wrapper form is more appropriate. caught an exception the exception_wrapper form is more appropriate.
*/ */
...@@ -65,28 +65,28 @@ public: ...@@ -65,28 +65,28 @@ public:
/// Set an interrupt handler to handle interrupts. See the documentation for /// Set an interrupt handler to handle interrupts. See the documentation for
/// Future::raise(). Your handler can do whatever it wants, but if you /// Future::raise(). Your handler can do whatever it wants, but if you
/// bother to set one then you probably will want to fulfil the promise with /// bother to set one then you probably will want to fulfill the promise with
/// an exception (or special value) indicating how the interrupt was /// an exception (or special value) indicating how the interrupt was
/// handled. /// handled.
void setInterruptHandler(std::function<void(exception_wrapper const&)>); void setInterruptHandler(std::function<void(exception_wrapper const&)>);
/** Fulfil this Promise (only for Promise<void>) */ /** Fulfill this Promise (only for Promise<void>) */
void setValue(); void setValue();
/** Set the value (use perfect forwarding for both move and copy) */ /** Set the value (use perfect forwarding for both move and copy) */
template <class M> template <class M>
void setValue(M&& value); void setValue(M&& value);
void fulfilTry(Try<T> t); void setTry(Try<T> t);
/** Fulfil this Promise with the result of a function that takes no /** Fulfill this Promise with the result of a function that takes no
arguments and returns something implicitly convertible to T. arguments and returns something implicitly convertible to T.
Captures exceptions. e.g. Captures exceptions. e.g.
p.fulfil([] { do something that may throw; return a T; }); p.setWith([] { do something that may throw; return a T; });
*/ */
template <class F> template <class F>
void fulfil(F&& func); void setWith(F&& func);
private: private:
typedef typename Future<T>::corePtr corePtr; typedef typename Future<T>::corePtr corePtr;
......
...@@ -193,7 +193,7 @@ f2.then(y3); // racy ...@@ -193,7 +193,7 @@ f2.then(y3); // racy
## You make me Promises, Promises ## You make me Promises, Promises
If you are wrapping an asynchronous operation, or providing an asynchronous API to users, then you will want to make Promises. Every Future has a corresponding Promise (except Futures that spring into existence already completed, with `makeFuture()`). Promises are simple, you make one, you extract the Future, and you fulfil it with a value or an exception. Example: If you are wrapping an asynchronous operation, or providing an asynchronous API to users, then you will want to make Promises. Every Future has a corresponding Promise (except Futures that spring into existence already completed, with `makeFuture()`). Promises are simple, you make one, you extract the Future, and you fulfill it with a value or an exception. Example:
```C++ ```C++
Promise<int> p; Promise<int> p;
...@@ -221,11 +221,11 @@ f.isReady() == true ...@@ -221,11 +221,11 @@ f.isReady() == true
f.value() // throws the exception f.value() // throws the exception
``` ```
It's good practice to use fulfil which takes a function and automatically captures exceptions, e.g. It's good practice to use setWith which takes a function and automatically captures exceptions, e.g.
```C++ ```C++
Promise<int> p; Promise<int> p;
p.fulfil([]{ p.setWith([]{
try { try {
// do stuff that may throw // do stuff that may throw
return 42; return 42;
......
...@@ -68,7 +68,7 @@ enum class State : uint8_t { ...@@ -68,7 +68,7 @@ enum class State : uint8_t {
/// migrate between threads, though this usually happens within the API code. /// migrate between threads, though this usually happens within the API code.
/// For example, an async operation will probably make a Promise, grab its /// For example, an async operation will probably make a Promise, grab its
/// Future, then move the Promise into another thread that will eventually /// Future, then move the Promise into another thread that will eventually
/// fulfil it. With executors and via, this gets slightly more complicated at /// fulfill it. With executors and via, this gets slightly more complicated at
/// first blush, but it's the same principle. In general, as long as the user /// first blush, but it's the same principle. In general, as long as the user
/// doesn't access a Future or Promise object from more than one thread at a /// doesn't access a Future or Promise object from more than one thread at a
/// time there won't be any problems. /// time there won't be any problems.
......
...@@ -83,7 +83,7 @@ BENCHMARK_RELATIVE(hundredThens) { ...@@ -83,7 +83,7 @@ BENCHMARK_RELATIVE(hundredThens) {
someThens(100); someThens(100);
} }
// Lock contention. Although in practice fulfil()s tend to be temporally // Lock contention. Although in practice fulfills tend to be temporally
// separate from then()s, still sometimes they will be concurrent. So the // separate from then()s, still sometimes they will be concurrent. So the
// higher this number is, the better. // higher this number is, the better.
BENCHMARK_DRAW_LINE() BENCHMARK_DRAW_LINE()
...@@ -113,7 +113,7 @@ BENCHMARK(no_contention) { ...@@ -113,7 +113,7 @@ BENCHMARK(no_contention) {
b2.wait(); b2.wait();
} }
// The only thing we are measuring is how long fulfil + callbacks take // The only thing we are measuring is how long fulfill + callbacks take
producer.join(); producer.join();
} }
......
...@@ -617,17 +617,17 @@ TEST(Promise, setException) { ...@@ -617,17 +617,17 @@ TEST(Promise, setException) {
} }
} }
TEST(Promise, fulfil) { TEST(Promise, setWith) {
{ {
Promise<int> p; Promise<int> p;
auto f = p.getFuture(); auto f = p.getFuture();
p.fulfil([] { return 42; }); p.setWith([] { return 42; });
EXPECT_EQ(42, f.value()); EXPECT_EQ(42, f.value());
} }
{ {
Promise<int> p; Promise<int> p;
auto f = p.getFuture(); auto f = p.getFuture();
p.fulfil([]() -> int { throw eggs; }); p.setWith([]() -> int { throw eggs; });
EXPECT_THROW(f.value(), eggs_t); EXPECT_THROW(f.value(), eggs_t);
} }
} }
...@@ -1240,7 +1240,7 @@ TEST(Future, getFuture_after_setValue) { ...@@ -1240,7 +1240,7 @@ TEST(Future, getFuture_after_setValue) {
TEST(Future, getFuture_after_setException) { TEST(Future, getFuture_after_setException) {
Promise<void> p; Promise<void> p;
p.fulfil([]() -> void { throw std::logic_error("foo"); }); p.setWith([]() -> void { throw std::logic_error("foo"); });
EXPECT_THROW(p.getFuture().value(), std::logic_error); EXPECT_THROW(p.getFuture().value(), std::logic_error);
} }
...@@ -1302,7 +1302,7 @@ TEST(Future, context) { ...@@ -1302,7 +1302,7 @@ TEST(Future, context) {
EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test")); EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test"));
// Fulfil the promise // Fulfill the promise
p.setValue(); p.setValue();
} }
...@@ -1354,7 +1354,7 @@ TEST(Future, CircularDependencySharedPtrSelfReset) { ...@@ -1354,7 +1354,7 @@ TEST(Future, CircularDependencySharedPtrSelfReset) {
ptr.reset(); ptr.reset();
promise.fulfil([]{return 1l;}); promise.setWith([]{return 1l;});
} }
TEST(Future, Constructor) { TEST(Future, Constructor) {
......
...@@ -36,7 +36,7 @@ TEST(Sugar, pollNotReady) { ...@@ -36,7 +36,7 @@ TEST(Sugar, pollNotReady) {
TEST(Sugar, pollException) { TEST(Sugar, pollException) {
Promise<void> p; Promise<void> p;
auto f = p.getFuture(); auto f = p.getFuture();
p.fulfil([] { throw std::runtime_error("Runtime"); }); p.setWith([] { throw std::runtime_error("Runtime"); });
EXPECT_TRUE(f.poll().value().hasException()); EXPECT_TRUE(f.poll().value().hasException());
} }
......
...@@ -58,7 +58,7 @@ class OutputBufferingHandler : public BytesToBytesHandler, ...@@ -58,7 +58,7 @@ class OutputBufferingHandler : public BytesToBytesHandler,
MoveWrapper<std::vector<Promise<void>>> promises(std::move(promises_)); MoveWrapper<std::vector<Promise<void>>> promises(std::move(promises_));
ctx_->fireWrite(std::move(sends_)).then([promises](Try<void> t) mutable { ctx_->fireWrite(std::move(sends_)).then([promises](Try<void> t) mutable {
for (auto& p : *promises) { for (auto& p : *promises) {
p.fulfilTry(t); p.setTry(t);
} }
}); });
} }
......
...@@ -46,7 +46,7 @@ class FutureExecutor : public ExecutorImpl { ...@@ -46,7 +46,7 @@ class FutureExecutor : public ExecutorImpl {
auto moveFunc = folly::makeMoveWrapper(std::move(func)); auto moveFunc = folly::makeMoveWrapper(std::move(func));
ExecutorImpl::add([movePromise, moveFunc] () mutable { ExecutorImpl::add([movePromise, moveFunc] () mutable {
(*moveFunc)().then([movePromise] (Try<T>&& t) mutable { (*moveFunc)().then([movePromise] (Try<T>&& t) mutable {
movePromise->fulfilTry(std::move(t)); movePromise->setTry(std::move(t));
}); });
}); });
return future; return future;
...@@ -70,7 +70,7 @@ class FutureExecutor : public ExecutorImpl { ...@@ -70,7 +70,7 @@ class FutureExecutor : public ExecutorImpl {
auto movePromise = folly::makeMoveWrapper(std::move(promise)); auto movePromise = folly::makeMoveWrapper(std::move(promise));
auto moveFunc = folly::makeMoveWrapper(std::move(func)); auto moveFunc = folly::makeMoveWrapper(std::move(func));
ExecutorImpl::add([movePromise, moveFunc] () mutable { ExecutorImpl::add([movePromise, moveFunc] () mutable {
movePromise->fulfil(std::move(*moveFunc)); movePromise->setWith(std::move(*moveFunc));
}); });
return future; return future;
} }
......
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