Commit ff25b950 authored by Hans Fugal's avatar Hans Fugal Committed by dcsommer

(wangle) s/State/Core/

Summary:
codemod
`State` is such an overloaded term, and not really the best to describe this backing future/promise object. Yes, it holds the state but it's more than that and it gets in the way of calling the states of the state machines `State`s.

Test Plan: builds and tests pass

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, net-systems@, fugalh, exa, njormrod

FB internal diff: D1615707
parent 8d3b079a
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <folly/wangle/detail/State.h> #include <folly/wangle/detail/Core.h>
#include <folly/Baton.h> #include <folly/Baton.h>
namespace folly { namespace wangle { namespace folly { namespace wangle {
...@@ -35,13 +35,13 @@ struct isFuture<Future<T> > { ...@@ -35,13 +35,13 @@ struct isFuture<Future<T> > {
}; };
template <class T> template <class T>
Future<T>::Future(Future<T>&& other) noexcept : state_(nullptr) { Future<T>::Future(Future<T>&& other) noexcept : core_(nullptr) {
*this = std::move(other); *this = std::move(other);
} }
template <class T> template <class T>
Future<T>& Future<T>::operator=(Future<T>&& other) { Future<T>& Future<T>::operator=(Future<T>&& other) {
std::swap(state_, other.state_); std::swap(core_, other.core_);
return *this; return *this;
} }
...@@ -52,15 +52,15 @@ Future<T>::~Future() { ...@@ -52,15 +52,15 @@ Future<T>::~Future() {
template <class T> template <class T>
void Future<T>::detach() { void Future<T>::detach() {
if (state_) { if (core_) {
state_->detachFuture(); core_->detachFuture();
state_ = nullptr; core_ = nullptr;
} }
} }
template <class T> template <class T>
void Future<T>::throwIfInvalid() const { void Future<T>::throwIfInvalid() const {
if (!state_) if (!core_)
throw NoState(); throw NoState();
} }
...@@ -68,7 +68,7 @@ template <class T> ...@@ -68,7 +68,7 @@ template <class T>
template <class F> template <class F>
void Future<T>::setCallback_(F&& func) { void Future<T>::setCallback_(F&& func) {
throwIfInvalid(); throwIfInvalid();
state_->setCallback(std::move(func)); core_->setCallback(std::move(func));
} }
template <class T> template <class T>
...@@ -95,10 +95,10 @@ Future<T>::then(F&& func) { ...@@ -95,10 +95,10 @@ Future<T>::then(F&& func) {
sophisticated that avoids making a new Future object when it can, as an sophisticated that avoids making a new Future object when it can, as an
optimization. But this is correct. optimization. But this is correct.
state_ can't be moved, it is explicitly disallowed (as is copying). But core_ can't be moved, it is explicitly disallowed (as is copying). But
if there's ever a reason to allow it, this is one place that makes that if there's ever a reason to allow it, this is one place that makes that
assumption and would need to be fixed. We use a standard shared pointer assumption and would need to be fixed. We use a standard shared pointer
for state_ (by copying it in), which means in essence obj holds a shared for core_ (by copying it in), which means in essence obj holds a shared
pointer to itself. But this shouldn't leak because Promise will not pointer to itself. But this shouldn't leak because Promise will not
outlive the continuation, because Promise will setException() with a outlive the continuation, because Promise will setException() with a
broken Promise if it is destructed before completed. We could use a broken Promise if it is destructed before completed. We could use a
...@@ -110,11 +110,11 @@ Future<T>::then(F&& func) { ...@@ -110,11 +110,11 @@ Future<T>::then(F&& func) {
We have to move in the Promise and func using the MoveWrapper We have to move in the Promise and func using the MoveWrapper
hack. (func could be copied but it's a big drag on perf). hack. (func could be copied but it's a big drag on perf).
Two subtle but important points about this design. detail::State has no Two subtle but important points about this design. detail::Core has no
back pointers to Future or Promise, so if Future or Promise get moved back pointers to Future or Promise, so if Future or Promise get moved
(and they will be moved in performant code) we don't have to do (and they will be moved in performant code) we don't have to do
anything fancy. And because we store the continuation in the anything fancy. And because we store the continuation in the
detail::State, not in the Future, we can execute the continuation even detail::Core, not in the Future, we can execute the continuation even
after the Future has gone out of scope. This is an intentional design after the Future has gone out of scope. This is an intentional design
decision. It is likely we will want to be able to cancel a continuation decision. It is likely we will want to be able to cancel a continuation
in some circumstances, but I think it should be explicit not implicit in some circumstances, but I think it should be explicit not implicit
...@@ -172,21 +172,21 @@ template <class T> ...@@ -172,21 +172,21 @@ template <class T>
typename std::add_lvalue_reference<T>::type Future<T>::value() { typename std::add_lvalue_reference<T>::type Future<T>::value() {
throwIfInvalid(); throwIfInvalid();
return state_->value(); return core_->value();
} }
template <class T> template <class T>
typename std::add_lvalue_reference<const T>::type Future<T>::value() const { typename std::add_lvalue_reference<const T>::type Future<T>::value() const {
throwIfInvalid(); throwIfInvalid();
return state_->value(); return core_->value();
} }
template <class T> template <class T>
Try<T>& Future<T>::getTry() { Try<T>& Future<T>::getTry() {
throwIfInvalid(); throwIfInvalid();
return state_->getTry(); return core_->getTry();
} }
template <class T> template <class T>
...@@ -195,7 +195,7 @@ inline Future<T> Future<T>::via(Executor* executor) { ...@@ -195,7 +195,7 @@ inline Future<T> Future<T>::via(Executor* executor) {
throwIfInvalid(); throwIfInvalid();
this->deactivate(); this->deactivate();
state_->setExecutor(executor); core_->setExecutor(executor);
return std::move(*this); return std::move(*this);
} }
...@@ -203,7 +203,7 @@ inline Future<T> Future<T>::via(Executor* executor) { ...@@ -203,7 +203,7 @@ inline Future<T> Future<T>::via(Executor* executor) {
template <class T> template <class T>
bool Future<T>::isReady() const { bool Future<T>::isReady() const {
throwIfInvalid(); throwIfInvalid();
return state_->ready(); return core_->ready();
} }
// makeFuture // makeFuture
......
...@@ -199,23 +199,23 @@ class Future { ...@@ -199,23 +199,23 @@ class Future {
/// ///
/// Inactive Futures will activate upon destruction. /// Inactive Futures will activate upon destruction.
void activate() { void activate() {
state_->activate(); core_->activate();
} }
void deactivate() { void deactivate() {
state_->deactivate(); core_->deactivate();
} }
bool isActive() { bool isActive() {
return state_->isActive(); return core_->isActive();
} }
private: private:
typedef detail::State<T>* statePtr; typedef detail::Core<T>* corePtr;
// shared state object // shared core state object
statePtr state_; corePtr core_;
explicit explicit
Future(statePtr obj) : state_(obj) {} Future(corePtr obj) : core_(obj) {}
void detach(); void detach();
......
...@@ -20,31 +20,31 @@ ...@@ -20,31 +20,31 @@
#include <thread> #include <thread>
#include <folly/wangle/WangleException.h> #include <folly/wangle/WangleException.h>
#include <folly/wangle/detail/State.h> #include <folly/wangle/detail/Core.h>
namespace folly { namespace wangle { namespace folly { namespace wangle {
template <class T> template <class T>
Promise<T>::Promise() : retrieved_(false), state_(new detail::State<T>()) Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
{} {}
template <class T> template <class T>
Promise<T>::Promise(Promise<T>&& other) : state_(nullptr) { Promise<T>::Promise(Promise<T>&& other) : core_(nullptr) {
*this = std::move(other); *this = std::move(other);
} }
template <class T> template <class T>
Promise<T>& Promise<T>::operator=(Promise<T>&& other) { Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
std::swap(state_, other.state_); std::swap(core_, other.core_);
std::swap(retrieved_, other.retrieved_); std::swap(retrieved_, other.retrieved_);
return *this; return *this;
} }
template <class T> template <class T>
void Promise<T>::throwIfFulfilled() { void Promise<T>::throwIfFulfilled() {
if (!state_) if (!core_)
throw NoState(); throw NoState();
if (state_->ready()) if (core_->ready())
throw PromiseAlreadySatisfied(); throw PromiseAlreadySatisfied();
} }
...@@ -61,11 +61,11 @@ Promise<T>::~Promise() { ...@@ -61,11 +61,11 @@ Promise<T>::~Promise() {
template <class T> template <class T>
void Promise<T>::detach() { void Promise<T>::detach() {
if (state_) { if (core_) {
if (!retrieved_) if (!retrieved_)
state_->detachFuture(); core_->detachFuture();
state_->detachPromise(); core_->detachPromise();
state_ = nullptr; core_ = nullptr;
} }
} }
...@@ -73,7 +73,7 @@ template <class T> ...@@ -73,7 +73,7 @@ template <class T>
Future<T> Promise<T>::getFuture() { Future<T> Promise<T>::getFuture() {
throwIfRetrieved(); throwIfRetrieved();
retrieved_ = true; retrieved_ = true;
return Future<T>(state_); return Future<T>(core_);
} }
template <class T> template <class T>
...@@ -85,13 +85,13 @@ void Promise<T>::setException(E const& e) { ...@@ -85,13 +85,13 @@ void Promise<T>::setException(E const& e) {
template <class T> template <class T>
void Promise<T>::setException(std::exception_ptr const& e) { void Promise<T>::setException(std::exception_ptr const& e) {
throwIfFulfilled(); throwIfFulfilled();
state_->setException(e); core_->setException(e);
} }
template <class T> template <class T>
void Promise<T>::fulfilTry(Try<T>&& t) { void Promise<T>::fulfilTry(Try<T>&& t) {
throwIfFulfilled(); throwIfFulfilled();
state_->fulfil(std::move(t)); core_->fulfil(std::move(t));
} }
template <class T> template <class T>
......
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
Promise(Promise<T>&&); Promise(Promise<T>&&);
Promise& operator=(Promise<T>&&); Promise& operator=(Promise<T>&&);
/** Return a Future tied to the shared state. This can be called only /** Return a Future tied to the shared core state. This can be called only
once, thereafter Future already retrieved exception will be raised. */ once, thereafter Future already retrieved exception will be raised. */
Future<T> getFuture(); Future<T> getFuture();
...@@ -76,13 +76,13 @@ public: ...@@ -76,13 +76,13 @@ public:
void fulfil(F&& func); void fulfil(F&& func);
private: private:
typedef typename Future<T>::statePtr statePtr; typedef typename Future<T>::corePtr corePtr;
// Whether the Future has been retrieved (a one-time operation). // Whether the Future has been retrieved (a one-time operation).
bool retrieved_; bool retrieved_;
// shared state object // shared core state object
statePtr state_; corePtr core_;
void throwIfFulfilled(); void throwIfFulfilled();
void throwIfRetrieved(); void throwIfRetrieved();
......
...@@ -37,24 +37,24 @@ void empty_callback(Try<T>&&) { } ...@@ -37,24 +37,24 @@ void empty_callback(Try<T>&&) { }
/** The shared state object for Future and Promise. */ /** The shared state object for Future and Promise. */
template<typename T> template<typename T>
class State { class Core {
public: public:
// This must be heap-constructed. There's probably a way to enforce that in // This must be heap-constructed. There's probably a way to enforce that in
// code but since this is just internal detail code and I don't know how // code but since this is just internal detail code and I don't know how
// off-hand, I'm punting. // off-hand, I'm punting.
State() = default; Core() = default;
~State() { ~Core() {
assert(calledBack_); assert(calledBack_);
assert(detached_ == 2); assert(detached_ == 2);
} }
// not copyable // not copyable
State(State const&) = delete; Core(Core const&) = delete;
State& operator=(State const&) = delete; Core& operator=(Core const&) = delete;
// not movable (see comment in the implementation of Future::then) // not movable (see comment in the implementation of Future::then)
State(State&&) noexcept = delete; Core(Core&&) noexcept = delete;
State& operator=(State&&) = delete; Core& operator=(Core&&) = delete;
Try<T>& getTry() { Try<T>& getTry() {
return *value_; return *value_;
......
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