From: Viswanath Sivakumar Date: Thu, 5 Nov 2015 11:48:26 +0000 (-0800) Subject: Set interrupt handler correctly on SharedPromise X-Git-Tag: deprecate-dynamic-initializer~276 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=8cb94e9bfc9df742defff3d3d91b58ea8938c345;p=folly.git 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 --- diff --git a/folly/futures/SharedPromise-inl.h b/folly/futures/SharedPromise-inl.h index 76bd0cae..8b3ac0f3 100644 --- a/folly/futures/SharedPromise-inl.h +++ b/folly/futures/SharedPromise-inl.h @@ -41,6 +41,7 @@ SharedPromise& SharedPromise::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 SharedPromise::getFuture() { return makeFuture(Try(try_)); } else { promises_.emplace_back(); + if (interruptHandler_) { + promises_.back().setInterruptHandler(interruptHandler_); + } return promises_.back().getFuture(); } } @@ -88,6 +92,7 @@ void SharedPromise::setInterruptHandler( if (hasValue_) { return; } + interruptHandler_ = fn; for (auto& p : promises_) { p.setInterruptHandler(fn); } diff --git a/folly/futures/SharedPromise.h b/folly/futures/SharedPromise.h index 11419302..cda8f40b 100644 --- a/folly/futures/SharedPromise.h +++ b/folly/futures/SharedPromise.h @@ -113,6 +113,7 @@ private: bool hasValue_{false}; Try try_; std::vector> promises_; + std::function interruptHandler_; }; } diff --git a/folly/futures/test/SharedPromiseTest.cpp b/folly/futures/test/SharedPromiseTest.cpp index 1dd23b85..9ecef423 100644 --- a/folly/futures/test/SharedPromiseTest.cpp +++ b/folly/futures/test/SharedPromiseTest.cpp @@ -117,3 +117,12 @@ TEST(SharedPromise, isFulfilled) { p = std::move(p2); EXPECT_TRUE(p.isFulfilled()); } + +TEST(SharedPromise, interruptHandler) { + SharedPromise p; + bool flag = false; + p.setInterruptHandler([&](const exception_wrapper&) { flag = true; }); + auto f = p.getFuture(); + f.cancel(); + EXPECT_TRUE(flag); +}