Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
task_group.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #ifndef __TBB_task_group_H
22 #define __TBB_task_group_H
23 
24 #include "task.h"
25 #include "tbb_exception.h"
27 
28 #if __TBB_TASK_GROUP_CONTEXT
29 
30 namespace tbb {
31 
32 namespace internal {
33  template<typename F> class task_handle_task;
34 }
35 
36 class task_group;
38 
39 template<typename F>
40 class task_handle : internal::no_assign {
41  template<typename _F> friend class internal::task_handle_task;
42  friend class task_group;
43  friend class structured_task_group;
44 
45  static const intptr_t scheduled = 0x1;
46 
48  intptr_t my_state;
49 
50  void mark_scheduled () {
51  // The check here is intentionally lax to avoid the impact of interlocked operation
52  if ( my_state & scheduled )
55  }
56 public:
57  task_handle( const F& f ) : my_func(f), my_state(0) {}
58 #if __TBB_CPP11_RVALUE_REF_PRESENT
59  task_handle( F&& f ) : my_func( std::move(f)), my_state(0) {}
60 #endif
61 
62  void operator() () const { my_func(); }
63 };
64 
69 };
70 
71 namespace internal {
72 
73 template<typename F>
74 class task_handle_task : public task {
77  my_handle();
78  return NULL;
79  }
80 public:
81  task_handle_task( task_handle<F>& h ) : my_handle(h) { h.mark_scheduled(); }
82 };
83 
84 class task_group_base : internal::no_copy {
85  class ref_count_guard : internal::no_copy {
87  public:
90  }
93  }
94  };
95 protected:
98 
99  task& owner () { return *my_root; }
100 
101  template<typename F>
103  __TBB_TRY {
105  // We need to increase the reference count of the root task to notify waiters that
106  // this task group has some work in progress.
107  ref_count_guard guard(*my_root);
108  f();
109  }
110  } __TBB_CATCH( ... ) {
112  }
113  return wait();
114  }
115 
116  template<typename Task, typename F>
118  owner().spawn( *new( owner().allocate_additional_child_of(*my_root) ) Task( internal::forward<F>(f) ));
119  }
120 
121 public:
122  task_group_base( uintptr_t traits = 0 )
123  : my_context(task_group_context::bound, task_group_context::default_traits | traits)
124  {
127  }
128 
130  if( my_root->ref_count() > 1 ) {
131 #if __TBB_CPP17_UNCAUGHT_EXCEPTIONS_PRESENT
132  bool stack_unwinding_in_progress = std::uncaught_exceptions() > 0;
133 #else
134  bool stack_unwinding_in_progress = std::uncaught_exception();
135 #endif
136  // Always attempt to do proper cleanup to avoid inevitable memory corruption
137  // in case of missing wait (for the sake of better testability & debuggability)
138  if ( !is_canceling() )
139  cancel();
140  __TBB_TRY {
142  } __TBB_CATCH (...) {
143  task::destroy(*my_root);
144  __TBB_RETHROW();
145  }
146  task::destroy(*my_root);
147  if ( !stack_unwinding_in_progress )
149  }
150  else {
151  task::destroy(*my_root);
152  }
153  }
154 
155  template<typename F>
156  void run( task_handle<F>& h ) {
157  internal_run< internal::task_handle_task<F> >( h );
158  }
159 
161  __TBB_TRY {
163  } __TBB_CATCH( ... ) {
164  my_context.reset();
165  __TBB_RETHROW();
166  }
168  // TODO: the reset method is not thread-safe. Ensure the correct behavior.
169  my_context.reset();
170  return canceled;
171  }
172  return complete;
173  }
174 
175  bool is_canceling() {
177  }
178 
179  void cancel() {
181  }
182 }; // class task_group_base
183 
184 } // namespace internal
185 
186 class task_group : public internal::task_group_base {
187 public:
188  task_group () : task_group_base( task_group_context::concurrent_wait ) {}
189 
190 #if __SUNPRO_CC
191  template<typename F>
192  void run( task_handle<F>& h ) {
193  internal_run< internal::task_handle_task<F> >( h );
194  }
195 #else
196  using task_group_base::run;
197 #endif
198 
199 #if __TBB_CPP11_RVALUE_REF_PRESENT
200  template<typename F>
201  void run( F&& f ) {
203  }
204 #else
205  template<typename F>
206  void run(const F& f) {
207  internal_run<internal::function_task<F> >(f);
208  }
209 #endif
210 
211  template<typename F>
213  return internal_run_and_wait<const F>( f );
214  }
215 
216  // TODO: add task_handle rvalues support
217  template<typename F>
219  h.mark_scheduled();
220  return internal_run_and_wait< task_handle<F> >( h );
221  }
222 }; // class task_group
223 
224 class structured_task_group : public internal::task_group_base {
225 public:
226  // TODO: add task_handle rvalues support
227  template<typename F>
229  h.mark_scheduled();
230  return internal_run_and_wait< task_handle<F> >( h );
231  }
232 
236  return res;
237  }
238 }; // class structured_task_group
239 
240 inline
242  return task::self().is_cancelled();
243 }
244 
245 #if __TBB_CPP11_RVALUE_REF_PRESENT
246 template<class F>
248  return task_handle< typename internal::strip<F>::type >( std::forward<F>(f) );
249 }
250 #else
251 template<class F>
252 task_handle<F> make_task( const F& f ) {
253  return task_handle<F>( f );
254 }
255 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT */
256 
257 } // namespace tbb
258 
259 #endif /* __TBB_TASK_GROUP_CONTEXT */
260 
261 #endif /* __TBB_task_group_H */
#define __TBB_override
Definition: tbb_stddef.h:244
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function h
void __TBB_EXPORTED_METHOD register_pending_exception()
Records the pending exception, and cancels the task group.
task_handle< F > & my_handle
Definition: task_group.h:75
void operator()() const
Definition: task_group.h:62
void run(F &&f)
Definition: task_group.h:201
task_handle(F &&f)
Definition: task_group.h:59
task_group_base(uintptr_t traits=0)
Definition: task_group.h:122
void internal_run(__TBB_FORWARDING_REF(F) f)
Definition: task_group.h:117
#define __TBB_NOEXCEPT(expression)
Definition: tbb_stddef.h:114
task_group_status
Definition: task_group.h:65
task that does nothing. Useful for synchronization.
Definition: task.h:959
task_group_status run_and_wait(task_handle< F > &h)
Definition: task_group.h:228
Used to form groups of tasks.
Definition: task.h:335
task_group_status wait()
Definition: task_group.h:160
bool is_current_task_group_canceling()
Definition: task_group.h:241
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type type
task_handle_task(task_handle< F > &h)
Definition: task_group.h:81
task_group_status run_and_wait(const F &f)
Definition: task_group.h:212
#define __TBB_TRY
Definition: tbb_stddef.h:287
bool __TBB_EXPORTED_METHOD cancel_group_execution()
Initiates cancellation of all tasks in this cancellation group and its subordinate groups.
Base class for user-defined tasks.
Definition: task.h:592
static task &__TBB_EXPORTED_FUNC self()
The innermost task being executed or destroyed by the current thread at the moment.
Definition: task.cpp:205
static const intptr_t scheduled
Definition: task_group.h:45
void __TBB_EXPORTED_METHOD reset()
Forcefully reinitializes the context after the task tree it was associated with is completed.
task_handle(const F &f)
Definition: task_group.h:57
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
intptr_t my_state
Definition: task_group.h:48
void wait_for_all()
Wait for reference count to become one, and set reference count to zero.
Definition: task.h:792
friend class internal::task_handle_task
Definition: task_group.h:41
task_group_status wait()
Definition: task_group.h:233
#define __TBB_CATCH(e)
Definition: tbb_stddef.h:288
task_handle< typename internal::strip< F >::type > make_task(F &&f)
Definition: task_group.h:247
~task_group_base() __TBB_NOEXCEPT(false)
Definition: task_group.h:129
#define __TBB_FORWARDING_REF(A)
Definition: tbb_stddef.h:500
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task * task
The graph class.
void mark_scheduled()
Definition: task_group.h:50
int decrement_ref_count()
Atomically decrement reference count and returns its new value.
Definition: task.h:761
task_group_status run_and_wait(task_handle< F > &h)
Definition: task_group.h:218
task * execute() __TBB_override
Should be overridden by derived classes.
Definition: task_group.h:76
bool __TBB_EXPORTED_METHOD is_group_execution_cancelled() const
Returns true if the context received cancellation request.
void increment_ref_count()
Atomically increment reference count.
Definition: task.h:744
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:309
void run(task_handle< F > &h)
Definition: task_group.h:156
bool is_cancelled() const
Returns true if the context has received cancellation request.
Definition: task.h:913
void set_ref_count(int count)
Set reference count.
Definition: task.h:734
static internal::allocate_root_proxy allocate_root()
Returns proxy for overloaded new that allocates a root task.
Definition: task.h:636
task_group_status internal_run_and_wait(F &f)
Definition: task_group.h:102
task_group_context my_context
Definition: task_group.h:97
#define __TBB_RETHROW()
Definition: tbb_stddef.h:290
int ref_count() const
The internal reference count.
Definition: task.h:862

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.