task_group.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_task_group_H
  14. #define __TBB_task_group_H
  15. #define __TBB_task_group_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include "task.h"
  18. #include "tbb_exception.h"
  19. #include "internal/_template_helpers.h"
  20. #if TBB_PREVIEW_ISOLATED_TASK_GROUP && __TBB_TASK_ISOLATION
  21. #include "task_arena.h"
  22. #endif
  23. #if __TBB_TASK_GROUP_CONTEXT
  24. namespace tbb {
  25. namespace internal {
  26. template<typename F> class task_handle_task;
  27. }
  28. class task_group;
  29. class structured_task_group;
  30. #if TBB_PREVIEW_ISOLATED_TASK_GROUP && __TBB_TASK_ISOLATION
  31. class isolated_task_group;
  32. #endif
  33. template<typename F>
  34. class task_handle : internal::no_assign {
  35. template<typename _F> friend class internal::task_handle_task;
  36. friend class task_group;
  37. friend class structured_task_group;
  38. #if TBB_PREVIEW_ISOLATED_TASK_GROUP && __TBB_TASK_ISOLATION
  39. friend class isolated_task_group;
  40. #endif
  41. static const intptr_t scheduled = 0x1;
  42. F my_func;
  43. intptr_t my_state;
  44. void mark_scheduled () {
  45. // The check here is intentionally lax to avoid the impact of interlocked operation
  46. if ( my_state & scheduled )
  47. internal::throw_exception( internal::eid_invalid_multiple_scheduling );
  48. my_state |= scheduled;
  49. }
  50. public:
  51. task_handle( const F& f ) : my_func(f), my_state(0) {}
  52. #if __TBB_CPP11_RVALUE_REF_PRESENT
  53. task_handle( F&& f ) : my_func( std::move(f)), my_state(0) {}
  54. #endif
  55. void operator() () const { my_func(); }
  56. };
  57. enum task_group_status {
  58. not_complete,
  59. complete,
  60. canceled
  61. };
  62. namespace internal {
  63. template<typename F>
  64. class task_handle_task : public task {
  65. task_handle<F>& my_handle;
  66. task* execute() __TBB_override {
  67. my_handle();
  68. return NULL;
  69. }
  70. public:
  71. task_handle_task( task_handle<F>& h ) : my_handle(h) { h.mark_scheduled(); }
  72. };
  73. class task_group_base : internal::no_copy {
  74. class ref_count_guard : internal::no_copy {
  75. task& my_task;
  76. public:
  77. ref_count_guard(task& t) : my_task(t) {
  78. my_task.increment_ref_count();
  79. }
  80. ~ref_count_guard() {
  81. my_task.decrement_ref_count();
  82. }
  83. };
  84. protected:
  85. empty_task* my_root;
  86. task_group_context my_context;
  87. template<typename F>
  88. task_group_status internal_run_and_wait( F& f ) {
  89. __TBB_TRY {
  90. if ( !my_context.is_group_execution_cancelled() ) {
  91. // We need to increase the reference count of the root task to notify waiters that
  92. // this task group has some work in progress.
  93. ref_count_guard guard(*my_root);
  94. f();
  95. }
  96. } __TBB_CATCH( ... ) {
  97. my_context.register_pending_exception();
  98. }
  99. return wait();
  100. }
  101. template<typename Task, typename F>
  102. task* prepare_task( __TBB_FORWARDING_REF(F) f ) {
  103. return new( task::allocate_additional_child_of(*my_root) ) Task( internal::forward<F>(f) );
  104. }
  105. public:
  106. task_group_base( uintptr_t traits = 0 )
  107. : my_context(task_group_context::bound, task_group_context::default_traits | traits)
  108. {
  109. my_root = new( task::allocate_root(my_context) ) empty_task;
  110. my_root->set_ref_count(1);
  111. }
  112. ~task_group_base() __TBB_NOEXCEPT(false) {
  113. if( my_root->ref_count() > 1 ) {
  114. #if __TBB_CPP17_UNCAUGHT_EXCEPTIONS_PRESENT
  115. bool stack_unwinding_in_progress = std::uncaught_exceptions() > 0;
  116. #else
  117. bool stack_unwinding_in_progress = std::uncaught_exception();
  118. #endif
  119. // Always attempt to do proper cleanup to avoid inevitable memory corruption
  120. // in case of missing wait (for the sake of better testability & debuggability)
  121. if ( !is_canceling() )
  122. cancel();
  123. __TBB_TRY {
  124. my_root->wait_for_all();
  125. } __TBB_CATCH (...) {
  126. task::destroy(*my_root);
  127. __TBB_RETHROW();
  128. }
  129. task::destroy(*my_root);
  130. if ( !stack_unwinding_in_progress )
  131. internal::throw_exception( internal::eid_missing_wait );
  132. }
  133. else {
  134. task::destroy(*my_root);
  135. }
  136. }
  137. template<typename F>
  138. void run( task_handle<F>& h ) {
  139. task::spawn( *prepare_task< internal::task_handle_task<F> >(h) );
  140. }
  141. task_group_status wait() {
  142. __TBB_TRY {
  143. my_root->wait_for_all();
  144. } __TBB_CATCH( ... ) {
  145. my_context.reset();
  146. __TBB_RETHROW();
  147. }
  148. if ( my_context.is_group_execution_cancelled() ) {
  149. // TODO: the reset method is not thread-safe. Ensure the correct behavior.
  150. my_context.reset();
  151. return canceled;
  152. }
  153. return complete;
  154. }
  155. bool is_canceling() {
  156. return my_context.is_group_execution_cancelled();
  157. }
  158. void cancel() {
  159. my_context.cancel_group_execution();
  160. }
  161. }; // class task_group_base
  162. } // namespace internal
  163. class task_group : public internal::task_group_base {
  164. public:
  165. task_group () : task_group_base( task_group_context::concurrent_wait ) {}
  166. #if __SUNPRO_CC
  167. template<typename F>
  168. void run( task_handle<F>& h ) {
  169. internal_run< internal::task_handle_task<F> >( h );
  170. }
  171. #else
  172. using task_group_base::run;
  173. #endif
  174. #if __TBB_CPP11_RVALUE_REF_PRESENT
  175. template<typename F>
  176. void run( F&& f ) {
  177. task::spawn( *prepare_task< internal::function_task< typename internal::strip<F>::type > >(std::forward<F>(f)) );
  178. }
  179. #else
  180. template<typename F>
  181. void run(const F& f) {
  182. task::spawn( *prepare_task< internal::function_task<F> >(f) );
  183. }
  184. #endif
  185. template<typename F>
  186. task_group_status run_and_wait( const F& f ) {
  187. return internal_run_and_wait<const F>( f );
  188. }
  189. // TODO: add task_handle rvalues support
  190. template<typename F>
  191. task_group_status run_and_wait( task_handle<F>& h ) {
  192. h.mark_scheduled();
  193. return internal_run_and_wait< task_handle<F> >( h );
  194. }
  195. }; // class task_group
  196. class __TBB_DEPRECATED structured_task_group : public internal::task_group_base {
  197. public:
  198. // TODO: add task_handle rvalues support
  199. template<typename F>
  200. task_group_status run_and_wait ( task_handle<F>& h ) {
  201. h.mark_scheduled();
  202. return internal_run_and_wait< task_handle<F> >( h );
  203. }
  204. task_group_status wait() {
  205. task_group_status res = task_group_base::wait();
  206. my_root->set_ref_count(1);
  207. return res;
  208. }
  209. }; // class structured_task_group
  210. #if TBB_PREVIEW_ISOLATED_TASK_GROUP && __TBB_TASK_ISOLATION
  211. namespace internal {
  212. using interface7::internal::delegate_base;
  213. using interface7::internal::isolate_within_arena;
  214. class spawn_delegate : public delegate_base {
  215. task* task_to_spawn;
  216. void operator()() const __TBB_override {
  217. task::spawn(*task_to_spawn);
  218. }
  219. public:
  220. spawn_delegate(task* a_task) : task_to_spawn(a_task) {}
  221. };
  222. class wait_delegate : public delegate_base {
  223. void operator()() const __TBB_override {
  224. status = tg.wait();
  225. }
  226. protected:
  227. task_group& tg;
  228. task_group_status& status;
  229. public:
  230. wait_delegate(task_group& a_group, task_group_status& tgs)
  231. : tg(a_group), status(tgs) {}
  232. };
  233. template<typename F>
  234. class run_wait_delegate : public wait_delegate {
  235. F& func;
  236. void operator()() const __TBB_override {
  237. status = tg.run_and_wait( func );
  238. }
  239. public:
  240. run_wait_delegate(task_group& a_group, F& a_func, task_group_status& tgs)
  241. : wait_delegate(a_group, tgs), func(a_func) {}
  242. };
  243. } // namespace internal
  244. class isolated_task_group : public task_group {
  245. intptr_t this_isolation() {
  246. return reinterpret_cast<intptr_t>(this);
  247. }
  248. public:
  249. isolated_task_group () : task_group() {}
  250. #if __TBB_CPP11_RVALUE_REF_PRESENT
  251. template<typename F>
  252. void run( F&& f ) {
  253. internal::spawn_delegate sd(
  254. prepare_task< internal::function_task< typename internal::strip<F>::type > >(std::forward<F>(f))
  255. );
  256. internal::isolate_within_arena( sd, this_isolation() );
  257. }
  258. #else
  259. template<typename F>
  260. void run(const F& f) {
  261. internal::spawn_delegate sd( prepare_task< internal::function_task<F> >(f) );
  262. internal::isolate_within_arena( sd, this_isolation() );
  263. }
  264. #endif
  265. template<typename F>
  266. task_group_status run_and_wait( const F& f ) {
  267. task_group_status result = not_complete;
  268. internal::run_wait_delegate< const F > rwd( *this, f, result );
  269. internal::isolate_within_arena( rwd, this_isolation() );
  270. __TBB_ASSERT( result!=not_complete, "premature exit from wait?" );
  271. return result;
  272. }
  273. // TODO: add task_handle rvalues support
  274. template<typename F>
  275. void run( task_handle<F>& h ) {
  276. internal::spawn_delegate sd( prepare_task< internal::task_handle_task<F> >(h) );
  277. internal::isolate_within_arena( sd, this_isolation() );
  278. }
  279. template<typename F>
  280. task_group_status run_and_wait ( task_handle<F>& h ) {
  281. task_group_status result = not_complete;
  282. internal::run_wait_delegate< task_handle<F> > rwd( *this, h, result );
  283. internal::isolate_within_arena( rwd, this_isolation() );
  284. __TBB_ASSERT( result!=not_complete, "premature exit from wait?" );
  285. return result;
  286. }
  287. task_group_status wait() {
  288. task_group_status result = not_complete;
  289. internal::wait_delegate wd( *this, result );
  290. internal::isolate_within_arena( wd, this_isolation() );
  291. __TBB_ASSERT( result!=not_complete, "premature exit from wait?" );
  292. return result;
  293. }
  294. }; // class isolated_task_group
  295. #endif // TBB_PREVIEW_ISOLATED_TASK_GROUP && __TBB_TASK_ISOLATION
  296. inline
  297. bool is_current_task_group_canceling() {
  298. return task::self().is_cancelled();
  299. }
  300. #if __TBB_CPP11_RVALUE_REF_PRESENT
  301. template<class F>
  302. task_handle< typename internal::strip<F>::type > make_task( F&& f ) {
  303. return task_handle< typename internal::strip<F>::type >( std::forward<F>(f) );
  304. }
  305. #else
  306. template<class F>
  307. task_handle<F> make_task( const F& f ) {
  308. return task_handle<F>( f );
  309. }
  310. #endif /* __TBB_CPP11_RVALUE_REF_PRESENT */
  311. } // namespace tbb
  312. #endif /* __TBB_TASK_GROUP_CONTEXT */
  313. #include "internal/_warning_suppress_disable_notice.h"
  314. #undef __TBB_task_group_H_include_area
  315. #endif /* __TBB_task_group_H */