You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.9 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_task_group_H
  24. #define __TBB_task_group_H
  25. #include "task.h"
  26. #include "tbb_exception.h"
  27. #if __TBB_TASK_GROUP_CONTEXT
  28. namespace tbb {
  29. namespace internal {
  30. template<typename F> class task_handle_task;
  31. }
  32. class task_group;
  33. class structured_task_group;
  34. template<typename F>
  35. class task_handle : internal::no_assign {
  36. template<typename _F> friend class internal::task_handle_task;
  37. friend class task_group;
  38. friend class structured_task_group;
  39. static const intptr_t scheduled = 0x1;
  40. F my_func;
  41. intptr_t my_state;
  42. void mark_scheduled () {
  43. // The check here is intentionally lax to avoid the impact of interlocked operation
  44. if ( my_state & scheduled )
  45. internal::throw_exception( internal::eid_invalid_multiple_scheduling );
  46. my_state |= scheduled;
  47. }
  48. public:
  49. task_handle( const F& f ) : my_func(f), my_state(0) {}
  50. void operator() () const { my_func(); }
  51. };
  52. enum task_group_status {
  53. not_complete,
  54. complete,
  55. canceled
  56. };
  57. namespace internal {
  58. template<typename F>
  59. class task_handle_task : public task {
  60. task_handle<F>& my_handle;
  61. /*override*/ task* execute() {
  62. my_handle();
  63. return NULL;
  64. }
  65. public:
  66. task_handle_task( task_handle<F>& h ) : my_handle(h) { h.mark_scheduled(); }
  67. };
  68. class task_group_base : internal::no_copy {
  69. protected:
  70. empty_task* my_root;
  71. task_group_context my_context;
  72. task& owner () { return *my_root; }
  73. template<typename F>
  74. task_group_status internal_run_and_wait( F& f ) {
  75. __TBB_TRY {
  76. if ( !my_context.is_group_execution_cancelled() )
  77. f();
  78. } __TBB_CATCH( ... ) {
  79. my_context.register_pending_exception();
  80. }
  81. return wait();
  82. }
  83. template<typename F, typename Task>
  84. void internal_run( F& f ) {
  85. owner().spawn( *new( owner().allocate_additional_child_of(*my_root) ) Task(f) );
  86. }
  87. public:
  88. task_group_base( uintptr_t traits = 0 )
  89. : my_context(task_group_context::bound, task_group_context::default_traits | traits)
  90. {
  91. my_root = new( task::allocate_root(my_context) ) empty_task;
  92. my_root->set_ref_count(1);
  93. }
  94. ~task_group_base() {
  95. if( my_root->ref_count() > 1 ) {
  96. bool stack_unwinding_in_progress = std::uncaught_exception();
  97. // Always attempt to do proper cleanup to avoid inevitable memory corruption
  98. // in case of missing wait (for the sake of better testability & debuggability)
  99. if ( !is_canceling() )
  100. cancel();
  101. __TBB_TRY {
  102. my_root->wait_for_all();
  103. } __TBB_CATCH (...) {
  104. task::destroy(*my_root);
  105. __TBB_RETHROW();
  106. }
  107. task::destroy(*my_root);
  108. if ( !stack_unwinding_in_progress )
  109. internal::throw_exception( internal::eid_missing_wait );
  110. }
  111. else {
  112. task::destroy(*my_root);
  113. }
  114. }
  115. template<typename F>
  116. void run( task_handle<F>& h ) {
  117. internal_run< task_handle<F>, internal::task_handle_task<F> >( h );
  118. }
  119. task_group_status wait() {
  120. __TBB_TRY {
  121. my_root->wait_for_all();
  122. } __TBB_CATCH( ... ) {
  123. my_context.reset();
  124. __TBB_RETHROW();
  125. }
  126. if ( my_context.is_group_execution_cancelled() ) {
  127. my_context.reset();
  128. return canceled;
  129. }
  130. return complete;
  131. }
  132. bool is_canceling() {
  133. return my_context.is_group_execution_cancelled();
  134. }
  135. void cancel() {
  136. my_context.cancel_group_execution();
  137. }
  138. }; // class task_group_base
  139. } // namespace internal
  140. class task_group : public internal::task_group_base {
  141. public:
  142. task_group () : task_group_base( task_group_context::concurrent_wait ) {}
  143. #if TBB_DEPRECATED
  144. ~task_group() __TBB_TRY {
  145. __TBB_ASSERT( my_root->ref_count() != 0, NULL );
  146. if( my_root->ref_count() > 1 )
  147. my_root->wait_for_all();
  148. }
  149. #if TBB_USE_EXCEPTIONS
  150. catch (...) {
  151. // Have to destroy my_root here as the base class destructor won't be called
  152. task::destroy(*my_root);
  153. throw;
  154. }
  155. #endif /* TBB_USE_EXCEPTIONS */
  156. #endif /* TBB_DEPRECATED */
  157. #if __SUNPRO_CC
  158. template<typename F>
  159. void run( task_handle<F>& h ) {
  160. internal_run< task_handle<F>, internal::task_handle_task<F> >( h );
  161. }
  162. #else
  163. using task_group_base::run;
  164. #endif
  165. template<typename F>
  166. void run( const F& f ) {
  167. internal_run< const F, internal::function_task<F> >( f );
  168. }
  169. template<typename F>
  170. task_group_status run_and_wait( const F& f ) {
  171. return internal_run_and_wait<const F>( f );
  172. }
  173. template<typename F>
  174. task_group_status run_and_wait( task_handle<F>& h ) {
  175. h.mark_scheduled();
  176. return internal_run_and_wait< task_handle<F> >( h );
  177. }
  178. }; // class task_group
  179. class structured_task_group : public internal::task_group_base {
  180. public:
  181. template<typename F>
  182. task_group_status run_and_wait ( task_handle<F>& h ) {
  183. h.mark_scheduled();
  184. return internal_run_and_wait< task_handle<F> >( h );
  185. }
  186. task_group_status wait() {
  187. task_group_status res = task_group_base::wait();
  188. my_root->set_ref_count(1);
  189. return res;
  190. }
  191. }; // class structured_task_group
  192. inline
  193. bool is_current_task_group_canceling() {
  194. return task::self().is_cancelled();
  195. }
  196. template<class F>
  197. task_handle<F> make_task( const F& f ) {
  198. return task_handle<F>( f );
  199. }
  200. } // namespace tbb
  201. #endif /* __TBB_TASK_GROUP_CONTEXT */
  202. #endif /* __TBB_task_group_H */