From 4c7a8438a608fe4e0107c61fda9c29746e558008 Mon Sep 17 00:00:00 2001 From: Hans Fugal Date: Tue, 21 Oct 2014 10:24:03 -0700 Subject: [PATCH] (wangle) Core cleanup Summary: Just some cleaning hose in `detail::Core`. - rename `fulfil` to `setResult` - and `value_` to `result_` - remove unnecessary helper methods (I can be convinced to keep them if you like them) Test Plan: mechanical changes, tests still pass Reviewed By: davejwatson@fb.com Subscribers: trunkagent, folly-diffs@, net-systems@, fugalh, exa, njormrod FB internal diff: D1618161 --- folly/wangle/Future-inl.h | 2 +- folly/wangle/Promise-inl.h | 4 ++-- folly/wangle/detail/Core.h | 42 ++++++++++++++------------------------ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/folly/wangle/Future-inl.h b/folly/wangle/Future-inl.h index cf10e9f4..9db9ff5f 100644 --- a/folly/wangle/Future-inl.h +++ b/folly/wangle/Future-inl.h @@ -172,7 +172,7 @@ template typename std::add_lvalue_reference::type Future::value() { throwIfInvalid(); - return core_->value(); + return core_->getTry().value(); } template diff --git a/folly/wangle/Promise-inl.h b/folly/wangle/Promise-inl.h index 98993c7c..efb8edfa 100644 --- a/folly/wangle/Promise-inl.h +++ b/folly/wangle/Promise-inl.h @@ -85,13 +85,13 @@ void Promise::setException(E const& e) { template void Promise::setException(std::exception_ptr const& e) { throwIfFulfilled(); - core_->setException(e); + core_->setResult(Try(e)); } template void Promise::fulfilTry(Try&& t) { throwIfFulfilled(); - core_->fulfil(std::move(t)); + core_->setResult(std::move(t)); } template diff --git a/folly/wangle/detail/Core.h b/folly/wangle/detail/Core.h index 057e873f..1cfdcc13 100644 --- a/folly/wangle/detail/Core.h +++ b/folly/wangle/detail/Core.h @@ -58,7 +58,11 @@ class Core { Core& operator=(Core&&) = delete; Try& getTry() { - return *value_; + if (ready()) { + return *result_; + } else { + throw FutureNotReady(); + } } template @@ -76,39 +80,23 @@ class Core { maybeCallback(); } - void fulfil(Try&& t) { + void setResult(Try&& t) { { std::lock_guard lock(mutex_); if (ready()) { - throw std::logic_error("fulfil called twice"); + throw std::logic_error("setResult called twice"); } - value_ = std::move(t); + result_ = std::move(t); assert(ready()); } maybeCallback(); } - void setException(std::exception_ptr const& e) { - fulfil(Try(e)); - } - - template void setException(E const& e) { - fulfil(Try(std::make_exception_ptr(e))); - } - bool ready() const { - return value_.hasValue(); - } - - typename std::add_lvalue_reference::type value() { - if (ready()) { - return value_->value(); - } else { - throw FutureNotReady(); - } + return result_.hasValue(); } // Called by a destructing Future @@ -123,7 +111,7 @@ class Core { // Called by a destructing Promise void detachPromise() { if (!ready()) { - setException(BrokenPromise()); + setResult(Try(std::make_exception_ptr(BrokenPromise()))); } detachOne(); } @@ -152,17 +140,17 @@ class Core { void maybeCallback() { std::unique_lock lock(mutex_); if (!calledBack_ && - value_ && callback_ && isActive()) { + result_ && callback_ && isActive()) { // TODO(5306911) we should probably try/catch here if (executor_) { - MoveWrapper>> val(std::move(value_)); + MoveWrapper>> val(std::move(result_)); MoveWrapper&&)>> cb(std::move(callback_)); executor_->add([cb, val]() mutable { (*cb)(std::move(**val)); }); calledBack_ = true; } else { calledBack_ = true; lock.unlock(); - callback_(std::move(*value_)); + callback_(std::move(*result_)); } } } @@ -183,7 +171,7 @@ class Core { } } - folly::Optional> value_; + folly::Optional> result_; std::function&&)> callback_; bool calledBack_ = false; unsigned char detached_ = 0; @@ -191,7 +179,7 @@ class Core { Executor* executor_ = nullptr; // this lock isn't meant to protect all accesses to members, only the ones - // that need to be threadsafe: the act of setting value_ and callback_, and + // that need to be threadsafe: the act of setting result_ and callback_, and // seeing if they are set and whether we should then continue. folly::MicroSpinLock mutex_ {0}; }; -- 2.34.1