Commit 8cb94e9b authored by Viswanath Sivakumar's avatar Viswanath Sivakumar Committed by facebook-github-bot-1

Set interrupt handler correctly on SharedPromise

Summary: If SharedPromise::getFuture() is invoked after a call to setInterruptHandler,
then the interrupt handler isn't set on the newly created promise. This diff
fixes that.

Reviewed By: yfeldblum

Differential Revision: D2610289

fb-gh-sync-id: bf8fce9e881b83ccac17d13c6788ec2afd0b0153
parent 8f06296e
...@@ -41,6 +41,7 @@ SharedPromise<T>& SharedPromise<T>::operator=( ...@@ -41,6 +41,7 @@ SharedPromise<T>& SharedPromise<T>::operator=(
std::swap(size_, other.size_); std::swap(size_, other.size_);
std::swap(hasValue_, other.hasValue_); std::swap(hasValue_, other.hasValue_);
std::swap(try_, other.try_); std::swap(try_, other.try_);
std::swap(interruptHandler_, other.interruptHandler_);
std::swap(promises_, other.promises_); std::swap(promises_, other.promises_);
return *this; return *this;
...@@ -60,6 +61,9 @@ Future<T> SharedPromise<T>::getFuture() { ...@@ -60,6 +61,9 @@ Future<T> SharedPromise<T>::getFuture() {
return makeFuture<T>(Try<T>(try_)); return makeFuture<T>(Try<T>(try_));
} else { } else {
promises_.emplace_back(); promises_.emplace_back();
if (interruptHandler_) {
promises_.back().setInterruptHandler(interruptHandler_);
}
return promises_.back().getFuture(); return promises_.back().getFuture();
} }
} }
...@@ -88,6 +92,7 @@ void SharedPromise<T>::setInterruptHandler( ...@@ -88,6 +92,7 @@ void SharedPromise<T>::setInterruptHandler(
if (hasValue_) { if (hasValue_) {
return; return;
} }
interruptHandler_ = fn;
for (auto& p : promises_) { for (auto& p : promises_) {
p.setInterruptHandler(fn); p.setInterruptHandler(fn);
} }
......
...@@ -113,6 +113,7 @@ private: ...@@ -113,6 +113,7 @@ private:
bool hasValue_{false}; bool hasValue_{false};
Try<T> try_; Try<T> try_;
std::vector<Promise<T>> promises_; std::vector<Promise<T>> promises_;
std::function<void(exception_wrapper const&)> interruptHandler_;
}; };
} }
......
...@@ -117,3 +117,12 @@ TEST(SharedPromise, isFulfilled) { ...@@ -117,3 +117,12 @@ TEST(SharedPromise, isFulfilled) {
p = std::move(p2); p = std::move(p2);
EXPECT_TRUE(p.isFulfilled()); EXPECT_TRUE(p.isFulfilled());
} }
TEST(SharedPromise, interruptHandler) {
SharedPromise<int> p;
bool flag = false;
p.setInterruptHandler([&](const exception_wrapper&) { flag = true; });
auto f = p.getFuture();
f.cancel();
EXPECT_TRUE(flag);
}
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