Revert D4982969: [Folly] Destroy promise/future callback functions before waking...
[folly.git] / folly / futures / Promise-inl.h
index f38ee60dfd59f9dd7381a0d0fb2621327a2a4387..c55d34ca66e48e069918c726ebcd2c1ea67d3ed5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,12 +29,14 @@ Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
 {}
 
 template <class T>
-Promise<T>::Promise(Promise<T>&& other) : core_(nullptr) {
-  *this = std::move(other);
+Promise<T>::Promise(Promise<T>&& other) noexcept
+    : retrieved_(other.retrieved_), core_(other.core_) {
+  other.core_ = nullptr;
+  other.retrieved_ = false;
 }
 
 template <class T>
-Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
+Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
   std::swap(core_, other.core_);
   std::swap(retrieved_, other.retrieved_);
   return *this;
@@ -42,16 +44,19 @@ Promise<T>& Promise<T>::operator=(Promise<T>&& other) {
 
 template <class T>
 void Promise<T>::throwIfFulfilled() {
-  if (!core_)
+  if (UNLIKELY(!core_)) {
     throw NoState();
-  if (core_->ready())
+  }
+  if (UNLIKELY(core_->ready())) {
     throw PromiseAlreadySatisfied();
+  }
 }
 
 template <class T>
 void Promise<T>::throwIfRetrieved() {
-  if (retrieved_)
+  if (UNLIKELY(retrieved_)) {
     throw FutureAlreadyRetrieved();
+  }
 }
 
 template <class T>
@@ -107,7 +112,7 @@ void Promise<T>::setInterruptHandler(
 }
 
 template <class T>
-void Promise<T>::fulfilTry(Try<T> t) {
+void Promise<T>::setTry(Try<T>&& t) {
   throwIfFulfilled();
   core_->setResult(std::move(t));
 }
@@ -118,22 +123,22 @@ void Promise<T>::setValue(M&& v) {
   static_assert(!std::is_same<T, void>::value,
                 "Use setValue() instead");
 
-  fulfilTry(Try<T>(std::forward<M>(v)));
+  setTry(Try<T>(std::forward<M>(v)));
 }
 
 template <class T>
-void Promise<T>::setValue() {
-  static_assert(std::is_same<T, void>::value,
-                "Use setValue(value) instead");
-
-  fulfilTry(Try<void>());
+template <class F>
+void Promise<T>::setWith(F&& func) {
+  throwIfFulfilled();
+  setTry(makeTryWith(std::forward<F>(func)));
 }
 
 template <class T>
-template <class F>
-void Promise<T>::fulfil(F&& func) {
-  throwIfFulfilled();
-  fulfilTry(makeTryFunction(std::forward<F>(func)));
+bool Promise<T>::isFulfilled() const noexcept {
+  if (core_) {
+    return core_->hasResult();
+  }
+  return true;
 }
 
 }