Fix SharedPromise::isFulfilled doesn't get back correct value when it's running in...
[folly.git] / folly / futures / SharedPromise-inl.h
index 76bd0caec266b316f5e6af90e8882a0a55909606..9def3bc68658820600853a9380f604b9000a8cf1 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.
@@ -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;
@@ -60,6 +61,9 @@ Future<T> SharedPromise<T>::getFuture() {
     return makeFuture<T>(Try<T>(try_));
   } else {
     promises_.emplace_back();
+    if (interruptHandler_) {
+      promises_.back().setInterruptHandler(interruptHandler_);
+    }
     return promises_.back().getFuture();
   }
 }
@@ -88,6 +92,7 @@ void SharedPromise<T>::setInterruptHandler(
   if (hasValue_) {
     return;
   }
+  interruptHandler_ = fn;
   for (auto& p : promises_) {
     p.setInterruptHandler(fn);
   }
@@ -126,6 +131,7 @@ void SharedPromise<T>::setTry(Try<T>&& t) {
 
 template <class T>
 bool SharedPromise<T>::isFulfilled() {
+  std::lock_guard<std::mutex> g(mutex_);
   return hasValue_;
 }