123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #ifndef BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
- #define BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
- //////////////////////////////////////////////////////////////////////////////
- //
- // (C) Copyright Vicente J. Botet Escriba 2014-2015. 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)
- //
- // See http://www.boost.org/libs/thread for documentation.
- //
- //////////////////////////////////////////////////////////////////////////////
- #include <exception>
- #include <boost/throw_exception.hpp>
- #include <boost/thread/detail/config.hpp>
- #include <boost/thread/future.hpp>
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- #include <boost/thread/executors/basic_thread_pool.hpp>
- #endif
- #include <boost/thread/experimental/exception_list.hpp>
- #include <boost/thread/experimental/parallel/v2/inline_namespace.hpp>
- #include <boost/thread/csbl/vector.hpp>
- #include <boost/thread/detail/move.hpp>
- #include <boost/config/abi_prefix.hpp>
- #define BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- namespace boost
- {
- namespace experimental
- {
- namespace parallel
- {
- BOOST_THREAD_INLINE_NAMESPACE(v2)
- {
- class BOOST_SYMBOL_VISIBLE task_canceled_exception: public std::exception
- {
- public:
- //task_canceled_exception() BOOST_NOEXCEPT {}
- //task_canceled_exception(const task_canceled_exception&) BOOST_NOEXCEPT {}
- //task_canceled_exception& operator=(const task_canceled_exception&) BOOST_NOEXCEPT {}
- virtual const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
- { return "task_canceled_exception";}
- };
- template <class Executor>
- class task_region_handle_gen;
- namespace detail
- {
- void handle_task_region_exceptions(exception_list& errors)
- {
- try {
- throw;
- }
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- catch (task_canceled_exception&)
- {
- }
- #endif
- catch (exception_list const& el)
- {
- for (exception_list::const_iterator it = el.begin(); it != el.end(); ++it)
- {
- boost::exception_ptr const& e = *it;
- try {
- rethrow_exception(e);
- }
- catch (...)
- {
- handle_task_region_exceptions(errors);
- }
- }
- }
- catch (...)
- {
- errors.add(boost::current_exception());
- }
- }
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- template <class TRH, class F>
- struct wrapped
- {
- TRH& tr;
- F f;
- wrapped(TRH& tr, BOOST_THREAD_RV_REF(F) f) : tr(tr), f(move(f))
- {}
- void operator()()
- {
- try
- {
- f();
- }
- catch (...)
- {
- lock_guard<mutex> lk(tr.mtx);
- tr.canceled = true;
- throw;
- }
- }
- };
- #endif
- }
- template <class Executor>
- class task_region_handle_gen
- {
- private:
- // Private members and friends
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- template <class TRH, class F>
- friend struct detail::wrapped;
- #endif
- template <typename F>
- friend void task_region(BOOST_THREAD_FWD_REF(F) f);
- template<typename F>
- friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
- template <class Ex, typename F>
- friend void task_region(Ex&, BOOST_THREAD_FWD_REF(F) f);
- template<class Ex, typename F>
- friend void task_region_final(Ex&, BOOST_THREAD_FWD_REF(F) f);
- void wait_all()
- {
- wait_for_all(group.begin(), group.end());
- for (group_type::iterator it = group.begin(); it != group.end(); ++it)
- {
- BOOST_THREAD_FUTURE<void>& f = *it;
- if (f.has_exception())
- {
- try
- {
- boost::rethrow_exception(f.get_exception_ptr());
- }
- catch (...)
- {
- detail::handle_task_region_exceptions(exs);
- }
- }
- }
- if (exs.size() != 0)
- {
- boost::throw_exception(exs);
- }
- }
- protected:
- #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
- task_region_handle_gen()
- {}
- #endif
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
- task_region_handle_gen()
- : canceled(false)
- , ex(0)
- {}
- task_region_handle_gen(Executor& ex)
- : canceled(false)
- , ex(&ex)
- {}
- #endif
- #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
- task_region_handle_gen()
- : ex(0)
- {}
- task_region_handle_gen(Executor& ex)
- : ex(&ex)
- {}
- #endif
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
- task_region_handle_gen()
- : canceled(false)
- {
- }
- #endif
- ~task_region_handle_gen()
- {
- //wait_all();
- }
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- mutable mutex mtx;
- bool canceled;
- #endif
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- Executor* ex;
- #endif
- exception_list exs;
- typedef csbl::vector<BOOST_THREAD_FUTURE<void> > group_type;
- group_type group;
- public:
- BOOST_DELETED_FUNCTION(task_region_handle_gen(const task_region_handle_gen&))
- BOOST_DELETED_FUNCTION(task_region_handle_gen& operator=(const task_region_handle_gen&))
- BOOST_DELETED_FUNCTION(task_region_handle_gen* operator&() const)
- public:
- template<typename F>
- void run(BOOST_THREAD_FWD_REF(F) f)
- {
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- {
- lock_guard<mutex> lk(mtx);
- if (canceled) {
- boost::throw_exception(task_canceled_exception());
- }
- }
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- group.push_back(async(*ex, detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
- #else
- group.push_back(async(detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
- #endif
- #else
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- group.push_back(async(*ex, forward<F>(f)));
- #else
- group.push_back(async(forward<F>(f)));
- #endif
- #endif
- }
- void wait()
- {
- #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
- {
- lock_guard<mutex> lk(mtx);
- if (canceled) {
- boost::throw_exception(task_canceled_exception());
- }
- }
- #endif
- wait_all();
- }
- };
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- typedef basic_thread_pool default_executor;
- #else
- typedef int default_executor;
- #endif
- class task_region_handle :
- public task_region_handle_gen<default_executor>
- {
- default_executor tp;
- template <typename F>
- friend void task_region(BOOST_THREAD_FWD_REF(F) f);
- template<typename F>
- friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
- protected:
- task_region_handle() : task_region_handle_gen<default_executor>()
- {
- #if defined BOOST_THREAD_PROVIDES_EXECUTORS
- ex = &tp;
- #endif
- }
- BOOST_DELETED_FUNCTION(task_region_handle(const task_region_handle&))
- BOOST_DELETED_FUNCTION(task_region_handle& operator=(const task_region_handle&))
- BOOST_DELETED_FUNCTION(task_region_handle* operator&() const)
- };
- template <typename Executor, typename F>
- void task_region_final(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
- {
- task_region_handle_gen<Executor> tr(ex);
- try
- {
- f(tr);
- }
- catch (...)
- {
- detail::handle_task_region_exceptions(tr.exs);
- }
- tr.wait_all();
- }
- template <typename Executor, typename F>
- void task_region(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
- {
- task_region_final(ex, forward<F>(f));
- }
- template <typename F>
- void task_region_final(BOOST_THREAD_FWD_REF(F) f)
- {
- task_region_handle tr;
- try
- {
- f(tr);
- }
- catch (...)
- {
- detail::handle_task_region_exceptions(tr.exs);
- }
- tr.wait_all();
- }
- template <typename F>
- void task_region(BOOST_THREAD_FWD_REF(F) f)
- {
- task_region_final(forward<F>(f));
- }
- } // v2
- } // parallel
- } // experimental
- } // boost
- #include <boost/config/abi_suffix.hpp>
- #endif // header
|