task_region.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
  2. #define BOOST_THREAD_EXPERIMENTAL_PARALLEL_V2_TASK_REGION_HPP
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // (C) Copyright Vicente J. Botet Escriba 2014-2015. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/thread for documentation.
  10. //
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include <boost/thread/detail/config.hpp>
  13. #include <boost/thread/future.hpp>
  14. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  15. #include <boost/thread/executors/basic_thread_pool.hpp>
  16. #endif
  17. #include <boost/thread/experimental/exception_list.hpp>
  18. #include <boost/thread/experimental/parallel/v2/inline_namespace.hpp>
  19. #include <boost/thread/detail/move.hpp>
  20. #include <boost/config/abi_prefix.hpp>
  21. #define BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  22. namespace boost
  23. {
  24. namespace experimental
  25. {
  26. namespace parallel
  27. {
  28. BOOST_THREAD_INLINE_NAMESPACE(v2)
  29. {
  30. class BOOST_SYMBOL_VISIBLE task_canceled_exception: public std::exception
  31. {
  32. public:
  33. //task_canceled_exception() BOOST_NOEXCEPT {}
  34. //task_canceled_exception(const task_canceled_exception&) BOOST_NOEXCEPT {}
  35. //task_canceled_exception& operator=(const task_canceled_exception&) BOOST_NOEXCEPT {}
  36. virtual const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
  37. { return "task_canceled_exception";}
  38. };
  39. template <class Executor>
  40. class task_region_handle_gen;
  41. namespace detail
  42. {
  43. void handle_task_region_exceptions(exception_list& errors)
  44. {
  45. try {
  46. throw;
  47. }
  48. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  49. catch (task_canceled_exception&)
  50. {
  51. }
  52. #endif
  53. catch (exception_list const& el)
  54. {
  55. for (exception_list::const_iterator it = el.begin(); it != el.end(); ++it)
  56. {
  57. boost::exception_ptr const& e = *it;
  58. try {
  59. rethrow_exception(e);
  60. }
  61. catch (...)
  62. {
  63. handle_task_region_exceptions(errors);
  64. }
  65. }
  66. }
  67. catch (...)
  68. {
  69. errors.add(boost::current_exception());
  70. }
  71. }
  72. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  73. template <class TRH, class F>
  74. struct wrapped
  75. {
  76. TRH& tr;
  77. F f;
  78. wrapped(TRH& tr, BOOST_THREAD_RV_REF(F) f) : tr(tr), f(move(f))
  79. {}
  80. void operator()()
  81. {
  82. try
  83. {
  84. f();
  85. }
  86. catch (...)
  87. {
  88. lock_guard<mutex> lk(tr.mtx);
  89. tr.canceled = true;
  90. throw;
  91. }
  92. }
  93. };
  94. #endif
  95. }
  96. template <class Executor>
  97. class task_region_handle_gen
  98. {
  99. private:
  100. // Private members and friends
  101. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  102. template <class TRH, class F>
  103. friend struct detail::wrapped;
  104. #endif
  105. template <typename F>
  106. friend void task_region(BOOST_THREAD_FWD_REF(F) f);
  107. template<typename F>
  108. friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
  109. template <class Ex, typename F>
  110. friend void task_region(Ex&, BOOST_THREAD_FWD_REF(F) f);
  111. template<class Ex, typename F>
  112. friend void task_region_final(Ex&, BOOST_THREAD_FWD_REF(F) f);
  113. void wait_all()
  114. {
  115. wait_for_all(group.begin(), group.end());
  116. for (group_type::iterator it = group.begin(); it != group.end(); ++it)
  117. {
  118. future<void>& f = *it;
  119. if (f.has_exception())
  120. {
  121. try
  122. {
  123. boost::rethrow_exception(f.get_exception_ptr());
  124. }
  125. catch (...)
  126. {
  127. detail::handle_task_region_exceptions(exs);
  128. }
  129. }
  130. }
  131. if (exs.size() != 0)
  132. {
  133. boost::throw_exception(exs);
  134. }
  135. }
  136. protected:
  137. #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
  138. task_region_handle_gen()
  139. {}
  140. #endif
  141. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
  142. task_region_handle_gen()
  143. : canceled(false)
  144. , ex(0)
  145. {}
  146. task_region_handle_gen(Executor& ex)
  147. : canceled(false)
  148. , ex(&ex)
  149. {}
  150. #endif
  151. #if ! defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && defined BOOST_THREAD_PROVIDES_EXECUTORS
  152. task_region_handle_gen()
  153. : ex(0)
  154. {}
  155. task_region_handle_gen(Executor& ex)
  156. : ex(&ex)
  157. {}
  158. #endif
  159. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED && ! defined BOOST_THREAD_PROVIDES_EXECUTORS
  160. task_region_handle_gen()
  161. : canceled(false)
  162. {
  163. }
  164. #endif
  165. ~task_region_handle_gen()
  166. {
  167. //wait_all();
  168. }
  169. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  170. mutable mutex mtx;
  171. bool canceled;
  172. #endif
  173. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  174. Executor* ex;
  175. #endif
  176. exception_list exs;
  177. typedef csbl::vector<future<void> > group_type;
  178. group_type group;
  179. public:
  180. BOOST_DELETED_FUNCTION(task_region_handle_gen(const task_region_handle_gen&))
  181. BOOST_DELETED_FUNCTION(task_region_handle_gen& operator=(const task_region_handle_gen&))
  182. BOOST_DELETED_FUNCTION(task_region_handle_gen* operator&() const)
  183. public:
  184. template<typename F>
  185. void run(BOOST_THREAD_FWD_REF(F) f)
  186. {
  187. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  188. {
  189. lock_guard<mutex> lk(mtx);
  190. if (canceled) {
  191. boost::throw_exception(task_canceled_exception());
  192. }
  193. }
  194. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  195. group.push_back(async(*ex, detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
  196. #else
  197. group.push_back(async(detail::wrapped<task_region_handle_gen<Executor>, F>(*this, forward<F>(f))));
  198. #endif
  199. #else
  200. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  201. group.push_back(async(*ex, forward<F>(f)));
  202. #else
  203. group.push_back(async(forward<F>(f)));
  204. #endif
  205. #endif
  206. }
  207. void wait()
  208. {
  209. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  210. {
  211. lock_guard<mutex> lk(mtx);
  212. if (canceled) {
  213. boost::throw_exception(task_canceled_exception());
  214. }
  215. }
  216. #endif
  217. wait_all();
  218. }
  219. };
  220. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  221. typedef basic_thread_pool default_executor;
  222. #else
  223. typedef int default_executor;
  224. #endif
  225. class task_region_handle :
  226. public task_region_handle_gen<default_executor>
  227. {
  228. default_executor tp;
  229. template <typename F>
  230. friend void task_region(BOOST_THREAD_FWD_REF(F) f);
  231. template<typename F>
  232. friend void task_region_final(BOOST_THREAD_FWD_REF(F) f);
  233. protected:
  234. task_region_handle() : task_region_handle_gen<default_executor>()
  235. {
  236. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  237. ex = &tp;
  238. #endif
  239. }
  240. BOOST_DELETED_FUNCTION(task_region_handle(const task_region_handle&))
  241. BOOST_DELETED_FUNCTION(task_region_handle& operator=(const task_region_handle&))
  242. BOOST_DELETED_FUNCTION(task_region_handle* operator&() const)
  243. };
  244. template <typename Executor, typename F>
  245. void task_region_final(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
  246. {
  247. task_region_handle_gen<Executor> tr(ex);
  248. try
  249. {
  250. f(tr);
  251. }
  252. catch (...)
  253. {
  254. detail::handle_task_region_exceptions(tr.exs);
  255. }
  256. tr.wait_all();
  257. }
  258. template <typename Executor, typename F>
  259. void task_region(Executor& ex, BOOST_THREAD_FWD_REF(F) f)
  260. {
  261. task_region_final(ex, forward<F>(f));
  262. }
  263. template <typename F>
  264. void task_region_final(BOOST_THREAD_FWD_REF(F) f)
  265. {
  266. task_region_handle tr;
  267. try
  268. {
  269. f(tr);
  270. }
  271. catch (...)
  272. {
  273. detail::handle_task_region_exceptions(tr.exs);
  274. }
  275. tr.wait_all();
  276. }
  277. template <typename F>
  278. void task_region(BOOST_THREAD_FWD_REF(F) f)
  279. {
  280. task_region_final(forward<F>(f));
  281. }
  282. } // v2
  283. } // parallel
  284. } // experimental
  285. } // boost
  286. #include <boost/config/abi_suffix.hpp>
  287. #endif // header