Refactors folly sync test cases
[folly.git] / folly / futures / SharedPromise-inl.h
index 76bd0caec266b316f5e6af90e8882a0a55909606..78c72430d4a6f694bc3f4f6e72dcb3d519483a2c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2015-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ SharedPromise<T>& SharedPromise<T>::operator=(
   std::swap(size_, other.size_);
   std::swap(hasValue_, other.hasValue_);
   std::swap(try_, other.try_);
+  std::swap(interruptHandler_, other.interruptHandler_);
   std::swap(promises_, other.promises_);
 
   return *this;
@@ -53,17 +54,25 @@ size_t SharedPromise<T>::size() {
 }
 
 template <class T>
-Future<T> SharedPromise<T>::getFuture() {
+SemiFuture<T> SharedPromise<T>::getSemiFuture() {
   std::lock_guard<std::mutex> g(mutex_);
   size_++;
   if (hasValue_) {
     return makeFuture<T>(Try<T>(try_));
   } else {
     promises_.emplace_back();
-    return promises_.back().getFuture();
+    if (interruptHandler_) {
+      promises_.back().setInterruptHandler(interruptHandler_);
+    }
+    return promises_.back().getSemiFuture();
   }
 }
 
+template <class T>
+Future<T> SharedPromise<T>::getFuture() {
+  return getSemiFuture().via(&folly::InlineExecutor::instance());
+}
+
 template <class T>
 template <class E>
 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
@@ -88,6 +97,7 @@ void SharedPromise<T>::setInterruptHandler(
   if (hasValue_) {
     return;
   }
+  interruptHandler_ = fn;
   for (auto& p : promises_) {
     p.setInterruptHandler(fn);
   }
@@ -112,7 +122,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
   {
     std::lock_guard<std::mutex> g(mutex_);
     if (hasValue_) {
-      throw PromiseAlreadySatisfied();
+      throwPromiseAlreadySatisfied();
     }
     hasValue_ = true;
     try_ = std::move(t);
@@ -126,7 +136,8 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
 
 template <class T>
 bool SharedPromise<T>::isFulfilled() {
+  std::lock_guard<std::mutex> g(mutex_);
   return hasValue_;
 }
 
-}
+} // namespace folly