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.

252 lines
7.1 KiB

  1. /*
  2. Copyright 2005-2013 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. template<typename F>
  33. class task_handle : internal::no_assign {
  34. template<typename _F> friend class internal::task_handle_task;
  35. static const intptr_t scheduled = 0x1;
  36. F my_func;
  37. intptr_t my_state;
  38. void mark_scheduled () {
  39. // The check here is intentionally lax to avoid the impact of interlocked operation
  40. if ( my_state & scheduled )
  41. internal::throw_exception( internal::eid_invalid_multiple_scheduling );
  42. my_state |= scheduled;
  43. }
  44. public:
  45. task_handle( const F& f ) : my_func(f), my_state(0) {}
  46. void operator() () const { my_func(); }
  47. };
  48. enum task_group_status {
  49. not_complete,
  50. complete,
  51. canceled
  52. };
  53. namespace internal {
  54. // Suppress gratuitous warnings from icc 11.0 when lambda expressions are used in instances of function_task.
  55. //#pragma warning(disable: 588)
  56. template<typename F>
  57. class function_task : public task {
  58. F my_func;
  59. /*override*/ task* execute() {
  60. my_func();
  61. return NULL;
  62. }
  63. public:
  64. function_task( const F& f ) : my_func(f) {}
  65. };
  66. template<typename F>
  67. class task_handle_task : public task {
  68. task_handle<F>& my_handle;
  69. /*override*/ task* execute() {
  70. my_handle();
  71. return NULL;
  72. }
  73. public:
  74. task_handle_task( task_handle<F>& h ) : my_handle(h) { h.mark_scheduled(); }
  75. };
  76. class task_group_base : internal::no_copy {
  77. protected:
  78. empty_task* my_root;
  79. task_group_context my_context;
  80. task& owner () { return *my_root; }
  81. template<typename F>
  82. task_group_status internal_run_and_wait( F& f ) {
  83. __TBB_TRY {
  84. if ( !my_context.is_group_execution_cancelled() )
  85. f();
  86. } __TBB_CATCH( ... ) {
  87. my_context.register_pending_exception();
  88. }
  89. return wait();
  90. }
  91. template<typename F, typename Task>
  92. void internal_run( F& f ) {
  93. owner().spawn( *new( owner().allocate_additional_child_of(*my_root) ) Task(f) );
  94. }
  95. public:
  96. task_group_base( uintptr_t traits = 0 )
  97. : my_context(task_group_context::bound, task_group_context::default_traits | traits)
  98. {
  99. my_root = new( task::allocate_root(my_context) ) empty_task;
  100. my_root->set_ref_count(1);
  101. }
  102. ~task_group_base() {
  103. if( my_root->ref_count() > 1 ) {
  104. bool stack_unwinding_in_progress = std::uncaught_exception();
  105. // Always attempt to do proper cleanup to avoid inevitable memory corruption
  106. // in case of missing wait (for the sake of better testability & debuggability)
  107. if ( !is_canceling() )
  108. cancel();
  109. __TBB_TRY {
  110. my_root->wait_for_all();
  111. } __TBB_CATCH (...) {
  112. task::destroy(*my_root);
  113. __TBB_RETHROW();
  114. }
  115. task::destroy(*my_root);
  116. if ( !stack_unwinding_in_progress )
  117. internal::throw_exception( internal::eid_missing_wait );
  118. }
  119. else {
  120. task::destroy(*my_root);
  121. }
  122. }
  123. template<typename F>
  124. void run( task_handle<F>& h ) {
  125. internal_run< task_handle<F>, internal::task_handle_task<F> >( h );
  126. }
  127. task_group_status wait() {
  128. __TBB_TRY {
  129. my_root->wait_for_all();
  130. } __TBB_CATCH( ... ) {
  131. my_context.reset();
  132. __TBB_RETHROW();
  133. }
  134. if ( my_context.is_group_execution_cancelled() ) {
  135. my_context.reset();
  136. return canceled;
  137. }
  138. return complete;
  139. }
  140. bool is_canceling() {
  141. return my_context.is_group_execution_cancelled();
  142. }
  143. void cancel() {
  144. my_context.cancel_group_execution();
  145. }
  146. }; // class task_group_base
  147. } // namespace internal
  148. class task_group : public internal::task_group_base {
  149. public:
  150. task_group () : task_group_base( task_group_context::concurrent_wait ) {}
  151. #if TBB_DEPRECATED
  152. ~task_group() __TBB_TRY {
  153. __TBB_ASSERT( my_root->ref_count() != 0, NULL );
  154. if( my_root->ref_count() > 1 )
  155. my_root->wait_for_all();
  156. }
  157. #if TBB_USE_EXCEPTIONS
  158. catch (...) {
  159. // Have to destroy my_root here as the base class destructor won't be called
  160. task::destroy(*my_root);
  161. throw;
  162. }
  163. #endif /* TBB_USE_EXCEPTIONS */
  164. #endif /* TBB_DEPRECATED */
  165. #if __SUNPRO_CC
  166. template<typename F>
  167. void run( task_handle<F>& h ) {
  168. internal_run< task_handle<F>, internal::task_handle_task<F> >( h );
  169. }
  170. #else
  171. using task_group_base::run;
  172. #endif
  173. template<typename F>
  174. void run( const F& f ) {
  175. internal_run< const F, internal::function_task<F> >( f );
  176. }
  177. template<typename F>
  178. task_group_status run_and_wait( const F& f ) {
  179. return internal_run_and_wait<const F>( f );
  180. }
  181. template<typename F>
  182. task_group_status run_and_wait( task_handle<F>& h ) {
  183. return internal_run_and_wait< task_handle<F> >( h );
  184. }
  185. }; // class task_group
  186. class structured_task_group : public internal::task_group_base {
  187. public:
  188. template<typename F>
  189. task_group_status run_and_wait ( task_handle<F>& h ) {
  190. return internal_run_and_wait< task_handle<F> >( h );
  191. }
  192. task_group_status wait() {
  193. task_group_status res = task_group_base::wait();
  194. my_root->set_ref_count(1);
  195. return res;
  196. }
  197. }; // class structured_task_group
  198. inline
  199. bool is_current_task_group_canceling() {
  200. return task::self().is_cancelled();
  201. }
  202. template<class F>
  203. task_handle<F> make_task( const F& f ) {
  204. return task_handle<F>( f );
  205. }
  206. } // namespace tbb
  207. #endif /* __TBB_TASK_GROUP_CONTEXT */
  208. #endif /* __TBB_task_group_H */