From 6e427659a4e3f95f05053a58bc032eae4a7931b3 Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Thu, 29 Oct 2015 23:15:48 +0100 Subject: [PATCH] Remove the set_executor promise/packged_task member function. Added a promise constructor taking an Executor as parameter. The packaged_task will come later. --- include/boost/thread/future.hpp | 230 +++++++++++++++++--------------- 1 file changed, 126 insertions(+), 104 deletions(-) diff --git a/include/boost/thread/future.hpp b/include/boost/thread/future.hpp index 328223d4..18d2ca9f 100644 --- a/include/boost/thread/future.hpp +++ b/include/boost/thread/future.hpp @@ -94,6 +94,9 @@ namespace boost { + + struct executor_arg_t {}; + template shared_ptr static_shared_from_this(T* that) { @@ -160,10 +163,6 @@ namespace boost boost::function callback; // This declaration should be only included conditionally, but is included to maintain the same layout. continuations_type continuations; -// EXECUTOR -#if 1 - executor_ptr_type ex; -#endif // This declaration should be only included conditionally, but is included to maintain the same layout. virtual void launch_continuation() @@ -177,11 +176,6 @@ namespace boost is_constructed(false), policy_(launch::none), continuations() -// EXECUTOR -#if 1 - , - ex() -#endif {} shared_state_base(exceptional_ptr const& excp): @@ -192,42 +186,17 @@ namespace boost is_constructed(false), policy_(launch::none), continuations() -// EXECUTOR -#if 1 - , - ex() -#endif {} virtual ~shared_state_base() { } -// EXECUTOR -#if 1 - - executor_ptr_type get_executor() + virtual executor_ptr_type get_executor_ptr(boost::unique_lock&) { - return ex; + return executor_ptr_type(); } - void set_executor_policy(executor_ptr_type aex) - { - set_executor(); - ex = aex; - } - void set_executor_policy(executor_ptr_type aex, boost::lock_guard&) - { - set_executor(); - ex = aex; - } - void set_executor_policy(executor_ptr_type aex, boost::unique_lock&) - { - set_executor(); - ex = aex; - } -#endif - bool valid(boost::unique_lock&) { return is_valid_; } bool valid() { boost::unique_lock lk(this->mutex); @@ -810,6 +779,40 @@ namespace boost shared_state& operator=(shared_state const&); }; + template + struct executor_shared_state: shared_state { + typedef Executor executor_type; + executor_type& ex; + executor_ptr_type ex_ptr; + + executor_shared_state(Executor& ex): + shared_state(), + ex(ex) + { + this->set_executor(); + } + executor_shared_state(Executor& ex, exceptional_ptr const& excp): + shared_state(excp), + ex(ex) + { + this->set_executor(); + } + + executor_type& get_executor() + { + return ex; + } + + executor_ptr_type get_executor_ptr(boost::unique_lock&) + { + if (! ex_ptr) + { + ex_ptr.reset(new executor_ref(ex)); + } + return ex_ptr; + } + }; + ///////////////////////// /// future_async_shared_state_base ///////////////////////// @@ -2168,6 +2171,15 @@ namespace boost future_obtained = false; } #endif +#ifdef BOOST_THREAD_PROVIDES_EXECUTORS + template + promise(boost::executor_arg_t, Executor& ex): + future_(new detail::executor_shared_state(ex)), + future_obtained(false) + { + } +#endif + promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY future_(), @@ -2212,18 +2224,6 @@ namespace boost std::swap(future_obtained,other.future_obtained); } -#ifdef BOOST_THREAD_PROVIDES_EXECUTORS - void set_executor(executor_ptr_type aex) - { - lazy_init(); - if (future_.get()==0) - { - boost::throw_exception(promise_moved()); - } - boost::lock_guard lk(future_->mutex); - future_->set_executor_policy(aex, lk); - } -#endif // Result retrieval BOOST_THREAD_FUTURE get_future() { @@ -2396,6 +2396,14 @@ namespace boost future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state(), D(a2, 1) ); future_obtained = false; } +#endif +#ifdef BOOST_THREAD_PROVIDES_EXECUTORS + template + promise(boost::executor_arg_t, Executor& ex): + future_(new detail::executor_shared_state(ex)), + future_obtained(false) + { + } #endif promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY @@ -2548,6 +2556,14 @@ namespace boost future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state(), D(a2, 1) ); future_obtained = false; } +#endif +#ifdef BOOST_THREAD_PROVIDES_EXECUTORS + template + promise(boost::executor_arg_t, Executor& ex): + future_(new detail::executor_shared_state(ex)), + future_obtained(false) + { + } #endif promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY @@ -3484,15 +3500,6 @@ namespace boost return *this; } -#ifdef BOOST_THREAD_PROVIDES_EXECUTORS - void set_executor(executor_ptr_type aex) - { - if (!valid()) - boost::throw_exception(task_moved()); - boost::lock_guard lk(task->mutex); - task->set_executor_policy(aex, lk); - } -#endif void reset() { if (!valid()) boost::throw_exception(future_error(system::make_error_code(future_errc::no_state))); @@ -3859,22 +3866,21 @@ namespace detail { ///////////////////////// /// future_executor_shared_state_base ///////////////////////// - template - struct future_executor_shared_state: shared_state + template + struct future_executor_shared_state: executor_shared_state { - typedef shared_state base_type; + typedef executor_shared_state base_type; protected: public: - future_executor_shared_state() { + future_executor_shared_state(Ex& ex) : base_type(ex) { } - template - void init(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f) + template + void init(BOOST_THREAD_FWD_REF(Fp) f) { typedef typename decay::type Cont; - 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)); + this->ex.submit(boost::move(t)); } ~future_executor_shared_state() {} @@ -3886,9 +3892,9 @@ namespace detail { template BOOST_THREAD_FUTURE make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f) { - shared_ptr > - h(new future_executor_shared_state()); - h->init(ex, boost::forward(f)); + shared_ptr > + h(new future_executor_shared_state(ex)); + h->init(boost::forward(f)); return BOOST_THREAD_FUTURE(h); } @@ -4248,7 +4254,17 @@ namespace detail public: continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) - : parent(boost::move(f)), + : ShSt(), + parent(boost::move(f)), + continuation(boost::move(c)), + centinel(parent.future_) + { + } + + template + continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c, Ex& ex) + : ShSt(ex), + parent(boost::move(f)), continuation(boost::move(c)), centinel(parent.future_) { @@ -4285,7 +4301,17 @@ namespace detail public: continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) - : parent(boost::move(f)), + : ShSt(), + parent(boost::move(f)), + continuation(boost::move(c)), + centinel(parent.future_) + { + } + + template + continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c, Ex& ex) + : ShSt(ex), + parent(boost::move(f)), continuation(boost::move(c)), centinel(parent.future_) { @@ -4387,27 +4413,25 @@ namespace detail namespace detail { - template - struct future_executor_continuation_shared_state: continuation_shared_state + template + struct future_executor_continuation_shared_state: continuation_shared_state > { - typedef continuation_shared_state base_type; + typedef continuation_shared_state > base_type; public: - 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)) + future_executor_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c, Ex& ex) + : base_type(boost::move(f), boost::forward(c), ex) { } - template - void init(boost::unique_lock &lk, Ex& ex) + void init(boost::unique_lock &lk) { - this->set_executor_policy(executor_ptr_type(new executor_ref(ex)), lk); this->base_type::init(lk); } void launch_continuation() { run_it fct(static_shared_from_this(this)); - this->get_executor()->submit(boost::move(fct)); + this->get_executor().submit(boost::move(fct)); } ~future_executor_continuation_shared_state() {} @@ -4448,28 +4472,26 @@ namespace detail { ///////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS - template - struct shared_future_executor_continuation_shared_state: continuation_shared_state + template + struct shared_future_executor_continuation_shared_state: continuation_shared_state > { - typedef continuation_shared_state base_type; + typedef continuation_shared_state > base_type; public: - shared_future_executor_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) - : base_type(boost::move(f), boost::forward(c)) + shared_future_executor_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c, Ex& ex) + : base_type(boost::move(f), boost::forward(c), ex) { } - template - void init(boost::unique_lock &lk, Ex& ex) + void init(boost::unique_lock &lk) { - this->set_executor_policy(executor_ptr_type(new executor_ref(ex)), lk); this->base_type::init(lk); } void launch_continuation() { run_it fct(static_shared_from_this(this)); - this->get_executor()->submit(boost::move(fct)); + this->get_executor().submit(boost::move(fct)); } ~shared_future_executor_continuation_shared_state() {} @@ -4660,9 +4682,9 @@ namespace detail { boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) { typedef typename decay::type Cont; - shared_ptr > - h(new future_executor_continuation_shared_state(boost::move(f), boost::forward(c))); - h->init(lock, ex); + shared_ptr > + h(new future_executor_continuation_shared_state(boost::move(f), boost::forward(c), ex)); + h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4708,9 +4730,9 @@ namespace detail { boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { typedef typename decay::type Cont; - shared_ptr > - h(new shared_future_executor_continuation_shared_state(f, boost::forward(c))); - h->init(lock, ex); + shared_ptr > + h(new shared_future_executor_continuation_shared_state(f, boost::forward(c), ex)); + h->init(lock); return BOOST_THREAD_FUTURE(h); } @@ -4740,9 +4762,9 @@ namespace detail { ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - assert(this->future_->get_executor()); + assert(this->future_->get_executor_ptr(lock)); typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, lock, boost::move(*this), boost::forward(func) ))); @@ -4761,9 +4783,9 @@ namespace detail { ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - assert(this->future_->get_executor()); + assert(this->future_->get_executor_ptr(lock)); typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, lock, boost::move(*this), boost::forward(func) ))); @@ -4851,9 +4873,9 @@ namespace detail { ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - assert(this->future_->get_executor()); + assert(this->future_->get_executor_ptr(lock)); typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, lock, boost::move(*this), boost::forward(func) ))); @@ -4871,9 +4893,9 @@ namespace detail { ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { - assert(this->future_->get_executor()); + assert(this->future_->get_executor_ptr(lock)); typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_executor_continuation_shared_state, future_type>(ex, lock, boost::move(*this), boost::forward(func) ))); @@ -4966,7 +4988,7 @@ namespace detail { #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type>(ex, lock, *this, boost::forward(func) ))); @@ -4986,7 +5008,7 @@ namespace detail { #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { typedef executor Ex; - Ex& ex = *(this->future_->get_executor()); + Ex& ex = *(this->future_->get_executor_ptr(lock)); return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_executor_continuation_shared_state, future_type>(ex, lock, *this, boost::forward(func) )));