Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
task_arena.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 #ifndef __TBB_task_arena_H
18 #define __TBB_task_arena_H
19 
20 #define __TBB_task_arena_H_include_area
22 
23 #include "task.h"
24 #include "tbb_exception.h"
26 #if TBB_USE_THREADING_TOOLS
27 #include "atomic.h" // for as_atomic
28 #endif
29 #include "aligned_space.h"
30 
31 namespace tbb {
32 
33 namespace this_task_arena {
34  int max_concurrency();
35 } // namespace this_task_arena
36 
38 namespace internal {
40 
41  class arena;
43 } // namespace internal
45 
46 namespace interface7 {
47 class task_arena;
48 
50 namespace internal {
51 using namespace tbb::internal; //e.g. function_task from task.h
52 
54 public:
55  virtual void operator()() const = 0;
56  virtual ~delegate_base() {}
57 };
58 
59 // If decltype is available, the helper detects the return type of functor of specified type,
60 // otherwise it defines the void type.
61 template <typename F>
63 #if __TBB_CPP11_DECLTYPE_PRESENT && !__TBB_CPP11_DECLTYPE_OF_FUNCTION_RETURN_TYPE_BROKEN
64  typedef decltype(declval<F>()()) type;
65 #else
66  typedef void type;
67 #endif
68 };
69 
70 template<typename F, typename R>
72  F &my_func;
73  tbb::aligned_space<R> my_return_storage;
74  // The function should be called only once.
75  void operator()() const __TBB_override {
76  new (my_return_storage.begin()) R(my_func());
77  }
78 public:
79  delegated_function(F& f) : my_func(f) {}
80  // The function can be called only after operator() and only once.
81  R consume_result() const {
82  return tbb::internal::move(*(my_return_storage.begin()));
83  }
85  my_return_storage.begin()->~R();
86  }
87 };
88 
89 template<typename F>
91  F &my_func;
92  void operator()() const __TBB_override {
93  my_func();
94  }
95 public:
96  delegated_function(F& f) : my_func(f) {}
97  void consume_result() const {}
98 
99  friend class task_arena_base;
100 };
101 
103 protected:
106 
107 #if __TBB_TASK_GROUP_CONTEXT
108  task_group_context *my_context;
110 #endif
111 
114 
116  unsigned my_master_slots;
117 
120 
121  enum {
122  default_flags = 0
123 #if __TBB_TASK_GROUP_CONTEXT
125  , exact_exception_flag = task_group_context::exact_exception // used to specify flag for context directly
126 #endif
127  };
128 
129  task_arena_base(int max_concurrency, unsigned reserved_for_masters)
130  : my_arena(0)
132  , my_context(0)
133 #endif
134  , my_max_concurrency(max_concurrency)
135  , my_master_slots(reserved_for_masters)
136  , my_version_and_traits(default_flags)
137  {}
138 
139  void __TBB_EXPORTED_METHOD internal_initialize();
140  void __TBB_EXPORTED_METHOD internal_terminate();
141  void __TBB_EXPORTED_METHOD internal_attach();
142  void __TBB_EXPORTED_METHOD internal_enqueue( task&, intptr_t ) const;
143  void __TBB_EXPORTED_METHOD internal_execute( delegate_base& ) const;
144  void __TBB_EXPORTED_METHOD internal_wait() const;
145  static int __TBB_EXPORTED_FUNC internal_current_slot();
146  static int __TBB_EXPORTED_FUNC internal_max_concurrency( const task_arena * );
147 public:
149  static const int automatic = -1;
150  static const int not_initialized = -2;
151 
152 };
153 
154 #if __TBB_TASK_ISOLATION
155 void __TBB_EXPORTED_FUNC isolate_within_arena( delegate_base& d, intptr_t isolation = 0 );
156 
157 template<typename R, typename F>
158 R isolate_impl(F& f) {
161  return d.consume_result();
162 }
163 #endif /* __TBB_TASK_ISOLATION */
164 } // namespace internal
166 
174  friend void task::enqueue(task&, task_arena&
176  , priority_t
177 #endif
178  );
182  __TBB_ASSERT( my_arena, "task_arena initialization is incomplete" );
183 #if __TBB_TASK_GROUP_CONTEXT
184  __TBB_ASSERT( my_context, "task_arena initialization is incomplete" );
185 #endif
186 #if TBB_USE_THREADING_TOOLS
187  // Actual synchronization happens in internal_initialize & internal_attach.
188  // The race on setting my_initialized is benign, but should be hidden from Intel(R) Inspector
189  internal::as_atomic(my_initialized).fetch_and_store<release>(true);
190 #else
191  my_initialized = true;
192 #endif
193  }
194 
195  template<typename F>
198  , priority_t p = priority_t(0)
199 #endif
200  ) {
201 #if !__TBB_TASK_PRIORITY
202  intptr_t p = 0;
203 #endif
204  initialize();
205 #if __TBB_TASK_GROUP_CONTEXT
206  internal_enqueue(*new(task::allocate_root(*my_context)) internal::function_task< typename internal::strip<F>::type >(internal::forward<F>(f)), p);
207 #else
208  internal_enqueue(*new(task::allocate_root()) internal::function_task< typename internal::strip<F>::type >(internal::forward<F>(f)), p);
209 #endif /* __TBB_TASK_GROUP_CONTEXT */
210  }
211 
212  template<typename R, typename F>
213  R execute_impl(F& f) {
214  initialize();
216  internal_execute(d);
217  return d.consume_result();
218  }
219 
220 public:
222 
227  task_arena(int max_concurrency_ = automatic, unsigned reserved_for_masters = 1)
228  : task_arena_base(max_concurrency_, reserved_for_masters)
229  , my_initialized(false)
230  {}
231 
233  task_arena(const task_arena &s) // copy settings but not the reference or instance
234  : task_arena_base(s.my_max_concurrency, s.my_master_slots)
235  , my_initialized(false)
236  {}
237 
239  struct attach {};
240 
242  explicit task_arena( attach )
243  : task_arena_base(automatic, 1) // use default settings if attach fails
244  , my_initialized(false)
245  {
246  internal_attach();
247  if( my_arena ) my_initialized = true;
248  }
249 
251  inline void initialize() {
252  if( !my_initialized ) {
253  internal_initialize();
254  mark_initialized();
255  }
256  }
257 
259  inline void initialize(int max_concurrency_, unsigned reserved_for_masters = 1) {
260  // TODO: decide if this call must be thread-safe
261  __TBB_ASSERT(!my_arena, "Impossible to modify settings of an already initialized task_arena");
262  if( !my_initialized ) {
263  my_max_concurrency = max_concurrency_;
264  my_master_slots = reserved_for_masters;
265  initialize();
266  }
267  }
268 
270  inline void initialize(attach) {
271  // TODO: decide if this call must be thread-safe
272  __TBB_ASSERT(!my_arena, "Impossible to modify settings of an already initialized task_arena");
273  if( !my_initialized ) {
274  internal_attach();
275  if ( !my_arena ) internal_initialize();
276  mark_initialized();
277  }
278  }
279 
282  inline void terminate() {
283  if( my_initialized ) {
284  internal_terminate();
285  my_initialized = false;
286  }
287  }
288 
292  terminate();
293  }
294 
297  bool is_active() const { return my_initialized; }
298 
301 
302 #if __TBB_CPP11_RVALUE_REF_PRESENT
303  template<typename F>
304  void enqueue( F&& f ) {
305  enqueue_impl(std::forward<F>(f));
306  }
307 #else
308  template<typename F>
309  void enqueue( const F& f ) {
310  enqueue_impl(f);
311  }
312 #endif
313 
314 #if __TBB_TASK_PRIORITY
315  template<typename F>
318 #if __TBB_CPP11_RVALUE_REF_PRESENT
320 #if __TBB_PREVIEW_CRITICAL_TASKS
322  || p == internal::priority_critical, "Invalid priority level value");
323 #else
324  __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value");
325 #endif
326  enqueue_impl(std::forward<F>(f), p);
327  }
328 #else
329  __TBB_DEPRECATED void enqueue( const F& f, priority_t p ) {
330 #if __TBB_PREVIEW_CRITICAL_TASKS
332  || p == internal::priority_critical, "Invalid priority level value");
333 #else
334  __TBB_ASSERT(p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value");
335 #endif
336  enqueue_impl(f,p);
337  }
338 #endif
339 #endif// __TBB_TASK_PRIORITY
340 
345  template<typename F>
348  }
349 
354  template<typename F>
357  }
358 
359 #if __TBB_EXTRA_DEBUG
360  void debug_wait_until_empty() {
364  initialize();
365  internal_wait();
366  }
367 #endif //__TBB_EXTRA_DEBUG
368 
371  inline static int current_thread_index() {
372  return internal_current_slot();
373  }
374 
376  inline int max_concurrency() const {
377  // Handle special cases inside the library
378  return (my_max_concurrency>1) ? my_max_concurrency : internal_max_concurrency(this);
379  }
380 };
381 
382 namespace this_task_arena {
383 #if __TBB_TASK_ISOLATION
384  template<typename F>
389  }
390 
393  template<typename F>
396  }
397 #endif /* __TBB_TASK_ISOLATION */
398 } // namespace this_task_arena
399 } // namespace interfaceX
400 
402 
403 namespace this_task_arena {
404  using namespace interface7::this_task_arena;
405 
407  inline int current_thread_index() {
409  return idx == -1 ? tbb::task_arena::not_initialized : idx;
410  }
411 
413  inline int max_concurrency() {
415  }
416 } // namespace this_task_arena
417 
419 #if __TBB_TASK_PRIORITY
421 #else
422 void task::enqueue( task& t, task_arena& arena ) {
423  intptr_t p = 0;
424 #endif
425  arena.initialize();
427  arena.internal_enqueue(t, p);
428 }
429 } // namespace tbb
430 
432 #undef __TBB_task_arena_H_include_area
433 
434 #endif /* __TBB_task_arena_H */
task_arena_base(int max_concurrency, unsigned reserved_for_masters)
Definition: task_arena.h:129
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:319
Base class for user-defined tasks.
Definition: task.h:604
int max_concurrency() const
Returns the maximal number of threads that can work inside the arena.
Definition: task_arena.h:376
tbb::aligned_space< R > my_return_storage
Definition: task_arena.h:73
static int current_thread_index()
Definition: task_arena.h:371
void const char const char int ITT_FORMAT __itt_group_sync s
internal::arena * my_arena
NULL if not currently initialized.
Definition: task_arena.h:105
int max_concurrency()
Returns the maximal number of threads that can work inside the arena.
Definition: task_arena.h:413
void __TBB_EXPORTED_FUNC isolate_within_arena(delegate_base &d, intptr_t isolation=0)
#define __TBB_TASK_PRIORITY
Definition: tbb_config.h:574
Base class for types that should not be assigned.
Definition: tbb_stddef.h:322
static internal::allocate_root_proxy allocate_root()
Returns proxy for overloaded new that allocates a root task.
Definition: task.h:652
int current_thread_index()
Returns the index, aka slot number, of the calling thread in its current arena.
Definition: task_arena.h:407
#define __TBB_FORWARDING_REF(A)
Definition: tbb_stddef.h:517
intptr_t my_version_and_traits
Special settings.
Definition: task_arena.h:119
int my_max_concurrency
Concurrency level for deferred initialization.
Definition: task_arena.h:113
void enqueue_impl(__TBB_FORWARDING_REF(F) f, priority_t p=priority_t(0))
Definition: task_arena.h:196
atomic< T > & as_atomic(T &t)
Definition: atomic.h:572
static const int priority_critical
Definition: task.h:302
Release.
Definition: atomic.h:59
Used to form groups of tasks.
Definition: task.h:347
task_arena(int max_concurrency_=automatic, unsigned reserved_for_masters=1)
Creates task_arena with certain concurrency limits.
Definition: task_arena.h:227
void initialize(int max_concurrency_, unsigned reserved_for_masters=1)
Overrides concurrency level and forces initialization of internal representation. ...
Definition: task_arena.h:259
unsigned my_master_slots
Reserved master slots.
Definition: task_arena.h:116
void initialize()
Forces allocation of the resources for the task_arena as specified in constructor arguments...
Definition: task_arena.h:251
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
Identifiers declared inside namespace internal should never be used directly by client code...
Definition: atomic.h:65
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#define __TBB_override
Definition: tbb_stddef.h:240
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
task_arena(const task_arena &s)
Copies settings from another task_arena.
Definition: task_arena.h:233
__TBB_DEPRECATED void enqueue(F &&f, priority_t p)
Definition: task_arena.h:319
#define __TBB_TASK_GROUP_CONTEXT
Definition: tbb_config.h:544
void __TBB_EXPORTED_METHOD internal_enqueue(task &, intptr_t) const
Definition: arena.cpp:877
static int __TBB_EXPORTED_FUNC internal_max_concurrency(const task_arena *)
Definition: arena.cpp:1133
void const char const char int ITT_FORMAT __itt_group_sync p
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
static void enqueue(task &t)
Enqueue task for starvation-resistant execution.
Definition: task.h:825
Tag class used to indicate the "attaching" constructor.
Definition: task_arena.h:239
priority_t
Definition: task.h:306
internal::return_type_or_void< F >::type isolate(const F &f)
Definition: task_arena.h:394
task_arena(attach)
Creates an instance of task_arena attached to the current arena of the thread.
Definition: task_arena.h:242
void operator()() const __TBB_override
Definition: task_arena.h:75
internal::return_type_or_void< F >::type execute(const F &f)
Definition: task_arena.h:355
#define __TBB_DEPRECATED
Definition: tbb_config.h:639
internal::return_type_or_void< F >::type execute(F &f)
Definition: task_arena.h:346
void initialize(attach)
Attaches this instance to the current arena of the thread.
Definition: task_arena.h:270
#define __TBB_EXPORTED_FUNC
The graph class.

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.