Repository: mesos Updated Branches: refs/heads/master 7b0812e9b -> bbd8381eb
Replaced `std::shared_ptr` with `std::unique_ptr` in `Future`. Review: https://reviews.apache.org/r/63913/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/bbd8381e Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/bbd8381e Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/bbd8381e Branch: refs/heads/master Commit: bbd8381ebce3522841e80ae53f56b3049342f15b Parents: bca8c6a Author: Dmitry Zhuk <dz...@twopensource.com> Authored: Tue Dec 5 13:47:53 2017 -0800 Committer: Michael Park <mp...@apache.org> Committed: Tue Dec 5 14:08:36 2017 -0800 ---------------------------------------------------------------------- 3rdparty/libprocess/include/process/future.hpp | 68 +++++++++++---------- 1 file changed, 36 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/bbd8381e/3rdparty/libprocess/include/process/future.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/include/process/future.hpp b/3rdparty/libprocess/include/process/future.hpp index cad4c5e..54fbbeb 100644 --- a/3rdparty/libprocess/include/process/future.hpp +++ b/3rdparty/libprocess/include/process/future.hpp @@ -1482,7 +1482,7 @@ namespace internal { // Future since the compiler can't properly infer otherwise. template <typename T, typename X> void thenf(lambda::CallableOnce<Future<X>(const T&)>&& f, - const std::shared_ptr<Promise<X>>& promise, + std::unique_ptr<Promise<X>> promise, const Future<T>& future) { if (future.isReady()) { @@ -1501,7 +1501,7 @@ void thenf(lambda::CallableOnce<Future<X>(const T&)>&& f, template <typename T, typename X> void then(lambda::CallableOnce<X(const T&)>&& f, - const std::shared_ptr<Promise<X>>& promise, + std::unique_ptr<Promise<X>> promise, const Future<T>& future) { if (future.isReady()) { @@ -1521,7 +1521,7 @@ void then(lambda::CallableOnce<X(const T&)>&& f, template <typename T> void repair( lambda::CallableOnce<Future<T>(const Future<T>&)>&& f, - const std::shared_ptr<Promise<T>>& promise, + std::unique_ptr<Promise<T>> promise, const Future<T>& future) { CHECK(!future.isPending()); @@ -1591,23 +1591,23 @@ template <typename T> template <typename X> Future<X> Future<T>::then(lambda::CallableOnce<Future<X>(const T&)> f) const { - std::shared_ptr<Promise<X>> promise(new Promise<X>()); + std::unique_ptr<Promise<X>> promise(new Promise<X>()); + Future<X> future = promise->future(); - lambda::CallableOnce<void(const Future<T>&)> thenf = - lambda::partial(&internal::thenf<T, X>, std::move(f), promise, lambda::_1); + lambda::CallableOnce<void(const Future<T>&)> thenf = lambda::partial( + &internal::thenf<T, X>, std::move(f), std::move(promise), lambda::_1); onAny(std::move(thenf)); - onAbandoned([=]() { - promise->future().abandon(); + onAbandoned([=]() mutable { + future.abandon(); }); // Propagate discarding up the chain. To avoid cyclic dependencies, // we keep a weak future in the callback. - promise->future().onDiscard( - lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); + future.onDiscard(lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); - return promise->future(); + return future; } @@ -1615,23 +1615,23 @@ template <typename T> template <typename X> Future<X> Future<T>::then(lambda::CallableOnce<X(const T&)> f) const { - std::shared_ptr<Promise<X>> promise(new Promise<X>()); + std::unique_ptr<Promise<X>> promise(new Promise<X>()); + Future<X> future = promise->future(); - lambda::CallableOnce<void(const Future<T>&)> then = - lambda::partial(&internal::then<T, X>, std::move(f), promise, lambda::_1); + lambda::CallableOnce<void(const Future<T>&)> then = lambda::partial( + &internal::then<T, X>, std::move(f), std::move(promise), lambda::_1); onAny(std::move(then)); - onAbandoned([=]() { - promise->future().abandon(); + onAbandoned([=]() mutable { + future.abandon(); }); // Propagate discarding up the chain. To avoid cyclic dependencies, // we keep a weak future in the callback. - promise->future().onDiscard( - lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); + future.onDiscard(lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); - return promise->future(); + return future; } @@ -1686,21 +1686,21 @@ template <typename T> Future<T> Future<T>::repair( lambda::CallableOnce<Future<T>(const Future<T>&)> f) const { - std::shared_ptr<Promise<T>> promise(new Promise<T>()); + std::unique_ptr<Promise<T>> promise(new Promise<T>()); + Future<T> future = promise->future(); - onAny( - lambda::partial(&internal::repair<T>, std::move(f), promise, lambda::_1)); + onAny(lambda::partial( + &internal::repair<T>, std::move(f), std::move(promise), lambda::_1)); - onAbandoned([=]() { - promise->future().abandon(); + onAbandoned([=]() mutable { + future.abandon(); }); // Propagate discarding up the chain. To avoid cyclic dependencies, // we keep a weak future in the callback. - promise->future().onDiscard( - lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); + future.onDiscard(lambda::bind(&internal::discard<T>, WeakFuture<T>(*this))); - return promise->future(); + return future; } @@ -1940,11 +1940,15 @@ void discardPromises(std::set<Promise<T>*>* promises, const Future<T>& future) template <typename T> Future<T> undiscardable(const Future<T>& future) { - std::shared_ptr<Promise<T>> promise(new Promise<T>()); - future.onAny([promise](const Future<T>& future) { - promise->associate(future); - }); - return promise->future(); + std::unique_ptr<Promise<T>> promise(new Promise<T>()); + Future<T> future_ = promise->future(); + future.onAny(lambda::partial( + [](std::unique_ptr<Promise<T>> promise, const Future<T>& future) { + promise->associate(future); + }, + std::move(promise), + lambda::_1)); + return future_; }