// (C) Copyright 2008-10 Anthony Williams // (C) Copyright 2011-2015 Vicente J. Botet Escriba // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_THREAD_FUTURE_HPP #define BOOST_THREAD_FUTURE_HPP #include // boost::thread::future requires exception handling // due to boost::exception::exception_ptr dependency //#define BOOST_THREAD_CONTINUATION_SYNC #ifndef BOOST_NO_EXCEPTIONS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL #include #else #include #endif #include #include #ifdef BOOST_THREAD_USES_CHRONO #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS #include #include #if ! defined BOOST_NO_CXX11_ALLOCATOR #include #endif #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY #include #include #endif #include #include #include #include #if defined BOOST_THREAD_PROVIDES_FUTURE #define BOOST_THREAD_FUTURE future #else #define BOOST_THREAD_FUTURE unique_future #endif namespace boost { template shared_ptr static_shared_from_this(T* that) { return static_pointer_cast(that->shared_from_this()); } template shared_ptr static_shared_from_this(T const* that) { return static_pointer_cast(that->shared_from_this()); } #ifdef BOOST_THREAD_PROVIDES_EXECUTORS #else namespace executors { class executor; } #endif typedef shared_ptr executor_ptr_type; namespace detail { struct relocker { boost::unique_lock& lock_; relocker(boost::unique_lock& lk): lock_(lk) { lock_.unlock(); } ~relocker() { if (! lock_.owns_lock()) { lock_.lock(); } } void lock() { if (! lock_.owns_lock()) { lock_.lock(); } } private: relocker& operator=(relocker const&); }; struct shared_state_base : enable_shared_from_this { typedef std::list waiter_list; typedef waiter_list::iterator notify_when_ready_handle; // This type should be only included conditionally if interruptions are allowed, but is included to maintain the same layout. typedef shared_ptr continuation_ptr_type; typedef std::vector continuations_type; boost::exception_ptr exception; bool done; bool is_valid_; bool is_deferred_; bool is_constructed; launch policy_; mutable boost::mutex mutex; boost::condition_variable waiters; waiter_list external_waiters; boost::function callback; // This declaration should be only included conditionally, but is included to maintain the same layout. continuations_type continuations; executor_ptr_type ex; // This declaration should be only included conditionally, but is included to maintain the same layout. virtual void launch_continuation() { } shared_state_base(): done(false), is_valid_(true), is_deferred_(false), is_constructed(false), policy_(launch::none), continuations(), ex() {} shared_state_base(exceptional_ptr const& ex_): exception(ex_.ptr_), done(true), is_valid_(true), is_deferred_(false), is_constructed(false), policy_(launch::none), continuations(), ex() {} virtual ~shared_state_base() { } executor_ptr_type get_executor() { return ex; } 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; } bool valid(boost::unique_lock&) { return is_valid_; } bool valid() { boost::unique_lock lk(this->mutex); return valid(lk); } void invalidate(boost::unique_lock&) { is_valid_ = false; } void invalidate() { boost::unique_lock lk(this->mutex); invalidate(lk); } void validate(boost::unique_lock&) { is_valid_ = true; } void validate() { boost::unique_lock lk(this->mutex); validate(lk); } void set_deferred() { is_deferred_ = true; policy_ = launch::deferred; } void set_async() { is_deferred_ = false; policy_ = launch::async; } #ifdef BOOST_THREAD_PROVIDES_EXECUTORS void set_executor() { is_deferred_ = false; policy_ = launch::executor; } #else void set_executor() { } #endif notify_when_ready_handle notify_when_ready(boost::condition_variable_any& cv) { boost::unique_lock lock(this->mutex); do_callback(lock); return external_waiters.insert(external_waiters.end(),&cv); } void unnotify_when_ready(notify_when_ready_handle it) { boost::lock_guard lock(this->mutex); external_waiters.erase(it); } #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION void do_continuation(boost::unique_lock& lock) { if (! continuations.empty()) { continuations_type the_continuations = continuations; continuations.clear(); relocker rlk(lock); for (continuations_type::iterator it = the_continuations.begin(); it != the_continuations.end(); ++it) { (*it)->launch_continuation(); } } } #else void do_continuation(boost::unique_lock&) { } #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION virtual void set_continuation_ptr(continuation_ptr_type continuation, boost::unique_lock& lock) { continuations.push_back(continuation); if (done) { do_continuation(lock); } } #endif void mark_finished_internal(boost::unique_lock& lock) { done=true; waiters.notify_all(); for(waiter_list::const_iterator it=external_waiters.begin(), end=external_waiters.end();it!=end;++it) { (*it)->notify_all(); } do_continuation(lock); } void make_ready() { boost::unique_lock lock(this->mutex); mark_finished_internal(lock); } void do_callback(boost::unique_lock& lock) { if(callback && !done) { boost::function local_callback=callback; relocker relock(lock); local_callback(); } } virtual bool run_if_is_deferred() { boost::unique_lock lk(this->mutex); if (is_deferred_) { is_deferred_=false; execute(lk); return true; } else return false; } virtual bool run_if_is_deferred_or_ready() { boost::unique_lock lk(this->mutex); if (is_deferred_) { is_deferred_=false; execute(lk); return true; } else return done; } void wait_internal(boost::unique_lock &lk, bool rethrow=true) { do_callback(lk); if (is_deferred_) { is_deferred_=false; execute(lk); } while(!done) { waiters.wait(lk); } if(rethrow && exception) { boost::rethrow_exception(exception); } } virtual void wait(boost::unique_lock& lock, bool rethrow=true) { wait_internal(lock, rethrow); } void wait(bool rethrow=true) { boost::unique_lock lock(this->mutex); wait(lock, rethrow); } #if defined BOOST_THREAD_USES_DATETIME bool timed_wait_until(boost::system_time const& target_time) { boost::unique_lock lock(this->mutex); if (is_deferred_) return false; do_callback(lock); while(!done) { bool const success=waiters.timed_wait(lock,target_time); if(!success && !done) { return false; } } return true; } #endif #ifdef BOOST_THREAD_USES_CHRONO template future_status wait_until(const chrono::time_point& abs_time) { boost::unique_lock lock(this->mutex); if (is_deferred_) return future_status::deferred; do_callback(lock); while(!done) { cv_status const st=waiters.wait_until(lock,abs_time); if(st==cv_status::timeout && !done) { return future_status::timeout; } } return future_status::ready; } #endif void mark_exceptional_finish_internal(boost::exception_ptr const& e, boost::unique_lock& lock) { exception=e; mark_finished_internal(lock); } void mark_exceptional_finish() { boost::unique_lock lock(this->mutex); mark_exceptional_finish_internal(boost::current_exception(), lock); } void set_exception_at_thread_exit(exception_ptr e) { unique_lock lk(this->mutex); if (has_value(lk)) { throw_exception(promise_already_satisfied()); } exception=e; this->is_constructed = true; detail::make_ready_at_thread_exit(shared_from_this()); } bool has_value() const { boost::lock_guard lock(this->mutex); return done && ! exception; } bool has_value(unique_lock& ) const { return done && ! exception; } bool has_exception() const { boost::lock_guard lock(this->mutex); return done && exception; } launch launch_policy(boost::unique_lock&) const { return policy_; } future_state::state get_state(boost::unique_lock&) const { if(!done) { return future_state::waiting; } else { return future_state::ready; } } future_state::state get_state() const { boost::lock_guard guard(this->mutex); if(!done) { return future_state::waiting; } else { return future_state::ready; } } exception_ptr get_exception_ptr() { boost::unique_lock lock(this->mutex); wait_internal(lock, false); return exception; } template void set_wait_callback(F f,U* u) { boost::lock_guard lock(this->mutex); callback=boost::bind(f,boost::ref(*u)); } virtual void execute(boost::unique_lock&) {} private: shared_state_base(shared_state_base const&); shared_state_base& operator=(shared_state_base const&); }; // Used to create stand-alone futures template struct shared_state: detail::shared_state_base { #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL typedef boost::optional storage_type; #else typedef boost::csbl::unique_ptr storage_type; #endif #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES typedef T const& source_reference_type; typedef BOOST_THREAD_RV_REF(T) rvalue_source_type; typedef T move_dest_type; #elif defined BOOST_THREAD_USES_MOVE typedef typename conditional::value,T,T const&>::type source_reference_type; typedef BOOST_THREAD_RV_REF(T) rvalue_source_type; typedef T move_dest_type; #else typedef T& source_reference_type; typedef typename conditional::value, BOOST_THREAD_RV_REF(T),T const&>::type rvalue_source_type; typedef typename conditional::value, BOOST_THREAD_RV_REF(T),T>::type move_dest_type; #endif typedef const T& shared_future_get_result_type; storage_type result; shared_state(): result() {} shared_state(exceptional_ptr const& ex): detail::shared_state_base(ex), result() {} ~shared_state() { } void mark_finished_with_result_internal(source_reference_type result_, boost::unique_lock& lock) { #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result = result_; #else result.reset(new T(result_)); #endif this->mark_finished_internal(lock); } void mark_finished_with_result_internal(rvalue_source_type result_, boost::unique_lock& lock) { #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result = boost::move(result_); #elif ! defined BOOST_NO_CXX11_RVALUE_REFERENCES result.reset(new T(boost::move(result_))); #else result.reset(new T(static_cast(result_))); #endif this->mark_finished_internal(lock); } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template void mark_finished_with_result_internal(boost::unique_lock& lock, BOOST_THREAD_FWD_REF(Args)... args) { #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result.emplace(boost::forward(args)...); #else result.reset(new T(boost::forward(args)...)); #endif this->mark_finished_internal(lock); } #endif void mark_finished_with_result(source_reference_type result_) { boost::unique_lock lock(this->mutex); this->mark_finished_with_result_internal(result_, lock); } void mark_finished_with_result(rvalue_source_type result_) { boost::unique_lock lock(this->mutex); #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES mark_finished_with_result_internal(boost::move(result_), lock); #else mark_finished_with_result_internal(static_cast(result_), lock); #endif } storage_type& get_storage(boost::unique_lock& lk) { wait_internal(lk); return result; } virtual move_dest_type get(boost::unique_lock& lk) { return boost::move(*get_storage(lk)); } move_dest_type get() { boost::unique_lock lk(this->mutex); return this->get(lk); } virtual shared_future_get_result_type get_sh(boost::unique_lock& lk) { return *get_storage(lk); } shared_future_get_result_type get_sh() { boost::unique_lock lk(this->mutex); return this->get_sh(lk); } void set_value_at_thread_exit(source_reference_type result_) { unique_lock lk(this->mutex); if (this->has_value(lk)) { throw_exception(promise_already_satisfied()); } #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result = result_; #else result.reset(new T(result_)); #endif this->is_constructed = true; detail::make_ready_at_thread_exit(shared_from_this()); } void set_value_at_thread_exit(rvalue_source_type result_) { unique_lock lk(this->mutex); if (this->has_value(lk)) throw_exception(promise_already_satisfied()); #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result = boost::move(result_); #else result.reset(new T(boost::move(result_))); #endif #else #if defined BOOST_THREAD_FUTURE_USES_OPTIONAL result = boost::move(result_); #else result.reset(new T(static_cast(result_))); #endif #endif this->is_constructed = true; detail::make_ready_at_thread_exit(shared_from_this()); } private: shared_state(shared_state const&); shared_state& operator=(shared_state const&); }; template struct shared_state: detail::shared_state_base { typedef T* storage_type; typedef T& source_reference_type; typedef T& move_dest_type; typedef T& shared_future_get_result_type; T* result; shared_state(): result(0) {} shared_state(exceptional_ptr const& ex): detail::shared_state_base(ex), result(0) {} ~shared_state() { } void mark_finished_with_result_internal(source_reference_type result_, boost::unique_lock& lock) { result= &result_; mark_finished_internal(lock); } void mark_finished_with_result(source_reference_type result_) { boost::unique_lock lock(this->mutex); mark_finished_with_result_internal(result_, lock); } virtual T& get(boost::unique_lock& lock) { wait_internal(lock); return *result; } T& get() { boost::unique_lock lk(this->mutex); return get(lk); } virtual T& get_sh(boost::unique_lock& lock) { wait_internal(lock); return *result; } T& get_sh() { boost::unique_lock lock(this->mutex); return get_sh(lock); } void set_value_at_thread_exit(T& result_) { unique_lock lk(this->mutex); if (this->has_value(lk)) throw_exception(promise_already_satisfied()); result= &result_; this->is_constructed = true; detail::make_ready_at_thread_exit(shared_from_this()); } private: shared_state(shared_state const&); shared_state& operator=(shared_state const&); }; template<> struct shared_state: detail::shared_state_base { typedef void shared_future_get_result_type; typedef void move_dest_type; shared_state() {} shared_state(exceptional_ptr const& ex): detail::shared_state_base(ex) {} void mark_finished_with_result_internal(boost::unique_lock& lock) { mark_finished_internal(lock); } void mark_finished_with_result() { boost::unique_lock lock(this->mutex); mark_finished_with_result_internal(lock); } virtual void get(boost::unique_lock& lock) { this->wait_internal(lock); } void get() { boost::unique_lock lock(this->mutex); this->get(lock); } virtual void get_sh(boost::unique_lock& lock) { this->wait_internal(lock); } void get_sh() { boost::unique_lock lock(this->mutex); this->get_sh(lock); } void set_value_at_thread_exit() { unique_lock lk(this->mutex); if (this->has_value(lk)) { throw_exception(promise_already_satisfied()); } this->is_constructed = true; detail::make_ready_at_thread_exit(shared_from_this()); } private: shared_state(shared_state const&); shared_state& operator=(shared_state const&); }; ///////////////////////// /// future_async_shared_state_base ///////////////////////// template struct future_async_shared_state_base: shared_state { typedef shared_state base_type; protected: #ifdef BOOST_THREAD_FUTURE_BLOCKING boost::thread thr_; void join() { if (this_thread::get_id() == thr_.get_id()) { thr_.detach(); return; } if (thr_.joinable()) thr_.join(); } #endif public: future_async_shared_state_base() { this->set_async(); } ~future_async_shared_state_base() { #ifdef BOOST_THREAD_FUTURE_BLOCKING join(); #elif defined BOOST_THREAD_ASYNC_FUTURE_WAITS unique_lock lk(this->mutex); while(!this->done) { this->waiters.wait(lk); } #endif } virtual void wait(boost::unique_lock& lk, bool rethrow) { #ifdef BOOST_THREAD_FUTURE_BLOCKING { relocker rlk(lk); join(); } #endif this->base_type::wait(lk, rethrow); } }; ///////////////////////// /// future_async_shared_state ///////////////////////// template struct future_async_shared_state: future_async_shared_state_base { future_async_shared_state() { } void init(BOOST_THREAD_FWD_REF(Fp) f) { #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::forward(f)); #else boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::forward(f)).detach(); #endif } static void run(shared_ptr that, BOOST_THREAD_FWD_REF(Fp) f) { try { that->mark_finished_with_result(f()); } catch(...) { that->mark_exceptional_finish(); } } }; template struct future_async_shared_state: public future_async_shared_state_base { void init(BOOST_THREAD_FWD_REF(Fp) f) { #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)); #else boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)).detach(); #endif } static void run(shared_ptr that, BOOST_THREAD_FWD_REF(Fp) f) { try { f(); that->mark_finished_with_result(); } catch(...) { that->mark_exceptional_finish(); } } }; template struct future_async_shared_state: future_async_shared_state_base { void init(BOOST_THREAD_FWD_REF(Fp) f) { #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)); #else boost::thread(&future_async_shared_state::run, static_shared_from_this(this), boost::move(f)).detach(); #endif } static void run(shared_ptr that, BOOST_THREAD_FWD_REF(Fp) f) { try { that->mark_finished_with_result(f()); } catch(...) { that->mark_exceptional_finish(); } } }; ////////////////////////// /// future_deferred_shared_state ////////////////////////// template struct future_deferred_shared_state: shared_state { typedef shared_state base_type; Fp func_; public: explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f) : func_(boost::move(f)) { this->set_deferred(); } virtual void execute(boost::unique_lock& lck) { try { Fp local_fuct=boost::move(func_); relocker relock(lck); Rp res = local_fuct(); relock.lock(); this->mark_finished_with_result_internal(boost::move(res), lck); } catch (...) { this->mark_exceptional_finish_internal(current_exception(), lck); } } }; template struct future_deferred_shared_state: shared_state { typedef shared_state base_type; Fp func_; public: explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f) : func_(boost::move(f)) { this->set_deferred(); } virtual void execute(boost::unique_lock& lck) { try { this->mark_finished_with_result_internal(func_(), lck); } catch (...) { this->mark_exceptional_finish_internal(current_exception(), lck); } } }; template struct future_deferred_shared_state: shared_state { typedef shared_state base_type; Fp func_; public: explicit future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f) : func_(boost::move(f)) { this->set_deferred(); } virtual void execute(boost::unique_lock& lck) { try { Fp local_fuct=boost::move(func_); relocker relock(lck); local_fuct(); relock.lock(); this->mark_finished_with_result_internal(lck); } catch (...) { this->mark_exceptional_finish_internal(current_exception(), lck); } } }; class future_waiter { public: typedef std::vector::size_type count_type; private: struct registered_waiter; struct registered_waiter { boost::shared_ptr future_; detail::shared_state_base::notify_when_ready_handle handle; count_type index; registered_waiter(boost::shared_ptr const& a_future, detail::shared_state_base::notify_when_ready_handle handle_, count_type index_): future_(a_future),handle(handle_),index(index_) {} }; struct all_futures_lock { #ifdef _MANAGED typedef std::ptrdiff_t count_type_portable; #else typedef count_type count_type_portable; #endif count_type_portable count; boost::scoped_array > locks; all_futures_lock(std::vector& futures): count(futures.size()),locks(new boost::unique_lock[count]) { for(count_type_portable i=0;i(futures[i].future_->mutex)); } } void lock() { boost::lock(locks.get(),locks.get()+count); } void unlock() { for(count_type_portable i=0;i futures_; count_type future_count; public: future_waiter(): future_count(0) {} template void add(F& f) { if(f.future_) { registered_waiter waiter(f.future_,f.future_->notify_when_ready(cv),future_count); try { futures_.push_back(waiter); } catch(...) { f.future_->unnotify_when_ready(waiter.handle); throw; } } ++future_count; } #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES template void add(F1& f1, Fs&... fs) { add(f1); add(fs...); } #endif count_type wait() { all_futures_lock lk(futures_); for(;;) { for(count_type i=0;idone) { return futures_[i].index; } } cv.wait(lk); } } ~future_waiter() { for(count_type i=0;iunnotify_when_ready(futures_[i].handle); } } }; } template class BOOST_THREAD_FUTURE; template class shared_future; template struct is_future_type > : true_type { }; template struct is_future_type > : true_type { }; // template // typename boost::disable_if,Iterator>::type wait_for_any(Iterator begin,Iterator end) // { // if(begin==end) // return end; // // detail::future_waiter waiter; // for(Iterator current=begin;current!=end;++current) // { // waiter.add(*current); // } // return boost::next(begin,waiter.wait()); // } #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES template typename boost::enable_if,typename detail::future_waiter::count_type>::type wait_for_any(F1& f1,F2& f2) { detail::future_waiter waiter; waiter.add(f1); waiter.add(f2); return waiter.wait(); } template typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3) { detail::future_waiter waiter; waiter.add(f1); waiter.add(f2); waiter.add(f3); return waiter.wait(); } template typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3,F4& f4) { detail::future_waiter waiter; waiter.add(f1); waiter.add(f2); waiter.add(f3); waiter.add(f4); return waiter.wait(); } template typename detail::future_waiter::count_type wait_for_any(F1& f1,F2& f2,F3& f3,F4& f4,F5& f5) { detail::future_waiter waiter; waiter.add(f1); waiter.add(f2); waiter.add(f3); waiter.add(f4); waiter.add(f5); return waiter.wait(); } #else template typename boost::enable_if, typename detail::future_waiter::count_type>::type wait_for_any(F1& f1, Fs&... fs) { detail::future_waiter waiter; waiter.add(f1, fs...); return waiter.wait(); } #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template class promise; template class packaged_task; namespace detail { /// Common implementation for all the futures independently of the return type class base_future { public: }; /// Common implementation for future and shared_future. template class basic_future : public base_future { protected: public: typedef boost::shared_ptr > future_ptr; typedef typename detail::shared_state::move_dest_type move_dest_type; static //BOOST_CONSTEXPR future_ptr make_exceptional_future_ptr(exceptional_ptr const& ex) { return future_ptr(new detail::shared_state(ex)); } future_ptr future_; basic_future(future_ptr a_future): future_(a_future) { } public: typedef future_state::state state; BOOST_THREAD_MOVABLE_ONLY(basic_future) basic_future(): future_() {} //BOOST_CONSTEXPR basic_future(exceptional_ptr const& ex) : future_(make_exceptional_future_ptr(ex)) { } ~basic_future() { } basic_future(BOOST_THREAD_RV_REF(basic_future) other) BOOST_NOEXCEPT: future_(BOOST_THREAD_RV(other).future_) { BOOST_THREAD_RV(other).future_.reset(); } basic_future& operator=(BOOST_THREAD_RV_REF(basic_future) other) BOOST_NOEXCEPT { future_=BOOST_THREAD_RV(other).future_; BOOST_THREAD_RV(other).future_.reset(); return *this; } void swap(basic_future& that) BOOST_NOEXCEPT { future_.swap(that.future_); } // functions to check state, and wait for ready state get_state(boost::unique_lock& lk) const { if(!future_) { return future_state::uninitialized; } return future_->get_state(lk); } state get_state() const { if(!future_) { return future_state::uninitialized; } return future_->get_state(); } bool is_ready() const { return get_state()==future_state::ready; } bool is_ready(boost::unique_lock& lk) const { return get_state(lk)==future_state::ready; } bool has_exception() const { return future_ && future_->has_exception(); } bool has_value() const { return future_ && future_->has_value(); } launch launch_policy(boost::unique_lock& lk) const { if ( future_ ) return future_->launch_policy(lk); else return launch(launch::none); } launch launch_policy() const { if ( future_ ) { boost::unique_lock lk(this->future_->mutex); return future_->launch_policy(lk); } else return launch(launch::none); } exception_ptr get_exception_ptr() { return future_ ? future_->get_exception_ptr() : exception_ptr(); } bool valid() const BOOST_NOEXCEPT { return future_ != 0 && future_->valid(); } void wait() const { if(!future_) { boost::throw_exception(future_uninitialized()); } future_->wait(false); } typedef detail::shared_state_base::notify_when_ready_handle notify_when_ready_handle; boost::mutex& mutex() { if(!future_) { boost::throw_exception(future_uninitialized()); } return future_->mutex; } notify_when_ready_handle notify_when_ready(boost::condition_variable_any& cv) { if(!future_) { boost::throw_exception(future_uninitialized()); } return future_->notify_when_ready(cv); } void unnotify_when_ready(notify_when_ready_handle h) { if(!future_) { boost::throw_exception(future_uninitialized()); } return future_->unnotify_when_ready(h); } #if defined BOOST_THREAD_USES_DATETIME template bool timed_wait(Duration const& rel_time) const { return timed_wait_until(boost::get_system_time()+rel_time); } bool timed_wait_until(boost::system_time const& abs_time) const { if(!future_) { boost::throw_exception(future_uninitialized()); } return future_->timed_wait_until(abs_time); } #endif #ifdef BOOST_THREAD_USES_CHRONO template future_status wait_for(const chrono::duration& rel_time) const { return wait_until(chrono::steady_clock::now() + rel_time); } template future_status wait_until(const chrono::time_point& abs_time) const { if(!future_) { boost::throw_exception(future_uninitialized()); } return future_->wait_until(abs_time); } #endif }; } // detail BOOST_THREAD_DCL_MOVABLE_BEG(R) detail::basic_future BOOST_THREAD_DCL_MOVABLE_END namespace detail { #if (!defined _MSC_VER || _MSC_VER >= 1400) // _MSC_VER == 1400 on MSVC 2005 template BOOST_THREAD_FUTURE make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f); template BOOST_THREAD_FUTURE make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f); #endif // #if (!defined _MSC_VER || _MSC_VER >= 1400) #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template struct future_deferred_continuation_shared_state; template struct future_async_continuation_shared_state; template BOOST_THREAD_FUTURE make_future_async_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE make_future_sync_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE make_future_deferred_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE make_shared_future_deferred_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template BOOST_THREAD_FUTURE make_shared_future_sync_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS 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); 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); template BOOST_THREAD_FUTURE make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f); #endif #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP template struct future_unwrap_shared_state; template inline BOOST_THREAD_FUTURE make_future_unwrap_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f); #endif } #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY) template< typename InputIterator> typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_all(InputIterator first, InputIterator last); inline BOOST_THREAD_FUTURE > when_all(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> BOOST_THREAD_FUTURE::type, typename decay::type...> > when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif template< typename InputIterator> typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_any(InputIterator first, InputIterator last); inline BOOST_THREAD_FUTURE > when_any(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> BOOST_THREAD_FUTURE::type, typename decay::type...> > when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY template class BOOST_THREAD_FUTURE : public detail::basic_future { private: typedef detail::basic_future base_type; typedef typename base_type::future_ptr future_ptr; friend class shared_future; friend class promise; #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template friend struct detail::future_async_continuation_shared_state; template friend struct detail::future_deferred_continuation_shared_state; template friend BOOST_THREAD_FUTURE detail::make_future_async_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_sync_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_deferred_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_deferred_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_sync_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS 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); 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); template friend BOOST_THREAD_FUTURE detail::make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f); #endif #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP template friend struct detail::future_unwrap_shared_state; template friend BOOST_THREAD_FUTURE detail::make_future_unwrap_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f); #endif #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY) template< typename InputIterator> friend typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_all(InputIterator first, InputIterator last); //friend inline BOOST_THREAD_FUTURE > when_all(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> friend BOOST_THREAD_FUTURE::type, typename decay::type...> > when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif template< typename InputIterator> friend typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_any(InputIterator first, InputIterator last); //friend inline BOOST_THREAD_FUTURE > when_any(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> friend BOOST_THREAD_FUTURE::type, typename decay::type...> > when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK template friend class packaged_task; // todo check if this works in windows #else friend class packaged_task; #endif friend class detail::future_waiter; template friend BOOST_THREAD_FUTURE detail::make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f); template friend BOOST_THREAD_FUTURE detail::make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f); typedef typename base_type::move_dest_type move_dest_type; BOOST_THREAD_FUTURE(future_ptr a_future): base_type(a_future) { } public: BOOST_THREAD_MOVABLE_ONLY(BOOST_THREAD_FUTURE) typedef future_state::state state; typedef R value_type; // EXTENSION BOOST_CONSTEXPR BOOST_THREAD_FUTURE() {} //BOOST_CONSTEXPR BOOST_THREAD_FUTURE(exceptional_ptr const& ex): base_type(ex) {} ~BOOST_THREAD_FUTURE() { } BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT: base_type(boost::move(static_cast(BOOST_THREAD_RV(other)))) { } inline explicit BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE >) other); // EXTENSION explicit BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(shared_future) other) : base_type(boost::move(static_cast(BOOST_THREAD_RV(other)))) {} BOOST_THREAD_FUTURE& operator=(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT { this->base_type::operator=(boost::move(static_cast(BOOST_THREAD_RV(other)))); return *this; } shared_future share() { return shared_future(::boost::move(*this)); } void swap(BOOST_THREAD_FUTURE& other) { static_cast(this)->swap(other); } // todo this function must be private and friendship provided to the internal users. void set_async() { this->future_->set_async(); } // todo this function must be private and friendship provided to the internal users. void set_deferred() { this->future_->set_deferred(); } bool run_if_is_deferred() { return this->future_->run_if_is_deferred(); } bool run_if_is_deferred_or_ready() { return this->future_->run_if_is_deferred_or_ready(); } // retrieving the value move_dest_type get() { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif return this->future_->get(lk); } template typename boost::disable_if< is_void, move_dest_type>::type get_or(BOOST_THREAD_RV_REF(R2) v) { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } this->future_->wait(lk, false); #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif if (this->future_->has_value(lk)) { return this->future_->get(lk); } else { return boost::move(v); } } template typename boost::disable_if< is_void, move_dest_type>::type get_or(R2 const& v) // EXTENSION { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } this->future_->wait(lk, false); #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif if (this->future_->has_value(lk)) { return this->future_->get(lk); } else { return v; } } #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template inline BOOST_THREAD_FUTURE::type> then(BOOST_THREAD_FWD_REF(F) func); // EXTENSION template inline BOOST_THREAD_FUTURE::type> then(launch policy, BOOST_THREAD_FWD_REF(F) func); // EXTENSION #ifdef BOOST_THREAD_PROVIDES_EXECUTORS template inline BOOST_THREAD_FUTURE::type> then(Ex& ex, BOOST_THREAD_FWD_REF(F) func); // EXTENSION #endif template inline typename boost::disable_if< is_void, BOOST_THREAD_FUTURE >::type fallback_to(BOOST_THREAD_RV_REF(R2) v); // EXTENSION template inline typename boost::disable_if< is_void, BOOST_THREAD_FUTURE >::type fallback_to(R2 const& v); // EXTENSION #endif }; BOOST_THREAD_DCL_MOVABLE_BEG(T) BOOST_THREAD_FUTURE BOOST_THREAD_DCL_MOVABLE_END template class BOOST_THREAD_FUTURE > : public detail::basic_future > { typedef BOOST_THREAD_FUTURE R; private: typedef detail::basic_future base_type; typedef typename base_type::future_ptr future_ptr; friend class shared_future; friend class promise; #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template friend struct detail::future_async_continuation_shared_state; template friend struct detail::future_deferred_continuation_shared_state; template friend BOOST_THREAD_FUTURE detail::make_future_async_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_sync_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_deferred_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_deferred_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_async_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_shared_future_sync_continuation_shared_state(boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS 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); 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); template friend BOOST_THREAD_FUTURE detail::make_future_executor_shared_state(Executor& ex, BOOST_THREAD_FWD_REF(Fp) f); #endif #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP template friend struct detail::future_unwrap_shared_state; template friend BOOST_THREAD_FUTURE detail::make_future_unwrap_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f); #endif #if defined(BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY) template< typename InputIterator> friend typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_all(InputIterator first, InputIterator last); friend inline BOOST_THREAD_FUTURE > when_all(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> friend BOOST_THREAD_FUTURE::type, typename decay::type...> > when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif template< typename InputIterator> friend typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_any(InputIterator first, InputIterator last); friend inline BOOST_THREAD_FUTURE > when_any(); #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> friend BOOST_THREAD_FUTURE::type, typename decay::type...> > when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures); #endif #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK template friend class packaged_task; // todo check if this works in windows #else friend class packaged_task; #endif friend class detail::future_waiter; template friend BOOST_THREAD_FUTURE detail::make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f); template friend BOOST_THREAD_FUTURE detail::make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f); typedef typename base_type::move_dest_type move_dest_type; BOOST_THREAD_FUTURE(future_ptr a_future): base_type(a_future) { } public: BOOST_THREAD_MOVABLE_ONLY(BOOST_THREAD_FUTURE) typedef future_state::state state; typedef R value_type; // EXTENSION BOOST_CONSTEXPR BOOST_THREAD_FUTURE() {} //BOOST_CONSTEXPR BOOST_THREAD_FUTURE(exceptional_ptr const& ex): base_type(ex) {} ~BOOST_THREAD_FUTURE() { } BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT: base_type(boost::move(static_cast(BOOST_THREAD_RV(other)))) { } BOOST_THREAD_FUTURE& operator=(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE) other) BOOST_NOEXCEPT { this->base_type::operator=(boost::move(static_cast(BOOST_THREAD_RV(other)))); return *this; } shared_future share() { return shared_future(::boost::move(*this)); } void swap(BOOST_THREAD_FUTURE& other) { static_cast(this)->swap(other); } // todo this function must be private and friendship provided to the internal users. void set_async() { this->future_->set_async(); } // todo this function must be private and friendship provided to the internal users. void set_deferred() { this->future_->set_deferred(); } bool run_if_is_deferred() { return this->future_->run_if_is_deferred(); } bool run_if_is_deferred_or_ready() { return this->future_->run_if_is_deferred_or_ready(); } // retrieving the value move_dest_type get() { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif return this->future_->get(lk); } move_dest_type get_or(BOOST_THREAD_RV_REF(R) v) // EXTENSION { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } this->future_->wait(lk, false); #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif if (this->future_->has_value(lk)) return this->future_->get(lk); else return boost::move(v); } move_dest_type get_or(R const& v) // EXTENSION { if (this->future_ == 0) { boost::throw_exception(future_uninitialized()); } unique_lock lk(this->future_->mutex); if (! this->future_->valid(lk)) { boost::throw_exception(future_uninitialized()); } this->future_->wait(lk, false); #ifdef BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET this->future_->invalidate(lk); #endif if (this->future_->has_value(lk)) return this->future_->get(lk); else return v; } #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template inline BOOST_THREAD_FUTURE::type> then(BOOST_THREAD_FWD_REF(F) func); // EXTENSION template inline BOOST_THREAD_FUTURE::type> then(launch policy, BOOST_THREAD_FWD_REF(F) func); // EXTENSION #ifdef BOOST_THREAD_PROVIDES_EXECUTORS template inline BOOST_THREAD_FUTURE::type> then(Ex &ex, BOOST_THREAD_FWD_REF(F) func); // EXTENSION #endif #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP inline BOOST_THREAD_FUTURE unwrap(); // EXTENSION #endif }; template class shared_future : public detail::basic_future { typedef detail::basic_future base_type; typedef typename base_type::future_ptr future_ptr; friend class detail::future_waiter; friend class promise; #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template friend struct detail::future_async_continuation_shared_state; template friend struct detail::future_deferred_continuation_shared_state; template friend BOOST_THREAD_FUTURE detail::make_future_async_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_sync_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); template friend BOOST_THREAD_FUTURE detail::make_future_deferred_continuation_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c); #endif #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK template friend class packaged_task;// todo check if this works in windows #else friend class packaged_task; #endif shared_future(future_ptr a_future): base_type(a_future) {} public: BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_future) typedef R value_type; // EXTENSION shared_future(shared_future const& other): base_type(other.future_) {} typedef future_state::state state; BOOST_CONSTEXPR shared_future() {} //BOOST_CONSTEXPR shared_future(exceptional_ptr const& ex): base_type(ex) {} ~shared_future() {} shared_future& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_future) other) { this->future_ = other.future_; return *this; } shared_future(BOOST_THREAD_RV_REF(shared_future) other) BOOST_NOEXCEPT : base_type(boost::move(static_cast(BOOST_THREAD_RV(other)))) { } shared_future(BOOST_THREAD_RV_REF( BOOST_THREAD_FUTURE ) other) BOOST_NOEXCEPT : base_type(boost::move(static_cast(BOOST_THREAD_RV(other)))) { } shared_future& operator=(BOOST_THREAD_RV_REF(shared_future) other) BOOST_NOEXCEPT { base_type::operator=(boost::move(static_cast(BOOST_THREAD_RV(other)))); return *this; } shared_future& operator=(BOOST_THREAD_RV_REF( BOOST_THREAD_FUTURE ) other) BOOST_NOEXCEPT { base_type::operator=(boost::move(static_cast(BOOST_THREAD_RV(other)))); return *this; } void swap(shared_future& other) BOOST_NOEXCEPT { static_cast(this)->swap(other); } bool run_if_is_deferred() { return this->future_->run_if_is_deferred(); } bool run_if_is_deferred_or_ready() { return this->future_->run_if_is_deferred_or_ready(); } // retrieving the value typename detail::shared_state::shared_future_get_result_type get() const { if(!this->future_) { boost::throw_exception(future_uninitialized()); } return this->future_->get_sh(); } template typename boost::disable_if< is_void, typename detail::shared_state::shared_future_get_result_type>::type get_or(BOOST_THREAD_RV_REF(R2) v) const // EXTENSION { if(!this->future_) { boost::throw_exception(future_uninitialized()); } this->future_->wait(); if (this->future_->has_value()) return this->future_->get_sh(); else return boost::move(v); } #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION template inline BOOST_THREAD_FUTURE::type> then(BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION template inline BOOST_THREAD_FUTURE::type> then(launch policy, BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION #ifdef BOOST_THREAD_PROVIDES_EXECUTORS template inline BOOST_THREAD_FUTURE::type> then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) const; // EXTENSION #endif #endif }; BOOST_THREAD_DCL_MOVABLE_BEG(T) shared_future BOOST_THREAD_DCL_MOVABLE_END template class promise { typedef boost::shared_ptr > future_ptr; typedef typename detail::shared_state::source_reference_type source_reference_type; typedef typename detail::shared_state::rvalue_source_type rvalue_source_type; typedef typename detail::shared_state::move_dest_type move_dest_type; typedef typename detail::shared_state::shared_future_get_result_type shared_future_get_result_type; future_ptr future_; bool future_obtained; void lazy_init() { #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY #include if(!atomic_load(&future_)) { future_ptr blank; atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state)); } #include #endif } public: BOOST_THREAD_MOVABLE_ONLY(promise) #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS template promise(boost::allocator_arg_t, Allocator a) { typedef typename Allocator::template rebind >::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state(), D(a2, 1) ); future_obtained = false; } #endif promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY future_(), #else future_(new detail::shared_state()), #endif future_obtained(false) {} ~promise() { if(future_) { boost::unique_lock lock(future_->mutex); if(!future_->done && !future_->is_constructed) { future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock); } } } // Assignment promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT : future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained) { BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; } promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT { future_=BOOST_THREAD_RV(rhs).future_; future_obtained=BOOST_THREAD_RV(rhs).future_obtained; BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; return *this; } void swap(promise& other) { future_.swap(other.future_); 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() { lazy_init(); if (future_.get()==0) { boost::throw_exception(promise_moved()); } if (future_obtained) { boost::throw_exception(future_already_retrieved()); } future_obtained=true; return BOOST_THREAD_FUTURE(future_); } #if defined BOOST_NO_CXX11_RVALUE_REFERENCES template typename boost::enable_if_c::value && is_same::value, void>::type set_value(TR const & r) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_finished_with_result_internal(r, lock); } #else void set_value(source_reference_type r) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_finished_with_result_internal(r, lock); } #endif void set_value(rvalue_source_type r) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES future_->mark_finished_with_result_internal(boost::move(r), lock); #else future_->mark_finished_with_result_internal(static_cast(r), lock); #endif } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template void emplace(BOOST_THREAD_FWD_REF(Args) ...args) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_finished_with_result_internal(lock, boost::forward(args)...); } #endif void set_exception(boost::exception_ptr p) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_exceptional_finish_internal(p, lock); } template void set_exception(E ex) { set_exception(boost::copy_exception(ex)); } // setting the result with deferred notification #if defined BOOST_NO_CXX11_RVALUE_REFERENCES template typename boost::enable_if_c::value && is_same::value, void>::type set_value_at_thread_exit(TR const& r) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_value_at_thread_exit(r); } #else void set_value_at_thread_exit(source_reference_type r) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_value_at_thread_exit(r); } #endif void set_value_at_thread_exit(BOOST_THREAD_RV_REF(R) r) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_value_at_thread_exit(boost::move(r)); } void set_exception_at_thread_exit(exception_ptr e) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_exception_at_thread_exit(e); } template void set_exception_at_thread_exit(E ex) { set_exception_at_thread_exit(boost::copy_exception(ex)); } template void set_wait_callback(F f) { lazy_init(); future_->set_wait_callback(f,this); } }; template class promise { typedef boost::shared_ptr > future_ptr; future_ptr future_; bool future_obtained; void lazy_init() { #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY #include if(!atomic_load(&future_)) { future_ptr blank; atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state)); } #include #endif } public: BOOST_THREAD_MOVABLE_ONLY(promise) #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS template promise(boost::allocator_arg_t, Allocator a) { typedef typename Allocator::template rebind >::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state(), D(a2, 1) ); future_obtained = false; } #endif promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY future_(), #else future_(new detail::shared_state()), #endif future_obtained(false) {} ~promise() { if(future_) { boost::unique_lock lock(future_->mutex); if(!future_->done && !future_->is_constructed) { future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock); } } } // Assignment promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT : future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained) { BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; } promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT { future_=BOOST_THREAD_RV(rhs).future_; future_obtained=BOOST_THREAD_RV(rhs).future_obtained; BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; return *this; } void swap(promise& other) { future_.swap(other.future_); std::swap(future_obtained,other.future_obtained); } // Result retrieval BOOST_THREAD_FUTURE get_future() { lazy_init(); if (future_.get()==0) { boost::throw_exception(promise_moved()); } if (future_obtained) { boost::throw_exception(future_already_retrieved()); } future_obtained=true; return BOOST_THREAD_FUTURE(future_); } void set_value(R& r) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_finished_with_result_internal(r, lock); } void set_exception(boost::exception_ptr p) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_exceptional_finish_internal(p, lock); } template void set_exception(E ex) { set_exception(boost::copy_exception(ex)); } // setting the result with deferred notification void set_value_at_thread_exit(R& r) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_value_at_thread_exit(r); } void set_exception_at_thread_exit(exception_ptr e) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_exception_at_thread_exit(e); } template void set_exception_at_thread_exit(E ex) { set_exception_at_thread_exit(boost::copy_exception(ex)); } template void set_wait_callback(F f) { lazy_init(); future_->set_wait_callback(f,this); } }; template <> class promise { typedef boost::shared_ptr > future_ptr; future_ptr future_; bool future_obtained; void lazy_init() { #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY if(!atomic_load(&future_)) { future_ptr blank; atomic_compare_exchange(&future_,&blank,future_ptr(new detail::shared_state)); } #endif } public: BOOST_THREAD_MOVABLE_ONLY(promise) #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS template promise(boost::allocator_arg_t, Allocator a) { typedef typename Allocator::template rebind >::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; future_ = future_ptr(::new(a2.allocate(1)) detail::shared_state(), D(a2, 1) ); future_obtained = false; } #endif promise(): #if defined BOOST_THREAD_PROVIDES_PROMISE_LAZY future_(), #else future_(new detail::shared_state), #endif future_obtained(false) {} ~promise() { if(future_) { boost::unique_lock lock(future_->mutex); if(!future_->done && !future_->is_constructed) { future_->mark_exceptional_finish_internal(boost::copy_exception(broken_promise()), lock); } } } // Assignment promise(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT : future_(BOOST_THREAD_RV(rhs).future_),future_obtained(BOOST_THREAD_RV(rhs).future_obtained) { // we need to release the future as shared_ptr doesn't implements move semantics BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; } promise & operator=(BOOST_THREAD_RV_REF(promise) rhs) BOOST_NOEXCEPT { future_=BOOST_THREAD_RV(rhs).future_; future_obtained=BOOST_THREAD_RV(rhs).future_obtained; BOOST_THREAD_RV(rhs).future_.reset(); BOOST_THREAD_RV(rhs).future_obtained=false; return *this; } void swap(promise& other) { future_.swap(other.future_); std::swap(future_obtained,other.future_obtained); } // Result retrieval BOOST_THREAD_FUTURE get_future() { lazy_init(); if (future_.get()==0) { boost::throw_exception(promise_moved()); } if(future_obtained) { boost::throw_exception(future_already_retrieved()); } future_obtained=true; //return BOOST_THREAD_MAKE_RV_REF(BOOST_THREAD_FUTURE(future_)); return BOOST_THREAD_FUTURE(future_); } void set_value() { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_finished_with_result_internal(lock); } void set_exception(boost::exception_ptr p) { lazy_init(); boost::unique_lock lock(future_->mutex); if(future_->done) { boost::throw_exception(promise_already_satisfied()); } future_->mark_exceptional_finish_internal(p,lock); } template void set_exception(E ex) { set_exception(boost::copy_exception(ex)); } // setting the result with deferred notification void set_value_at_thread_exit() { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_value_at_thread_exit(); } void set_exception_at_thread_exit(exception_ptr e) { if (future_.get()==0) { boost::throw_exception(promise_moved()); } future_->set_exception_at_thread_exit(e); } template void set_exception_at_thread_exit(E ex) { set_exception_at_thread_exit(boost::copy_exception(ex)); } template void set_wait_callback(F f) { lazy_init(); future_->set_wait_callback(f,this); } }; } #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS namespace boost { namespace container { template struct uses_allocator< ::boost::promise , Alloc> : true_type { }; }} #if ! defined BOOST_NO_CXX11_ALLOCATOR namespace std { template struct uses_allocator< ::boost::promise , Alloc> : true_type { }; } #endif #endif namespace boost { BOOST_THREAD_DCL_MOVABLE_BEG(T) promise BOOST_THREAD_DCL_MOVABLE_END namespace detail { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK template struct task_base_shared_state; #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_base_shared_state: #else template struct task_base_shared_state: #endif #else template struct task_base_shared_state: #endif detail::shared_state { bool started; task_base_shared_state(): started(false) {} void reset() { // todo The packaged_task::reset must be as if an assignemnt froma new packaged_task with the same function // the reset function is an optimization that avoids reallocating a new task. started=false; this->validate(); } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) virtual void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args)=0; void run(BOOST_THREAD_RV_REF(ArgTypes) ... args) #else virtual void do_run()=0; void run() #endif { { boost::lock_guard lk(this->mutex); if(started) { boost::throw_exception(task_already_started()); } started=true; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) do_run(boost::move(args)...); #else do_run(); #endif } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) virtual void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args)=0; void apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) #else virtual void do_apply()=0; void apply() #endif { { boost::lock_guard lk(this->mutex); if(started) { boost::throw_exception(task_already_started()); } started=true; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) do_apply(boost::move(args)...); #else do_apply(); #endif } void owner_destroyed() { boost::unique_lock lk(this->mutex); if(!started) { started=true; this->mark_exceptional_finish_internal(boost::copy_exception(boost::broken_promise()), lk); } } }; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK template struct task_shared_state; #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template struct task_shared_state: task_base_shared_state #endif #else template struct task_shared_state: task_base_shared_state #endif { private: task_shared_state(task_shared_state&); public: F f; task_shared_state(F const& f_): f(f_) {} task_shared_state(BOOST_THREAD_RV_REF(F) f_): f(boost::move(f_)) {} F callable() { return boost::move(f); } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->set_value_at_thread_exit(f(boost::move(args)...)); } #else void do_apply() { try { this->set_value_at_thread_exit(f()); } #endif catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->mark_finished_with_result(f(boost::move(args)...)); } #else void do_run() { try { #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES R res((f())); this->mark_finished_with_result(boost::move(res)); #else this->mark_finished_with_result(f()); #endif } #endif catch(...) { this->mark_exceptional_finish(); } } }; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template struct task_shared_state: task_base_shared_state #endif #else template struct task_shared_state: task_base_shared_state #endif { private: task_shared_state(task_shared_state&); public: F f; task_shared_state(F const& f_): f(f_) {} task_shared_state(BOOST_THREAD_RV_REF(F) f_): f(boost::move(f_)) {} F callable() { return f; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->set_value_at_thread_exit(f(boost::move(args)...)); } #else void do_apply() { try { this->set_value_at_thread_exit(f()); } #endif catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->mark_finished_with_result(f(boost::move(args)...)); } #else void do_run() { try { R& res((f())); this->mark_finished_with_result(res); } #endif catch(...) { this->mark_exceptional_finish(); } } }; #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR) #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template struct task_shared_state: task_base_shared_state #endif #else template struct task_shared_state : task_base_shared_state #endif { private: task_shared_state(task_shared_state&); #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef R (*CallableType)(ArgTypes ... ); #else typedef R (*CallableType)(); #endif public: CallableType f; task_shared_state(CallableType f_): f(f_) {} CallableType callable() { return f; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->set_value_at_thread_exit(f(boost::move(args)...)); } #else void do_apply() { try { R r((f())); this->set_value_at_thread_exit(boost::move(r)); } #endif catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->mark_finished_with_result(f(boost::move(args)...)); } #else void do_run() { try { R res((f())); this->mark_finished_with_result(boost::move(res)); } #endif catch(...) { this->mark_exceptional_finish(); } } }; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template struct task_shared_state: task_base_shared_state #endif #else template struct task_shared_state : task_base_shared_state #endif { private: task_shared_state(task_shared_state&); public: #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef R& (*CallableType)(BOOST_THREAD_RV_REF(ArgTypes) ... ); #else typedef R& (*CallableType)(); #endif CallableType f; task_shared_state(CallableType f_): f(f_) {} CallableType callable() { return boost::move(f); } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->set_value_at_thread_exit(f(boost::move(args)...)); } #else void do_apply() { try { this->set_value_at_thread_exit(f()); } #endif catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { this->mark_finished_with_result(f(boost::move(args)...)); } #else void do_run() { try { this->mark_finished_with_result(f()); } #endif catch(...) { this->mark_exceptional_finish(); } } }; #endif #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template struct task_shared_state: task_base_shared_state #endif #else template struct task_shared_state: task_base_shared_state #endif { private: task_shared_state(task_shared_state&); public: typedef F CallableType; F f; task_shared_state(F const& f_): f(f_) {} task_shared_state(BOOST_THREAD_RV_REF(F) f_): f(boost::move(f_)) {} F callable() { return boost::move(f); } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { f(boost::move(args)...); #else void do_apply() { try { f(); #endif this->set_value_at_thread_exit(); } catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { f(boost::move(args)...); #else void do_run() { try { f(); #endif this->mark_finished_with_result(); } catch(...) { this->mark_exceptional_finish(); } } }; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template struct task_shared_state: task_base_shared_state #else template<> struct task_shared_state: task_base_shared_state #endif #else template<> struct task_shared_state: task_base_shared_state #endif { private: task_shared_state(task_shared_state&); #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef void (*CallableType)(ArgTypes...); #else typedef void (*CallableType)(); #endif public: CallableType f; task_shared_state(CallableType f_): f(f_) {} CallableType callable() { return f; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_apply(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { f(boost::move(args)...); #else void do_apply() { try { f(); #endif this->set_value_at_thread_exit(); } catch(...) { this->set_exception_at_thread_exit(current_exception()); } } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void do_run(BOOST_THREAD_RV_REF(ArgTypes) ... args) { try { f(boost::move(args)...); #else void do_run() { try { f(); #endif this->mark_finished_with_result(); } catch(...) { this->mark_exceptional_finish(); } } }; } #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template class packaged_task { typedef boost::shared_ptr > task_ptr; boost::shared_ptr > task; #else template class packaged_task { typedef boost::shared_ptr > task_ptr; boost::shared_ptr > task; #endif #else template class packaged_task { typedef boost::shared_ptr > task_ptr; boost::shared_ptr > task; #endif bool future_obtained; struct dummy; public: typedef R result_type; BOOST_THREAD_MOVABLE_ONLY(packaged_task) packaged_task(): future_obtained(false) {} // construction and destruction #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR) #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) explicit packaged_task(R(*f)(), BOOST_THREAD_FWD_REF(ArgTypes)... args) { typedef R(*FR)(BOOST_THREAD_FWD_REF(ArgTypes)...); typedef detail::task_shared_state task_shared_state_type; task= task_ptr(new task_shared_state_type(f, boost::move(args)...)); future_obtained=false; } #else explicit packaged_task(R(*f)()) { typedef R(*FR)(); typedef detail::task_shared_state task_shared_state_type; task= task_ptr(new task_shared_state_type(f)); future_obtained=false; } #endif #else explicit packaged_task(R(*f)()) { typedef R(*FR)(); typedef detail::task_shared_state task_shared_state_type; task= task_ptr(new task_shared_state_type(f)); future_obtained=false; } #endif #endif #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES template explicit packaged_task(BOOST_THREAD_FWD_REF(F) f , typename boost::disable_if::type, packaged_task>, dummy* >::type=0 ) { typedef typename decay::type FR; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif task = task_ptr(new task_shared_state_type(boost::forward(f))); future_obtained = false; } #else template explicit packaged_task(F const& f , typename boost::disable_if::type, packaged_task>, dummy* >::type=0 ) { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif task = task_ptr(new task_shared_state_type(f)); future_obtained=false; } template explicit packaged_task(BOOST_THREAD_RV_REF(F) f) { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; task = task_ptr(new task_shared_state_type(boost::move(f))); #else typedef detail::task_shared_state task_shared_state_type; task = task_ptr(new task_shared_state_type(boost::move(f))); #endif #else typedef detail::task_shared_state task_shared_state_type; task = task_ptr(new task_shared_state_type(boost::move(f))); #endif future_obtained=false; } #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS #if defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR) template packaged_task(boost::allocator_arg_t, Allocator a, R(*f)()) { typedef R(*FR)(); #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif typedef typename Allocator::template rebind::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(f), D(a2, 1) ); future_obtained = false; } #endif // BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES template packaged_task(boost::allocator_arg_t, Allocator a, BOOST_THREAD_FWD_REF(F) f) { typedef typename decay::type FR; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif typedef typename Allocator::template rebind::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(boost::forward(f)), D(a2, 1) ); future_obtained = false; } #else // ! defined BOOST_NO_CXX11_RVALUE_REFERENCES template packaged_task(boost::allocator_arg_t, Allocator a, const F& f) { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif typedef typename Allocator::template rebind::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(f), D(a2, 1) ); future_obtained = false; } template packaged_task(boost::allocator_arg_t, Allocator a, BOOST_THREAD_RV_REF(F) f) { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) typedef detail::task_shared_state task_shared_state_type; #else typedef detail::task_shared_state task_shared_state_type; #endif #else typedef detail::task_shared_state task_shared_state_type; #endif typedef typename Allocator::template rebind::other A2; A2 a2(a); typedef thread_detail::allocator_destructor D; task = task_ptr(::new(a2.allocate(1)) task_shared_state_type(boost::move(f)), D(a2, 1) ); future_obtained = false; } #endif //BOOST_NO_CXX11_RVALUE_REFERENCES #endif // BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS ~packaged_task() { if(task) { task->owner_destroyed(); } } // assignment packaged_task(BOOST_THREAD_RV_REF(packaged_task) other) BOOST_NOEXCEPT : future_obtained(BOOST_THREAD_RV(other).future_obtained) { task.swap(BOOST_THREAD_RV(other).task); BOOST_THREAD_RV(other).future_obtained=false; } packaged_task& operator=(BOOST_THREAD_RV_REF(packaged_task) other) BOOST_NOEXCEPT { #if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES packaged_task temp(boost::move(other)); #else packaged_task temp(static_cast(other)); #endif swap(temp); 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))); // As if *this = packaged_task(task->callable()); task->reset(); future_obtained=false; } void swap(packaged_task& other) BOOST_NOEXCEPT { task.swap(other.task); std::swap(future_obtained,other.future_obtained); } bool valid() const BOOST_NOEXCEPT { return task.get()!=0; } // result retrieval BOOST_THREAD_FUTURE get_future() { if(!task) { boost::throw_exception(task_moved()); } else if(!future_obtained) { future_obtained=true; return BOOST_THREAD_FUTURE(task); } else { boost::throw_exception(future_already_retrieved()); } } // execution #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) void operator()(ArgTypes... args) { if(!task) { boost::throw_exception(task_moved()); } task->run(boost::move(args)...); } void make_ready_at_thread_exit(ArgTypes... args) { if(!task) { boost::throw_exception(task_moved()); } if (task->has_value()) { boost::throw_exception(promise_already_satisfied()); } task->apply(boost::move(args)...); } #else void operator()() { if(!task) { boost::throw_exception(task_moved()); } task->run(); } void make_ready_at_thread_exit() { if(!task) { boost::throw_exception(task_moved()); } if (task->has_value()) boost::throw_exception(promise_already_satisfied()); task->apply(); } #endif template void set_wait_callback(F f) { task->set_wait_callback(f,this); } }; } #if defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS namespace boost { namespace container { template struct uses_allocator< ::boost::packaged_task , Alloc> : true_type {}; }} #if ! defined BOOST_NO_CXX11_ALLOCATOR namespace std { template struct uses_allocator< ::boost::packaged_task , Alloc> : true_type {}; } #endif #endif namespace boost { BOOST_THREAD_DCL_MOVABLE_BEG(T) packaged_task BOOST_THREAD_DCL_MOVABLE_END namespace detail { //////////////////////////////// // make_future_deferred_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_future_deferred_shared_state(BOOST_THREAD_FWD_REF(Fp) f) { shared_ptr > h(new future_deferred_shared_state(boost::forward(f))); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_future_async_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_future_async_shared_state(BOOST_THREAD_FWD_REF(Fp) f) { shared_ptr > h(new future_async_shared_state()); h->init(boost::forward(f)); return BOOST_THREAD_FUTURE(h); } } //////////////////////////////// // template // future async(launch policy, F&&, ArgTypes&&...); //////////////////////////////// #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE async(launch policy, R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) { typedef R(*F)(BOOST_THREAD_FWD_REF(ArgTypes)...); typedef detail::invoker::type, typename decay::type...> BF; typedef typename BF::result_type Rp; if (underlying_cast(policy) & int(launch::async)) { return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_async_shared_state( BF( f , thread_detail::decay_copy(boost::forward(args))... ) )); } else if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_deferred_shared_state( BF( f , thread_detail::decay_copy(boost::forward(args))... ) )); } else { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); } } #else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE async(launch policy, R(*f)()) { #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK typedef packaged_task packaged_task_type; #else typedef packaged_task packaged_task_type; #endif if (underlying_cast(policy) & int(launch::async)) { packaged_task_type pt( f ); BOOST_THREAD_FUTURE ret = BOOST_THREAD_MAKE_RV_REF(pt.get_future()); ret.set_async(); boost::thread( boost::move(pt) ).detach(); return ::boost::move(ret); } else if (underlying_cast(policy) & int(launch::deferred)) { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); } else { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); } } #endif #endif // defined(BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR) #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE::type( typename decay::type... )>::type> async(launch policy, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) { typedef detail::invoker::type, typename decay::type...> BF; typedef typename BF::result_type Rp; if (underlying_cast(policy) & int(launch::async)) { return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_async_shared_state( BF( thread_detail::decay_copy(boost::forward(f)) , thread_detail::decay_copy(boost::forward(args))... ) )); } else if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_deferred_shared_state( BF( thread_detail::decay_copy(boost::forward(f)) , thread_detail::decay_copy(boost::forward(args))... ) )); } else { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); } } #else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE::type()>::type> async(launch policy, BOOST_THREAD_FWD_REF(F) f) { typedef typename boost::result_of::type()>::type R; #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK typedef packaged_task packaged_task_type; #else // defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK typedef packaged_task packaged_task_type; #endif // defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK if (underlying_cast(policy) & int(launch::async)) { packaged_task_type pt( boost::forward(f) ); BOOST_THREAD_FUTURE ret = pt.get_future(); ret.set_async(); boost::thread( boost::move(pt) ).detach(); return ::boost::move(ret); } else if (underlying_cast(policy) & int(launch::deferred)) { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); // return boost::detail::make_future_deferred_shared_state( // BF( // thread_detail::decay_copy(boost::forward(f)) // ) // ); } else { std::terminate(); //BOOST_THREAD_FUTURE ret; //return ::boost::move(ret); } } #endif // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) #ifdef BOOST_THREAD_PROVIDES_EXECUTORS namespace detail { ///////////////////////// /// shared_state_nullary_task ///////////////////////// template struct shared_state_nullary_task { typedef shared_ptr storage_type; storage_type that; Fp f_; public: shared_state_nullary_task(storage_type st, BOOST_THREAD_FWD_REF(Fp) f) : that(st), f_(boost::move(f)) {}; #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_state_nullary_task) shared_state_nullary_task(shared_state_nullary_task const& x) //BOOST_NOEXCEPT : that(x.that), f_(x.f_) {} shared_state_nullary_task& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT { if (this != &x) { that=x.that; f_=x.f_; } return *this; } // move shared_state_nullary_task(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT : that(x.that), f_(boost::move(x.f_)) { x.that.reset(); } shared_state_nullary_task& operator=(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT { if (this != &x) { that=x.that; f_=boost::move(x.f_); x.that.reset(); } return *this; } #endif void operator()() { shared_ptr > that_ = static_pointer_cast >(that); try { that_->mark_finished_with_result(f_()); } catch(...) { that_->mark_exceptional_finish(); } } ~shared_state_nullary_task() { } }; template struct shared_state_nullary_task { typedef shared_ptr storage_type; storage_type that; Fp f_; public: shared_state_nullary_task(storage_type st, BOOST_THREAD_FWD_REF(Fp) f) : that(st), f_(boost::move(f)) {}; #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) BOOST_THREAD_COPYABLE_AND_MOVABLE(shared_state_nullary_task) shared_state_nullary_task(shared_state_nullary_task const& x) //BOOST_NOEXCEPT : that(x.that), f_(x.f_) {} shared_state_nullary_task& operator=(BOOST_THREAD_COPY_ASSIGN_REF(shared_state_nullary_task) x) //BOOST_NOEXCEPT { if (this != &x) { that=x.that; f_=x.f_; } return *this; } // move shared_state_nullary_task(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) BOOST_NOEXCEPT : that(x.that), f_(boost::move(x.f_)) { x.that.reset(); } shared_state_nullary_task& operator=(BOOST_THREAD_RV_REF(shared_state_nullary_task) x) BOOST_NOEXCEPT { if (this != &x) { that=x.that; f_=boost::move(x.f_); x.that.reset(); } return *this; } #endif void operator()() { shared_ptr > that_ = static_pointer_cast >(that); try { f_(); that_->mark_finished_with_result(); } catch(...) { that_->mark_exceptional_finish(); } } }; } BOOST_THREAD_DCL_MOVABLE_BEG2(R,F) detail::shared_state_nullary_task BOOST_THREAD_DCL_MOVABLE_END namespace detail { ///////////////////////// /// future_executor_shared_state_base ///////////////////////// template struct future_executor_shared_state: shared_state { typedef shared_state base_type; protected: public: future_executor_shared_state() { } template void init(Executor& ex, 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)); } ~future_executor_shared_state() {} }; //////////////////////////////// // make_future_executor_shared_state //////////////////////////////// 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)); return BOOST_THREAD_FUTURE(h); } } // detail //////////////////////////////// // template // future async(Executor& ex, F&&, ArgTypes&&...); //////////////////////////////// //#if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if defined(BOOST_THREAD_PROVIDES_INVOKE) && ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && ! defined(BOOST_NO_CXX11_HDR_TUPLE) #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR template BOOST_THREAD_FUTURE async(Executor& ex, R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) { typedef R(*F)(BOOST_THREAD_FWD_REF(ArgTypes)...); typedef detail::invoker::type, typename decay::type...> BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( f , thread_detail::decay_copy(boost::forward(args))... ) )); } #endif // defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR template BOOST_THREAD_FUTURE::type( typename decay::type... )>::type> async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) { typedef detail::invoker::type, typename decay::type...> BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( thread_detail::decay_copy(boost::forward(f)) , thread_detail::decay_copy(boost::forward(args))... ) )); } #else // ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR template BOOST_THREAD_FUTURE async(Executor& ex, R(*f)()) { typedef R(*F)(); typedef detail::invoker BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( f ) )); } template BOOST_THREAD_FUTURE async(Executor& ex, R(*f)(BOOST_THREAD_FWD_REF(A1)), BOOST_THREAD_FWD_REF(A1) a1) { typedef R(*F)(BOOST_THREAD_FWD_REF(A1)); typedef detail::invoker::type> BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( f , thread_detail::decay_copy(boost::forward(a1)) ) )); } #endif // defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR template BOOST_THREAD_FUTURE::type()>::type> async(Executor& ex, BOOST_THREAD_FWD_REF(F) f) { typedef detail::invoker::type> BF; typedef typename BF::result_type Rp; return boost::detail::make_future_executor_shared_state(ex, BF( thread_detail::decay_copy(boost::forward(f)) ) ); } template BOOST_THREAD_FUTURE::type( typename decay::type )>::type> async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1) { typedef detail::invoker::type, typename decay::type> BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( thread_detail::decay_copy(boost::forward(f)) , thread_detail::decay_copy(boost::forward(a1)) ) )); } template BOOST_THREAD_FUTURE::type( typename decay::type, typename decay::type )>::type> async(Executor& ex, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(A1) a1, BOOST_THREAD_FWD_REF(A2) a2) { typedef detail::invoker::type, typename decay::type, typename decay::type> BF; typedef typename BF::result_type Rp; return BOOST_THREAD_MAKE_RV_REF(boost::detail::make_future_executor_shared_state(ex, BF( thread_detail::decay_copy(boost::forward(f)) , thread_detail::decay_copy(boost::forward(a1)) , thread_detail::decay_copy(boost::forward(a2)) ) )); } #endif //! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #endif //////////////////////////////// // template // future async(F&&, ArgTypes&&...); //////////////////////////////// #if defined BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE async(R(*f)(BOOST_THREAD_FWD_REF(ArgTypes)...), BOOST_THREAD_FWD_REF(ArgTypes)... args) { return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f, boost::forward(args)...)); } #else template BOOST_THREAD_FUTURE async(R(*f)()) { return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), f)); } #endif #endif #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) template BOOST_THREAD_FUTURE::type( typename decay::type... )>::type> async(BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(ArgTypes)... args) { return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward(f), boost::forward(args)...)); } #else template BOOST_THREAD_FUTURE::type> async(BOOST_THREAD_FWD_REF(F) f) { return BOOST_THREAD_MAKE_RV_REF(async(launch(launch::any), boost::forward(f))); } #endif //////////////////////////////// // make_future deprecated //////////////////////////////// template BOOST_THREAD_FUTURE::type> make_future(BOOST_THREAD_FWD_REF(T) value) { typedef typename decay::type future_value_type; promise p; p.set_value(boost::forward(value)); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } #if defined BOOST_THREAD_USES_MOVE inline BOOST_THREAD_FUTURE make_future() { promise p; p.set_value(); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } #endif //////////////////////////////// // make_ready_future //////////////////////////////// namespace detail { template struct deduced_type_impl { typedef T type; }; template struct deduced_type_impl const> { typedef T& type; }; template struct deduced_type_impl > { typedef T& type; }; #if __cplusplus > 201103L template struct deduced_type_impl > { typedef T& type; }; #endif template struct deduced_type { typedef typename detail::deduced_type_impl::type>::type type; }; } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template #else template #endif BOOST_THREAD_FUTURE::type> make_ready_future(BOOST_THREAD_FWD_REF(T) value) { typedef typename detail::deduced_type::type future_value_type; promise p; p.set_value(boost::forward(value)); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } // explicit overloads template BOOST_THREAD_FUTURE make_ready_future(typename remove_reference::type & x) { promise p; p.set_value(x); return p.get_future(); } template BOOST_THREAD_FUTURE make_ready_future(BOOST_THREAD_FWD_REF(typename remove_reference::type) x) { promise p; p.set_value(forward::type>(x)); return p.get_future(); } // variadic overload #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template BOOST_THREAD_FUTURE make_ready_future(Args&&... args) { promise p; p.emplace(forward(args)...); return p.get_future(); } #endif template BOOST_THREAD_FUTURE make_ready_no_decay_future(T1 value) { typedef T future_value_type; promise p; p.set_value(value); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined BOOST_THREAD_USES_MOVE inline BOOST_THREAD_FUTURE make_ready_future() { promise p; p.set_value(); return p.get_future(); } #endif template BOOST_THREAD_FUTURE make_exceptional_future(exception_ptr ex) { promise p; p.set_exception(ex); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } template BOOST_THREAD_FUTURE make_exceptional_future(E ex) { promise p; p.set_exception(boost::copy_exception(ex)); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } template BOOST_THREAD_FUTURE make_exceptional_future() { promise p; p.set_exception(boost::current_exception()); return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } template BOOST_THREAD_FUTURE make_ready_future(exception_ptr ex) { return make_exceptional_future(ex); } #if 0 template make_future(CLOSURE closure) -> BOOST_THREAD_FUTURE { typedef decltype(closure()) T; promise p; try { p.set_value(closure()); } catch(...) { p.set_exception(std::current_exception()); } return BOOST_THREAD_MAKE_RV_REF(p.get_future()); } #endif //////////////////////////////// // make_shared_future deprecated //////////////////////////////// template shared_future::type> make_shared_future(BOOST_THREAD_FWD_REF(T) value) { typedef typename decay::type future_type; promise p; p.set_value(boost::forward(value)); return BOOST_THREAD_MAKE_RV_REF(p.get_future().share()); } inline shared_future make_shared_future() { promise p; return BOOST_THREAD_MAKE_RV_REF(p.get_future().share()); } //////////////////////////////// // detail::future_async_continuation_shared_state //////////////////////////////// #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION namespace detail { ////////////////////// // detail::continuation_shared_state ////////////////////// template > struct continuation_shared_state: ShSt { F parent; Fp continuation; public: continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) : parent(boost::move(f)), continuation(boost::move(c)) { } void init(boost::unique_lock &lock) { parent.future_->set_continuation_ptr(this->shared_from_this(), lock); } void call() { try { this->mark_finished_with_result(this->continuation(boost::move(this->parent))); } catch(...) { this->mark_exceptional_finish(); } // make sure parent is really cleared to prevent memory "leaks" this->parent = F(); } void call(boost::unique_lock& lck) { try { relocker relock(lck); // neither continuation nor parent are protected by the lock - call() must only // be called once, and no one else must modify it. Rp res = this->continuation(boost::move(this->parent)); // make sure parent is really cleared to prevent memory "leaks" this->parent = F(); relock.lock(); this->mark_finished_with_result_internal(boost::move(res), lck); } catch (...) { this->mark_exceptional_finish_internal(current_exception(), lck); // make sure parent is really cleared to prevent memory "leaks" relocker relock(lck); this->parent = F(); } } static void run(shared_ptr that_) { continuation_shared_state* that = static_cast(that_.get()); that->call(); } ~continuation_shared_state() {} }; template struct continuation_shared_state: ShSt { F parent; Fp continuation; public: continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) : parent(boost::move(f)), continuation(boost::move(c)) { } void init(boost::unique_lock &lock) { parent.future_->set_continuation_ptr(this->shared_from_this(), lock); } void call() { try { this->continuation(boost::move(this->parent)); this->mark_finished_with_result(); } catch(...) { this->mark_exceptional_finish(); } // make sure parent is really cleared to prevent memory "leaks" this->parent = F(); } void call(boost::unique_lock& lck) { try { { relocker relock(lck); // neither continuation nor parent are protected by the lock - call() must only // be called once, and no one else must modify it. this->continuation(boost::move(this->parent)); // make sure parent is really cleared to prevent memory "leaks" this->parent = F(); } this->mark_finished_with_result_internal(lck); } catch (...) { this->mark_exceptional_finish_internal(current_exception(), lck); // make sure parent is really cleared to prevent memory "leaks" relocker relock(lck); this->parent = F(); } } static void run(shared_ptr that_) { continuation_shared_state* that = static_cast(that_.get()); that->call(); } ~continuation_shared_state() {} }; ///////////////////////// /// future_async_continuation_shared_state ///////////////////////// template struct future_async_continuation_shared_state: continuation_shared_state > { 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)) { } void launch_continuation() { #if defined BOOST_THREAD_FUTURE_BLOCKING boost::lock_guard lk(this->mutex); this->thr_ = boost::thread(&future_async_continuation_shared_state::run, static_shared_from_this(this)); #else boost::thread(&base_type::run, static_shared_from_this(this)).detach(); #endif } }; ///////////////////////// /// future_sync_continuation_shared_state ///////////////////////// template struct future_sync_continuation_shared_state: continuation_shared_state > { typedef continuation_shared_state > base_type; public: future_sync_continuation_shared_state(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) { } void launch_continuation() { this->call(); } }; ///////////////////////// /// future_executor_continuation_shared_state ///////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS template struct run_it { shared_ptr that_; #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) BOOST_THREAD_COPYABLE_AND_MOVABLE(run_it) run_it(run_it const& x) //BOOST_NOEXCEPT : that_(x.that_) {} run_it& operator=(BOOST_THREAD_COPY_ASSIGN_REF(run_it) x) //BOOST_NOEXCEPT { if (this != &x) { that_=x.that_; } return *this; } // move run_it(BOOST_THREAD_RV_REF(run_it) x) BOOST_NOEXCEPT : that_(x.that_) { x.that_.reset(); } run_it& operator=(BOOST_THREAD_RV_REF(run_it) x) BOOST_NOEXCEPT { if (this != &x) { that_=x.that; x.that_.reset(); } return *this; } #endif run_it(shared_ptr that) : that_ (that) {} void operator()() { that_->run(that_); } }; } BOOST_THREAD_DCL_MOVABLE_BEG(F) detail::run_it BOOST_THREAD_DCL_MOVABLE_END namespace detail { template struct future_executor_continuation_shared_state: continuation_shared_state { 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)) { } template void init(boost::unique_lock &lk, Ex& ex) { 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)); } ~future_executor_continuation_shared_state() {} }; #endif ///////////////////////// /// shared_future_async_continuation_shared_state ///////////////////////// template struct shared_future_async_continuation_shared_state: continuation_shared_state > { typedef continuation_shared_state > base_type; public: shared_future_async_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) { } void launch_continuation() { #if defined BOOST_THREAD_FUTURE_BLOCKING boost::lock_guard lk(this->mutex); this->thr_ = boost::thread(&base_type::run, static_shared_from_this(this)); #else boost::thread(&base_type::run, static_shared_from_this(this)).detach(); #endif } }; ///////////////////////// /// shared_future_async_continuation_shared_state ///////////////////////// template struct shared_future_sync_continuation_shared_state: continuation_shared_state > { typedef continuation_shared_state > base_type; public: shared_future_sync_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) { } void launch_continuation() { this->call(); } }; ///////////////////////// /// shared_future_executor_continuation_shared_state ///////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS template struct shared_future_executor_continuation_shared_state: continuation_shared_state { 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)) { } template void init(boost::unique_lock &lk, Ex& ex) { 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)); } ~shared_future_executor_continuation_shared_state() {} }; #endif ////////////////////////// /// future_deferred_continuation_shared_state ////////////////////////// template struct future_deferred_continuation_shared_state: continuation_shared_state { typedef continuation_shared_state base_type; public: future_deferred_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_deferred(); } virtual void execute(boost::unique_lock& lk) { this->parent.wait(); this->call(lk); } virtual void launch_continuation() { } }; ////////////////////////// /// shared_future_deferred_continuation_shared_state ////////////////////////// template struct shared_future_deferred_continuation_shared_state: continuation_shared_state { typedef continuation_shared_state base_type; public: shared_future_deferred_continuation_shared_state(F f, BOOST_THREAD_FWD_REF(Fp) c) : base_type(boost::move(f), boost::forward(c)) { this->set_deferred(); } virtual void execute(boost::unique_lock& lk) { this->parent.wait(); this->call(lk); } virtual void launch_continuation() { } }; //////////////////////////////// // make_future_deferred_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_future_deferred_continuation_shared_state( 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_deferred_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_future_async_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_future_async_continuation_shared_state( 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_async_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_future_sync_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_future_sync_continuation_shared_state( 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_sync_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_future_executor_continuation_shared_state //////////////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS 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) { typedef typename decay::type Cont; shared_ptr > h(new future_executor_continuation_shared_state(boost::move(f), boost::forward(c))); h->init(lock, ex); return BOOST_THREAD_FUTURE(h); } #endif //////////////////////////////// // make_shared_future_deferred_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_shared_future_deferred_continuation_shared_state( boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { typedef typename decay::type Cont; shared_ptr > h(new shared_future_deferred_continuation_shared_state(f, boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_shared_future_async_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_shared_future_async_continuation_shared_state( boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { typedef typename decay::type Cont; shared_ptr > h(new shared_future_async_continuation_shared_state(f, boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_shared_future_sync_continuation_shared_state //////////////////////////////// template BOOST_THREAD_FUTURE make_shared_future_sync_continuation_shared_state( boost::unique_lock &lock, F f, BOOST_THREAD_FWD_REF(Fp) c) { typedef typename decay::type Cont; shared_ptr > h(new shared_future_sync_continuation_shared_state(f, boost::forward(c))); h->init(lock); return BOOST_THREAD_FUTURE(h); } //////////////////////////////// // make_shared_future_executor_continuation_shared_state //////////////////////////////// #ifdef BOOST_THREAD_PROVIDES_EXECUTORS 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) { typedef typename decay::type Cont; shared_ptr > h(new shared_future_executor_continuation_shared_state(f, boost::forward(c))); h->init(lock, ex); return BOOST_THREAD_FUTURE(h); } #endif } //////////////////////////////// // template // auto future::then(launch policy, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> BOOST_THREAD_FUTURE::then(launch policy, BOOST_THREAD_FWD_REF(F) func) { typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); if (underlying_cast(policy) & int(launch::async)) { 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>( lock, boost::move(*this), boost::forward(func) ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy) & int(launch::executor)) { assert(this->future_->get_executor()); typedef executor Ex; Ex& 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 if (underlying_cast(policy) & int(launch::inherit)) { launch policy_ = this->launch_policy(lock); if (underlying_cast(policy_) & int(launch::async)) { 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>( lock, boost::move(*this), boost::forward(func) ))); #ifdef BOOST_THREAD_PROVIDES_EXECUTORS } else if (underlying_cast(policy_) & int(launch::executor)) { assert(this->future_->get_executor()); typedef executor Ex; Ex& 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>( lock, boost::move(*this), boost::forward(func) ))); } } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } } #ifdef BOOST_THREAD_PROVIDES_EXECUTORS //////////////////////////////// // template // auto future >::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> BOOST_THREAD_FUTURE::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) { typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); 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 //////////////////////////////// // template // auto future >::then(F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> BOOST_THREAD_FUTURE::then(BOOST_THREAD_FWD_REF(F) func) { #ifndef BOOST_THREAD_CONTINUATION_SYNC return this->then(this->launch_policy(), boost::forward(func)); #else typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); launch policy = this->launch_policy(lock); if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } #endif } //////////////////////////////// // template // auto future >::then(launch, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE >)>::type> BOOST_THREAD_FUTURE >::then(launch policy, BOOST_THREAD_FWD_REF(F) func) { typedef BOOST_THREAD_FUTURE R; typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); if (underlying_cast(policy) & int(launch::async)) { 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>( lock, boost::move(*this), boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::sync)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_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)) { assert(this->future_->get_executor()); typedef executor Ex; Ex& 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 if (underlying_cast(policy) & int(launch::inherit)) { launch policy_ = this->launch_policy(lock); if (underlying_cast(policy_) & int(launch::async)) { 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>( lock, boost::move(*this), boost::forward(func) ))); } else if (underlying_cast(policy_) & int(launch::sync)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_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)) { assert(this->future_->get_executor()); typedef executor Ex; Ex& 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>( lock, boost::move(*this), boost::forward(func) ))); } } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_async_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } } #ifdef BOOST_THREAD_PROVIDES_EXECUTORS //////////////////////////////// // template // auto future >::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE >)>::type> BOOST_THREAD_FUTURE >::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) { typedef BOOST_THREAD_FUTURE R; typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); 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 //////////////////////////////// // template // auto future >::then(F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE >)>::type> BOOST_THREAD_FUTURE >::then(BOOST_THREAD_FWD_REF(F) func) { #ifndef BOOST_THREAD_CONTINUATION_SYNC return this->then(this->launch_policy(), boost::forward(func)); #else typedef BOOST_THREAD_FUTURE R; typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); launch policy = this->launch_policy(lock); if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_deferred_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_future_sync_continuation_shared_state, future_type>( lock, boost::move(*this), boost::forward(func) ))); } #endif } //////////////////////////////// // template // auto shared_future::then(launch policy, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> shared_future::then(launch policy, BOOST_THREAD_FWD_REF(F) func) const { typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); 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>( lock, *this, boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } else if (underlying_cast(policy) & int(launch::sync)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_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()); 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 if (underlying_cast(policy) & int(launch::inherit)) { launch policy_ = this->launch_policy(lock); if (underlying_cast(policy_) & int(launch::async)) { 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)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } else if (underlying_cast(policy_) & int(launch::sync)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_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()); 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>( lock, *this, boost::forward(func) ))); } } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_async_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } } #ifdef BOOST_THREAD_PROVIDES_EXECUTORS //////////////////////////////// // template // auto shared_future::then(Ex&, F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> shared_future::then(Ex& ex, BOOST_THREAD_FWD_REF(F) func) const { typedef typename boost::result_of)>::type future_type; 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>(ex, lock, *this, boost::forward(func) ))); } #endif //////////////////////////////// // template // auto shared_future::then(F&& func) -> BOOST_THREAD_FUTURE; //////////////////////////////// template template inline BOOST_THREAD_FUTURE)>::type> shared_future::then(BOOST_THREAD_FWD_REF(F) func) const { #ifndef BOOST_THREAD_CONTINUATION_SYNC return this->then(this->launch_policy(), boost::forward(func)); #else typedef typename boost::result_of)>::type future_type; BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); boost::unique_lock lock(this->future_->mutex); launch policy = this->launch_policy(lock); if (underlying_cast(policy) & int(launch::deferred)) { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_deferred_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } else { return BOOST_THREAD_MAKE_RV_REF((boost::detail::make_shared_future_sync_continuation_shared_state, future_type>( lock, *this, boost::forward(func) ))); } #endif } namespace detail { template struct mfallbacker_to { T value_; typedef T result_type; mfallbacker_to(BOOST_THREAD_RV_REF(T) v) : value_(boost::move(v)) {} T operator()(BOOST_THREAD_FUTURE fut) { return fut.get_or(boost::move(value_)); } }; template struct cfallbacker_to { T value_; typedef T result_type; cfallbacker_to(T const& v) : value_(v) {} T operator()(BOOST_THREAD_FUTURE fut) const { return fut.get_or(value_); } }; } //////////////////////////////// // future future::fallback_to(R&& v); //////////////////////////////// template template inline typename boost::disable_if< is_void, BOOST_THREAD_FUTURE >::type BOOST_THREAD_FUTURE::fallback_to(BOOST_THREAD_RV_REF(R2) v) { return then(detail::mfallbacker_to(boost::move(v))); } template template inline typename boost::disable_if< is_void, BOOST_THREAD_FUTURE >::type BOOST_THREAD_FUTURE::fallback_to(R2 const& v) { return then(detail::cfallbacker_to(v)); } #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP namespace detail { ///////////////////////// /// future_unwrap_shared_state ///////////////////////// template struct future_unwrap_shared_state: shared_state { F wrapped; typename F::value_type unwrapped; public: explicit future_unwrap_shared_state(BOOST_THREAD_RV_REF(F) f) : wrapped(boost::move(f)) { } void launch_continuation() { boost::unique_lock lk(this->mutex); // assert(wrapped.is_ready()); if (! unwrapped.valid() ) { if (wrapped.has_exception()) { this->mark_exceptional_finish_internal(wrapped.get_exception_ptr(), lk); } else { unwrapped = wrapped.get(); if (unwrapped.valid()) { lk.unlock(); boost::unique_lock lk2(unwrapped.future_->mutex); unwrapped.future_->set_continuation_ptr(this->shared_from_this(), lk2); } else { this->mark_exceptional_finish_internal(boost::copy_exception(future_uninitialized()), lk); } } } else { // assert(unwrapped.is_ready()); if (unwrapped.has_exception()) { this->mark_exceptional_finish_internal(unwrapped.get_exception_ptr(), lk); } else { this->mark_finished_with_result_internal(unwrapped.get(), lk); } } } }; template struct future_unwrap_shared_state: shared_state { F wrapped; typename F::value_type unwrapped; public: explicit future_unwrap_shared_state(BOOST_THREAD_RV_REF(F) f) : wrapped(boost::move(f)) { } void launch_continuation() { boost::unique_lock lk(this->mutex); // assert(wrapped.is_ready()); if (! unwrapped.valid() ) { if (wrapped.has_exception()) { this->mark_exceptional_finish_internal(wrapped.get_exception_ptr(), lk); } else { unwrapped = wrapped.get(); if (unwrapped.valid()) { lk.unlock(); boost::unique_lock lk2(unwrapped.future_->mutex); unwrapped.future_->set_continuation_ptr(this->shared_from_this(), lk2); } else { this->mark_exceptional_finish_internal(boost::copy_exception(future_uninitialized()), lk); } } } else { // assert(unwrapped.is_ready()); if (unwrapped.has_exception()) { this->mark_exceptional_finish_internal(unwrapped.get_exception_ptr(), lk); } else { this->mark_finished_with_result_internal(lk); } } } }; template BOOST_THREAD_FUTURE make_future_unwrap_shared_state(boost::unique_lock &lock, BOOST_THREAD_RV_REF(F) f) { shared_ptr > h(new future_unwrap_shared_state(boost::move(f))); h->wrapped.future_->set_continuation_ptr(h, lock); return BOOST_THREAD_FUTURE(h); } } template inline BOOST_THREAD_FUTURE::BOOST_THREAD_FUTURE(BOOST_THREAD_RV_REF(BOOST_THREAD_FUTURE >) other) : base_type(other.unwrap()) {} template BOOST_THREAD_FUTURE BOOST_THREAD_FUTURE >::unwrap() { BOOST_THREAD_ASSERT_PRECONDITION(this->future_!=0, future_uninitialized()); // keep state alive as we move ourself but hold the lock shared_ptr sentinel(this->future_); boost::unique_lock lock(sentinel->mutex); return boost::detail::make_future_unwrap_shared_state >, R2>(lock, boost::move(*this)); } #endif #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY namespace detail { struct input_iterator_tag {}; struct vector_tag {}; struct values_tag {}; template struct alias_t { typedef T type; }; BOOST_CONSTEXPR_OR_CONST input_iterator_tag input_iterator_tag_value = {}; BOOST_CONSTEXPR_OR_CONST vector_tag vector_tag_value = {}; BOOST_CONSTEXPR_OR_CONST values_tag values_tag_value = {}; //////////////////////////////// // detail::future_async_when_all_shared_state //////////////////////////////// template struct future_when_all_vector_shared_state: future_async_shared_state_base > { typedef csbl::vector vector_type; typedef typename F::value_type value_type; vector_type vec_; static void run(shared_ptr that_) { future_when_all_vector_shared_state* that = static_cast(that_.get()); try { boost::wait_for_all(that->vec_.begin(), that->vec_.end()); that->mark_finished_with_result(boost::move(that->vec_)); } catch(...) { that->mark_exceptional_finish(); } } bool run_deferred() { bool res = false; for (typename csbl::vector::iterator it = vec_.begin(); it != vec_.end(); ++it) { if (! it->run_if_is_deferred()) { res = true; } } return res; } void init() { if (! run_deferred()) { future_when_all_vector_shared_state::run(this->shared_from_this()); return; } #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_when_all_vector_shared_state::run, this->shared_from_this()); #else boost::thread(&future_when_all_vector_shared_state::run, this->shared_from_this()).detach(); #endif } public: template< typename InputIterator> future_when_all_vector_shared_state(input_iterator_tag, InputIterator first, InputIterator last) : vec_(std::make_move_iterator(first), std::make_move_iterator(last)) { } future_when_all_vector_shared_state(vector_tag, BOOST_THREAD_RV_REF(csbl::vector) v) : vec_(boost::move(v)) { } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> future_when_all_vector_shared_state(values_tag, BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) { vec_.push_back(boost::forward(f)); typename alias_t::type{ ( //first part of magic unpacker vec_.push_back(boost::forward(futures)),'0' )..., '0' }; //second part of magic unpacker } #endif ~future_when_all_vector_shared_state() {} }; //////////////////////////////// // detail::future_async_when_any_shared_state //////////////////////////////// template struct future_when_any_vector_shared_state: future_async_shared_state_base > { typedef csbl::vector vector_type; typedef typename F::value_type value_type; vector_type vec_; static void run(shared_ptr that_) { future_when_any_vector_shared_state* that = static_cast(that_.get()); try { boost::wait_for_any(that->vec_.begin(), that->vec_.end()); that->mark_finished_with_result(boost::move(that->vec_)); } catch(...) { that->mark_exceptional_finish(); } } bool run_deferred() { for (typename csbl::vector::iterator it = vec_.begin(); it != vec_.end(); ++it) { if (it->run_if_is_deferred_or_ready()) { return true; } } return false; } void init() { if (run_deferred()) { future_when_any_vector_shared_state::run(this->shared_from_this()); return; } #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_when_any_vector_shared_state::run, this->shared_from_this()); #else boost::thread(&future_when_any_vector_shared_state::run, this->shared_from_this()).detach(); #endif } public: template< typename InputIterator> future_when_any_vector_shared_state(input_iterator_tag, InputIterator first, InputIterator last) : vec_(std::make_move_iterator(first), std::make_move_iterator(last)) { } future_when_any_vector_shared_state(vector_tag, BOOST_THREAD_RV_REF(csbl::vector) v) : vec_(boost::move(v)) { } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> future_when_any_vector_shared_state(values_tag, BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures ) { vec_.push_back(boost::forward(f)); typename alias_t::type{ ( //first part of magic unpacker vec_.push_back(boost::forward(futures)) ,'0' )..., '0' }; //second part of magic unpacker } #endif ~future_when_any_vector_shared_state() {} }; #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) struct wait_for_all_fctr { template void operator()(T&&... v) { boost::wait_for_all(boost::forward(v)...); } }; struct wait_for_any_fctr { template void operator()(T&&... v) { boost::wait_for_any(boost::forward(v)...); } }; template ::value> struct accumulate_run_if_is_deferred { bool operator ()(Tuple& t) { return (! csbl::get(t).run_if_is_deferred()) || accumulate_run_if_is_deferred()(t); } }; template struct accumulate_run_if_is_deferred { bool operator ()(Tuple& ) { return false; } }; template< typename Tuple, typename T0, typename ...T> struct future_when_all_tuple_shared_state: future_async_shared_state_base { Tuple tup_; typedef typename make_tuple_indices<1+sizeof...(T)>::type Index; static void run(shared_ptr that_) { future_when_all_tuple_shared_state* that = static_cast(that_.get()); try { // TODO make use of apply(that->tup_, boost::detail::wait_for_all_fctor()); that->wait_for_all(Index()); that->mark_finished_with_result(boost::move(that->tup_)); } catch(...) { that->mark_exceptional_finish(); } } template void wait_for_all(tuple_indices) { #if defined BOOST_THREAD_PROVIDES_INVOKE return invoke(wait_for_all_fctr(), csbl::get(tup_)...); #else return wait_for_all_fctr()(csbl::get(tup_)...); #endif } bool run_deferred() { return accumulate_run_if_is_deferred()(tup_); } void init() { if (! run_deferred()) { future_when_all_tuple_shared_state::run(this->shared_from_this()); return; } #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_when_all_tuple_shared_state::run, this->shared_from_this()); #else boost::thread(&future_when_all_tuple_shared_state::run, this->shared_from_this()).detach(); #endif } public: template< typename F, typename ...Fs> future_when_all_tuple_shared_state(values_tag, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Fs) ... futures) : tup_(boost::csbl::make_tuple(boost::forward(f), boost::forward(futures)...)) { } ~future_when_all_tuple_shared_state() {} }; template ::value> struct apply_any_run_if_is_deferred_or_ready { bool operator ()(Tuple& t) { if (csbl::get(t).run_if_is_deferred_or_ready()) return true; return apply_any_run_if_is_deferred_or_ready()(t); } }; template struct apply_any_run_if_is_deferred_or_ready { bool operator ()(Tuple& ) { return false; } }; template< typename Tuple, typename T0, typename ...T > struct future_when_any_tuple_shared_state: future_async_shared_state_base { Tuple tup_; typedef typename make_tuple_indices<1+sizeof...(T)>::type Index; static void run(shared_ptr that_) { future_when_any_tuple_shared_state* that = static_cast(that_.get()); try { // TODO make use of apply(that->tup_, wait_for_any_fctr); that->wait_for_any(Index()); that->mark_finished_with_result(boost::move(that->tup_)); } catch(...) { that->mark_exceptional_finish(); } } template void wait_for_any(tuple_indices) { #if defined BOOST_THREAD_PROVIDES_INVOKE return invoke(wait_for_any_fctr(), csbl::get(tup_)...); #else return wait_for_any_fctr()(csbl::get(tup_)...); #endif } bool run_deferred() { return apply_any_run_if_is_deferred_or_ready()(tup_); } void init() { if (run_deferred()) { future_when_any_tuple_shared_state::run(this->shared_from_this()); return; } #ifdef BOOST_THREAD_FUTURE_BLOCKING this->thr_ = boost::thread(&future_when_any_tuple_shared_state::run, this->shared_from_this()); #else boost::thread(&future_when_any_tuple_shared_state::run, this->shared_from_this()).detach(); #endif } public: template< typename F, typename ...Fs> future_when_any_tuple_shared_state(values_tag, BOOST_THREAD_FWD_REF(F) f, BOOST_THREAD_FWD_REF(Fs) ... futures ) : tup_(boost::csbl::make_tuple(boost::forward(f), boost::forward(futures)...)) { } ~future_when_any_tuple_shared_state() {} }; #endif } template< typename InputIterator> typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_all(InputIterator first, InputIterator last) { typedef typename InputIterator::value_type value_type; typedef csbl::vector container_type; typedef detail::future_when_all_vector_shared_state factory_type; if (first==last) return make_ready_future(container_type()); shared_ptr h(new factory_type(detail::input_iterator_tag_value, first,last)); h->init(); return BOOST_THREAD_FUTURE(h); } inline BOOST_THREAD_FUTURE > when_all() { return make_ready_future(csbl::tuple<>()); } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> BOOST_THREAD_FUTURE::type, typename decay::type...> > when_all(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) { typedef csbl::tuple::type, typename decay::type...> container_type; typedef detail::future_when_all_tuple_shared_state::type, typename decay::type...> factory_type; shared_ptr h(new factory_type(detail::values_tag_value, boost::forward(f), boost::forward(futures)...)); h->init(); return BOOST_THREAD_FUTURE(h); } #endif template< typename InputIterator> typename boost::disable_if, BOOST_THREAD_FUTURE > >::type when_any(InputIterator first, InputIterator last) { typedef typename InputIterator::value_type value_type; typedef csbl::vector container_type; typedef detail::future_when_any_vector_shared_state factory_type; if (first==last) return make_ready_future(container_type()); shared_ptr h(new factory_type(detail::input_iterator_tag_value, first,last)); h->init(); return BOOST_THREAD_FUTURE(h); } inline BOOST_THREAD_FUTURE > when_any() { return make_ready_future(csbl::tuple<>()); } #if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template< typename T0, typename ...T> BOOST_THREAD_FUTURE::type, typename decay::type...> > when_any(BOOST_THREAD_FWD_REF(T0) f, BOOST_THREAD_FWD_REF(T) ... futures) { typedef csbl::tuple::type, typename decay::type...> container_type; typedef detail::future_when_any_tuple_shared_state::type, typename decay::type...> factory_type; shared_ptr h(new factory_type(detail::values_tag_value, boost::forward(f), boost::forward(futures)...)); h->init(); return BOOST_THREAD_FUTURE(h); } #endif #endif // BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY } #endif // BOOST_NO_EXCEPTIONS #endif // header