Commit c1a2ae85 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Let Pomise handle setting BrokenPromise

Summary:
[Folly] Let `Pomise` handle setting `BrokenPromise` in its dtor, since it is logically always the thing that invokes `setResult`, rather than having `Core` do it.

This splits up the various responsibilities a little better.

Reviewed By: marshallcline

Differential Revision: D7964264

fbshipit-source-id: a4871fd1a83a0a54d4a6c91fcb74c6df5aa592ba
parent ae57e3b7
...@@ -25,6 +25,18 @@ ...@@ -25,6 +25,18 @@
namespace folly { namespace folly {
namespace futures {
namespace detail {
template <typename T>
void coreDetachPromiseMaybeWithResult(Core<T>& core) {
if (!core.hasResult()) {
core.setResult(Try<T>(exception_wrapper(BrokenPromise(typeid(T).name()))));
}
core.detachPromise();
}
} // namespace detail
} // namespace futures
template <class T> template <class T>
Promise<T> Promise<T>::makeEmpty() noexcept { Promise<T> Promise<T>::makeEmpty() noexcept {
return Promise<T>(futures::detail::EmptyConstruct{}); return Promise<T>(futures::detail::EmptyConstruct{});
...@@ -68,7 +80,7 @@ void Promise<T>::detach() { ...@@ -68,7 +80,7 @@ void Promise<T>::detach() {
if (!retrieved_) { if (!retrieved_) {
core_->detachFuture(); core_->detachFuture();
} }
core_->detachPromise(); futures::detail::coreDetachPromiseMaybeWithResult(*core_);
core_ = nullptr; core_ = nullptr;
} }
} }
......
...@@ -208,11 +208,7 @@ class Core final { ...@@ -208,11 +208,7 @@ class Core final {
/// Called by a destructing Promise (in the Promise thread, by definition) /// Called by a destructing Promise (in the Promise thread, by definition)
void detachPromise() { void detachPromise() {
// detachPromise() and setResult() should never be called in parallel DCHECK(result_);
// so we don't need to protect this.
if (UNLIKELY(!result_)) {
setResult(Try<T>(exception_wrapper(BrokenPromise(typeid(T).name()))));
}
detachOne(); detachOne();
} }
...@@ -282,6 +278,7 @@ class Core final { ...@@ -282,6 +278,7 @@ class Core final {
~Core() { ~Core() {
DCHECK(attached_ == 0); DCHECK(attached_ == 0);
DCHECK(result_);
} }
// Helper class that stores a pointer to the `Core` object and calls // Helper class that stores a pointer to the `Core` object and calls
......
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