From 8961e8d59374e77bfca5daebac640a2bceb47ae7 Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Wed, 14 Oct 2015 03:04:26 +0200 Subject: [PATCH] go on with the future refactoring. --- include/boost/thread/future.hpp | 209 ++++++++++++++++++++------------ 1 file changed, 129 insertions(+), 80 deletions(-) diff --git a/include/boost/thread/future.hpp b/include/boost/thread/future.hpp index 1f41e5e8..4c98651e 100644 --- a/include/boost/thread/future.hpp +++ b/include/boost/thread/future.hpp @@ -1427,13 +1427,13 @@ namespace boost make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template + template BOOST_THREAD_FUTURE - make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); + make_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); - template + template BOOST_THREAD_FUTURE - make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); + make_shared_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE @@ -1511,13 +1511,13 @@ namespace boost detail::make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template + template friend BOOST_THREAD_FUTURE - detail::make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); + detail::make_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); - template + template friend BOOST_THREAD_FUTURE - detail::make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); + detail::make_shared_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE @@ -1767,13 +1767,13 @@ namespace boost detail::make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template + template friend BOOST_THREAD_FUTURE - detail::make_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); + detail::make_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); - template + template friend BOOST_THREAD_FUTURE - detail::make_shared_future_executor_continuation_shared_state(Ex& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); + detail::make_shared_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE @@ -3839,6 +3839,7 @@ namespace detail { void init(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f) { typedef typename decay::type Cont; + typedef typename decay::type Ex; this->set_executor_policy(executor_ptr_type(new executor_ref(ex))); shared_state_nullary_task t(this->shared_from_this(), boost::forward(f)); ex.submit(boost::move(t)); @@ -4272,9 +4273,9 @@ namespace detail ///////////////////////// template - struct future_async_continuation_shared_state: continuation_shared_state + struct future_async_continuation_shared_state: continuation_shared_state { - typedef continuation_shared_state base_type; + typedef continuation_shared_state base_type; public: future_async_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) @@ -4340,17 +4341,27 @@ namespace detail namespace detail { - template + template struct future_executor_continuation_shared_state: continuation_shared_state { typedef continuation_shared_state base_type; public: - future_executor_continuation_shared_state(Ex& ex, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) + future_executor_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) { - this->set_executor_policy(executor_ptr_type(new executor_ref(ex))); } + template + void init(boost::unique_lock &lk, Ex& ex) + { + this->set_executor_policy(executor_ptr_type(new executor_ref(ex))); + this->base_type::init(lk); + } + void init(boost::unique_lock &lk, executor_ptr_type& ex) + { + this->set_executor_policy(ex); + this->base_type::init(lk); + } void launch_continuation() { run_it fct(this->shared_from_this()); this->get_executor()->submit(boost::move(fct)); @@ -4364,9 +4375,9 @@ namespace detail { ///////////////////////// template - struct shared_future_async_continuation_shared_state: continuation_shared_state + struct shared_future_async_continuation_shared_state: continuation_shared_state { - typedef continuation_shared_state base_type; + typedef continuation_shared_state base_type; public: shared_future_async_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) @@ -4388,15 +4399,26 @@ namespace detail { ///////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template + template struct shared_future_executor_continuation_shared_state: continuation_shared_state { typedef continuation_shared_state base_type; public: - shared_future_executor_continuation_shared_state(Ex& ex, F f, BOOST_THREAD_FWD_REF(Fp) c) + shared_future_executor_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) + { + } + + template + void init(boost::unique_lock &lk, Ex& ex) { this->set_executor_policy(executor_ptr_type(new executor_ref(ex))); + this->base_type::init(lk); + } + void init(boost::unique_lock &lk, executor_ptr_type& ex) + { + this->set_executor_policy(ex); + this->base_type::init(lk); } void launch_continuation() { @@ -4552,8 +4574,9 @@ namespace detail { make_future_deferred_continuation_shared_state( boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) { - csbl::shared_ptr > - h(new future_deferred_continuation_shared_state(boost::move(f), boost::forward(c))); + typedef typename decay::type Cont; + csbl::shared_ptr > + h(new future_deferred_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4569,7 +4592,7 @@ namespace detail { typedef typename decay::type Cont; csbl::shared_ptr > - h(new future_async_continuation_shared_state(boost::move(f), boost::forward(c))); + h(new future_async_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4578,17 +4601,27 @@ namespace detail { // make_future_executor_continuation_shared_state //////////////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - - template + template BOOST_THREAD_FUTURE - make_future_executor_continuation_shared_state(Ex& ex, + make_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) { - csbl::shared_ptr > - h(new future_executor_continuation_shared_state(ex, boost::move(f), boost::forward(c))); - h->init(lock); + typedef typename decay::type Cont; + csbl::shared_ptr > + h(new future_executor_continuation_shared_state(boost::move(f), boost::forward(c))); + h->init(lock, ex); return BOOST_THREAD_FUTURE(h); } + template + BOOST_THREAD_FUTURE + make_future_executor_continuation_shared_state_ex(Ex& ex, + boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, + BOOST_THREAD_FWD_REF(Fp) c) { + typedef typename decay::type Cont; + executor_ptr_type sex(new executor_ref(ex)); + return make_future_executor_continuation_shared_state( + sex, lock, boost::move(f), boost::forward(c)); + } #endif //////////////////////////////// @@ -4599,8 +4632,9 @@ namespace detail { make_shared_future_deferred_continuation_shared_state( boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { - csbl::shared_ptr > - h(new shared_future_deferred_continuation_shared_state(f, boost::forward(c))); + typedef typename decay::type Cont; + csbl::shared_ptr > + h(new shared_future_deferred_continuation_shared_state(f, boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4615,7 +4649,7 @@ namespace detail { typedef typename decay::type Cont; csbl::shared_ptr > - h(new shared_future_async_continuation_shared_state(f, boost::forward(c))); + h(new shared_future_async_continuation_shared_state(f, boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4623,16 +4657,27 @@ namespace detail { // make_shared_future_executor_continuation_shared_state //////////////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template + template BOOST_THREAD_FUTURE - make_shared_future_executor_continuation_shared_state(Ex& ex, + make_shared_future_executor_continuation_shared_state(executor_ptr_type& ex, boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { - csbl::shared_ptr > - h(new shared_future_executor_continuation_shared_state(ex, f, boost::forward(c))); - h->init(lock); + typedef typename decay::type Cont; + csbl::shared_ptr > + h(new shared_future_executor_continuation_shared_state(f, boost::forward(c))); + h->init(lock, ex); return BOOST_THREAD_FUTURE(h); } + template + BOOST_THREAD_FUTURE + make_shared_future_executor_continuation_shared_state_ex(Ex& ex, + boost::unique_lock &lock, F f, + BOOST_THREAD_FWD_REF(Fp) c) { + typedef typename decay::type Cont; + executor_ptr_type sex(new executor_ref(ex)); + return make_shared_future_executor_continuation_shared_state( + sex, lock, f, boost::forward(c)); + } #endif } @@ -4649,22 +4694,23 @@ namespace detail { boost::unique_lock lock(this->future_->mutex); if (underlying_cast(policy) & int(launch::async)) { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::deferred)) { this->future_->wait_internal(lock); - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, + lock, boost::move(*this), boost::forward(func) + ))); #endif } else { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } @@ -4682,7 +4728,8 @@ namespace detail { BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); boost::unique_lock lock(this->future_->mutex); - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type, F>(ex, + executor_ptr_type h(new executor_ref(ex)); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(h, lock, boost::move(*this), boost::forward(func) ))); } @@ -4713,10 +4760,10 @@ namespace detail { ); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(this->launch_policy(lock)) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - lock.unlock(); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, + lock, boost::move(*this), boost::forward(func) + ))); #endif } else { return boost::detail::make_future_async_continuation_shared_state, future_type>( @@ -4739,22 +4786,22 @@ namespace detail { boost::unique_lock lock(this->future_->mutex); if (underlying_cast(policy) & int(launch::async)) { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::deferred)) { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - lock.unlock(); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, + lock, boost::move(*this), boost::forward(func) + ))); #endif } else { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } @@ -4773,7 +4820,8 @@ namespace detail { BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); boost::unique_lock lock(this->future_->mutex); - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type, F>(ex, + executor_ptr_type h(new executor_ref(ex)); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(h, lock, boost::move(*this), boost::forward(func) ))); } @@ -4793,23 +4841,23 @@ namespace detail { boost::unique_lock lock(this->future_->mutex); if (underlying_cast(this->launch_policy(lock)) & int(launch::async)) { - return boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ); } else if (underlying_cast(this->launch_policy(lock)) & int(launch::deferred)) { this->future_->wait_internal(lock); - return boost::detail::make_future_deferred_continuation_shared_state, future_type, F>( + return boost::detail::make_future_deferred_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(this->launch_policy(lock)) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - lock.unlock(); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, + lock, boost::move(*this), boost::forward(func) + ))); #endif } else { - return boost::detail::make_future_async_continuation_shared_state, future_type, F>( + return boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ); } @@ -4829,23 +4877,23 @@ namespace detail { boost::unique_lock lock(this->future_->mutex); if (underlying_cast(policy) & int(launch::async)) { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::deferred)) { this->future_->wait_internal(lock); - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - lock.unlock(); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type>(ex, + lock, *this, boost::forward(func) + ))); #endif } else { - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state, future_type, F>( + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } @@ -4864,7 +4912,8 @@ namespace detail { BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); boost::unique_lock lock(this->future_->mutex); - return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type, F>(ex, + executor_ptr_type h(new executor_ref(ex)); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type>(h, lock, *this, boost::forward(func) ))); } @@ -4893,10 +4942,10 @@ namespace detail { lock, *this, boost::forward(func)); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(this->launch_policy(lock)) & int(launch::executor)) { - typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); - lock.unlock(); - return this->then(ex, boost::forward(func)); + executor_ptr_type ex = this->future_->get_executor(); + return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type>(ex, + lock, *this, boost::forward(func) + ))); #endif } else { return boost::detail::make_shared_future_async_continuation_shared_state, future_type>( @@ -5093,7 +5142,7 @@ namespace detail // detail::future_async_when_all_shared_state //////////////////////////////// template - struct future_when_all_vector_shared_state: future_async_shared_state_base > + struct future_when_all_vector_shared_state: shared_state > { typedef csbl::vector vector_type; typedef typename F::value_type value_type; @@ -5163,7 +5212,7 @@ namespace detail // detail::future_async_when_any_shared_state //////////////////////////////// template - struct future_when_any_vector_shared_state: future_async_shared_state_base > + struct future_when_any_vector_shared_state: shared_state > { typedef csbl::vector vector_type; typedef typename F::value_type value_type; @@ -5267,7 +5316,7 @@ namespace detail template< typename Tuple, typename T0, typename ...T> - struct future_when_all_tuple_shared_state: future_async_shared_state_base + struct future_when_all_tuple_shared_state: shared_state { Tuple tup_; typedef typename make_tuple_indices<1+sizeof...(T)>::type Index; @@ -5339,7 +5388,7 @@ namespace detail }; template< typename Tuple, typename T0, typename ...T > - struct future_when_any_tuple_shared_state: future_async_shared_state_base + struct future_when_any_tuple_shared_state: shared_state { Tuple tup_; typedef typename make_tuple_indices<1+sizeof...(T)>::type Index;