1 #ifndef BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
2 #define BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
4 //////////////////////////////////////////////////////////////////////////////
6 // (C) Copyright Vicente J. Botet Escriba 2014-2015. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/thread for documentation.
12 //////////////////////////////////////////////////////////////////////////////
13 #include <boost/thread/detail/config.hpp>
15 #include <boost/thread/future.hpp>
16 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
17 #include <boost/thread/executors/basic_thread_pool.hpp>
19 #include <boost/thread/experimental/exception_list.hpp>
20 #include <boost/thread/experimental/parallel/v2/inline_namespace.hpp>
21 #include <boost/thread/detail/move.hpp>
23 #include <boost/config/abi_prefix.hpp>
25 #define BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
29 namespace experimental
33 BOOST_THREAD_INLINE_NAMESPACE(v2)
35 class BOOST_SYMBOL_VISIBLE task_canceled_exception: public std::exception
38 //task_canceled_exception() BOOST_NOEXCEPT {}
39 //task_canceled_exception(const task_canceled_exception&) BOOST_NOEXCEPT {}
40 //task_canceled_exception& operator=(const task_canceled_exception&) BOOST_NOEXCEPT {}
41 virtual const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
42 { return "task_canceled_exception";}
45 template <class Executor>
46 class task_region_handle_gen;
50 void handle_task_region_exceptions(exception_list& errors)
55 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
56 catch (task_canceled_exception&)
60 catch (exception_list const& el)
62 for (exception_list::const_iterator it = el.begin(); it != el.end(); ++it)
64 boost::exception_ptr const& e = *it;
70 handle_task_region_exceptions(errors);
76 errors.add(boost::current_exception());
80 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
81 template <class TRH, class F>
86 wrapped(TRH& tr, BOOST_THREAD_RV_REF(F) f) : tr(tr), f(move(f))
96 lock_guard<mutex> lk(tr.mtx);
105 template <class Executor>
106 class task_region_handle_gen
109 // Private members and friends
110 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
111 template <class TRH, class F>
112 friend struct detail::wrapped;
114 template <typename F>
115 friend void task_region(BOOST_THREAD_FWD_REF(F) f);
117 friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
118 template <class Ex, typename F>
119 friend void task_region(Ex&, BOOST_THREAD_FWD_REF(F) f);
120 template<class Ex, typename F>
121 friend void task_region_final(Ex&, BOOST_THREAD_FWD_REF(F) f);
125 wait_for_all(group.begin(), group.end());
127 for (group_type::iterator it = group.begin(); it != group.end(); ++it)
129 future<void>& f = *it;
130 if (f.has_exception())
134 boost::rethrow_exception(f.get_exception_ptr());
138 detail::handle_task_region_exceptions(exs);
144 boost::throw_exception(exs);
148 #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
149 task_region_handle_gen()
153 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
154 task_region_handle_gen()
158 task_region_handle_gen(Executor& ex)
165 #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
166 task_region_handle_gen()
169 task_region_handle_gen(Executor& ex)
174 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
175 task_region_handle_gen()
181 ~task_region_handle_gen()
186 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
190 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
194 typedef csbl::vector<future<void> > group_type;
198 BOOST_DELETED_FUNCTION(task_region_handle_gen(const task_region_handle_gen&))
199 BOOST_DELETED_FUNCTION(task_region_handle_gen& operator=(const task_region_handle_gen&))
200 BOOST_DELETED_FUNCTION(task_region_handle_gen* operator&() const)
204 void run(BOOST_THREAD_FWD_REF(F) f)
206 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
208 lock_guard<mutex> lk(mtx);
210 boost::throw_exception(task_canceled_exception());
213 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
214 group.push_back(async(*ex, detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
216 group.push_back(async(detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
219 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
220 group.push_back(async(*ex, forward<F>(f)));
222 group.push_back(async(forward<F>(f)));
229 #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
231 lock_guard<mutex> lk(mtx);
233 boost::throw_exception(task_canceled_exception());
240 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
241 typedef basic_thread_pool default_executor;
243 typedef int default_executor;
245 class task_region_handle :
246 public task_region_handle_gen<default_executor>
249 template <typename F>
250 friend void task_region(BOOST_THREAD_FWD_REF(F) f);
252 friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
255 task_region_handle() : task_region_handle_gen<default_executor>()
257 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
261 BOOST_DELETED_FUNCTION(task_region_handle(const task_region_handle&))
262 BOOST_DELETED_FUNCTION(task_region_handle& operator=(const task_region_handle&))
263 BOOST_DELETED_FUNCTION(task_region_handle* operator&() const)
267 template <typename Executor, typename F>
268 void task_region_final(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
270 task_region_handle_gen<Executor> tr(ex);
277 detail::handle_task_region_exceptions(tr.exs);
282 template <typename Executor, typename F>
283 void task_region(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
285 task_region_final(ex, forward<F>(f));
288 template <typename F>
289 void task_region_final(BOOST_THREAD_FWD_REF(F) f)
291 task_region_handle tr;
298 detail::handle_task_region_exceptions(tr.exs);
303 template <typename F>
304 void task_region(BOOST_THREAD_FWD_REF(F) f)
306 task_region_final(forward<F>(f));
314 #include <boost/config/abi_suffix.hpp>