/usr/include/tbb/task_arena.h is in libtbb-dev 2017~U7-8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /*
Copyright (c) 2005-2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_task_arena_H
#define __TBB_task_arena_H
#include "task.h"
#include "tbb_exception.h"
#if TBB_USE_THREADING_TOOLS
#include "atomic.h" // for as_atomic
#endif
namespace tbb {
namespace this_task_arena {
int max_concurrency();
} // namespace this_task_arena
//! @cond INTERNAL
namespace internal {
//! Internal to library. Should not be used by clients.
/** @ingroup task_scheduling */
class arena;
class task_scheduler_observer_v3;
} // namespace internal
//! @endcond
namespace interface7 {
class task_arena;
//! @cond INTERNAL
namespace internal {
using namespace tbb::internal; //e.g. function_task from task.h
class delegate_base : no_assign {
public:
virtual void operator()() const = 0;
virtual ~delegate_base() {}
};
template<typename F>
class delegated_function : public delegate_base {
F &my_func;
void operator()() const __TBB_override {
my_func();
}
public:
delegated_function ( F& f ) : my_func(f) {}
};
class task_arena_base {
protected:
//! NULL if not currently initialized.
internal::arena* my_arena;
#if __TBB_TASK_GROUP_CONTEXT
//! default context of the arena
task_group_context *my_context;
#endif
//! Concurrency level for deferred initialization
int my_max_concurrency;
//! Reserved master slots
unsigned my_master_slots;
//! Special settings
intptr_t my_version_and_traits;
enum {
default_flags = 0
#if __TBB_TASK_GROUP_CONTEXT
| (task_group_context::default_traits & task_group_context::exact_exception) // 0 or 1 << 16
, exact_exception_flag = task_group_context::exact_exception // used to specify flag for context directly
#endif
};
task_arena_base(int max_concurrency, unsigned reserved_for_masters)
: my_arena(0)
#if __TBB_TASK_GROUP_CONTEXT
, my_context(0)
#endif
, my_max_concurrency(max_concurrency)
, my_master_slots(reserved_for_masters)
, my_version_and_traits(default_flags)
{}
void __TBB_EXPORTED_METHOD internal_initialize();
void __TBB_EXPORTED_METHOD internal_terminate();
void __TBB_EXPORTED_METHOD internal_attach();
void __TBB_EXPORTED_METHOD internal_enqueue( task&, intptr_t ) const;
void __TBB_EXPORTED_METHOD internal_execute( delegate_base& ) const;
void __TBB_EXPORTED_METHOD internal_wait() const;
static int __TBB_EXPORTED_FUNC internal_current_slot();
static int __TBB_EXPORTED_FUNC internal_max_concurrency( const task_arena * );
public:
//! Typedef for number of threads that is automatic.
static const int automatic = -1;
static const int not_initialized = -2;
};
#if __TBB_TASK_ISOLATION
void __TBB_EXPORTED_FUNC isolate_within_arena( delegate_base& d, intptr_t reserved = 0 );
#endif /* __TBB_TASK_ISOLATION */
} // namespace internal
//! @endcond
/** 1-to-1 proxy representation class of scheduler's arena
* Constructors set up settings only, real construction is deferred till the first method invocation
* Destructor only removes one of the references to the inner arena representation.
* Final destruction happens when all the references (and the work) are gone.
*/
class task_arena : public internal::task_arena_base {
friend class tbb::internal::task_scheduler_observer_v3;
friend int tbb::this_task_arena::max_concurrency();
bool my_initialized;
void mark_initialized() {
__TBB_ASSERT( my_arena, "task_arena initialization is incomplete" );
#if __TBB_TASK_GROUP_CONTEXT
__TBB_ASSERT( my_context, "task_arena initialization is incomplete" );
#endif
#if TBB_USE_THREADING_TOOLS
// Actual synchronization happens in internal_initialize & internal_attach.
// The race on setting my_initialized is benign, but should be hidden from Intel(R) Inspector
internal::as_atomic(my_initialized).fetch_and_store<release>(true);
#else
my_initialized = true;
#endif
}
public:
//! Creates task_arena with certain concurrency limits
/** Sets up settings only, real construction is deferred till the first method invocation
* @arg max_concurrency specifies total number of slots in arena where threads work
* @arg reserved_for_masters specifies number of slots to be used by master threads only.
* Value of 1 is default and reflects behavior of implicit arenas.
**/
task_arena(int max_concurrency_ = automatic, unsigned reserved_for_masters = 1)
: task_arena_base(max_concurrency_, reserved_for_masters)
, my_initialized(false)
{}
//! Copies settings from another task_arena
task_arena(const task_arena &s) // copy settings but not the reference or instance
: task_arena_base(s.my_max_concurrency, s.my_master_slots)
, my_initialized(false)
{}
//! Tag class used to indicate the "attaching" constructor
struct attach {};
//! Creates an instance of task_arena attached to the current arena of the thread
explicit task_arena( attach )
: task_arena_base(automatic, 1) // use default settings if attach fails
, my_initialized(false)
{
internal_attach();
if( my_arena ) my_initialized = true;
}
//! Forces allocation of the resources for the task_arena as specified in constructor arguments
inline void initialize() {
if( !my_initialized ) {
internal_initialize();
mark_initialized();
}
}
//! Overrides concurrency level and forces initialization of internal representation
inline void initialize(int max_concurrency_, unsigned reserved_for_masters = 1) {
// TODO: decide if this call must be thread-safe
__TBB_ASSERT( !my_arena, "Impossible to modify settings of an already initialized task_arena");
if( !my_initialized ) {
my_max_concurrency = max_concurrency_;
my_master_slots = reserved_for_masters;
initialize();
}
}
//! Attaches this instance to the current arena of the thread
inline void initialize(attach) {
// TODO: decide if this call must be thread-safe
__TBB_ASSERT( !my_arena, "Impossible to modify settings of an already initialized task_arena");
if( !my_initialized ) {
internal_attach();
if( !my_arena ) internal_initialize();
mark_initialized();
}
}
//! Removes the reference to the internal arena representation.
//! Not thread safe wrt concurrent invocations of other methods.
inline void terminate() {
if( my_initialized ) {
internal_terminate();
my_initialized = false;
}
}
//! Removes the reference to the internal arena representation, and destroys the external object.
//! Not thread safe wrt concurrent invocations of other methods.
~task_arena() {
terminate();
}
//! Returns true if the arena is active (initialized); false otherwise.
//! The name was chosen to match a task_scheduler_init method with the same semantics.
bool is_active() const { return my_initialized; }
//! Enqueues a task into the arena to process a functor, and immediately returns.
//! Does not require the calling thread to join the arena
template<typename F>
void enqueue( const F& f ) {
initialize();
#if __TBB_TASK_GROUP_CONTEXT
internal_enqueue( *new( task::allocate_root(*my_context) ) internal::function_task<F>(f), 0 );
#else
internal_enqueue( *new( task::allocate_root() ) internal::function_task<F>(f), 0 );
#endif
}
#if __TBB_TASK_PRIORITY
//! Enqueues a task with priority p into the arena to process a functor f, and immediately returns.
//! Does not require the calling thread to join the arena
template<typename F>
void enqueue( const F& f, priority_t p ) {
__TBB_ASSERT( p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value" );
initialize();
#if __TBB_TASK_GROUP_CONTEXT
internal_enqueue( *new( task::allocate_root(*my_context) ) internal::function_task<F>(f), (intptr_t)p );
#else
internal_enqueue( *new( task::allocate_root() ) internal::function_task<F>(f), (intptr_t)p );
#endif
}
#endif// __TBB_TASK_PRIORITY
//! Joins the arena and executes a functor, then returns
//! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
//! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
template<typename F>
void execute(F& f) {
initialize();
internal::delegated_function<F> d(f);
internal_execute( d );
}
//! Joins the arena and executes a functor, then returns
//! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
//! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
template<typename F>
void execute(const F& f) {
initialize();
internal::delegated_function<const F> d(f);
internal_execute( d );
}
#if __TBB_EXTRA_DEBUG
//! Wait for all work in the arena to be completed
//! Even submitted by other application threads
//! Joins arena if/when possible (in the same way as execute())
void debug_wait_until_empty() {
initialize();
internal_wait();
}
#endif //__TBB_EXTRA_DEBUG
//! Returns the index, aka slot number, of the calling thread in its current arena
//! This method is deprecated and replaced with this_task_arena::current_thread_index()
inline static int current_thread_index() {
return internal_current_slot();
}
//! Returns the maximal number of threads that can work inside the arena
inline int max_concurrency() const {
// Handle special cases inside the library
return (my_max_concurrency>1) ? my_max_concurrency : internal_max_concurrency(this);
}
};
#if __TBB_TASK_ISOLATION
namespace this_task_arena {
template<typename F>
void isolate( const F& f ) {
internal::delegated_function<const F> d(f);
internal::isolate_within_arena( d );
}
}
#endif /* __TBB_TASK_ISOLATION */
} // namespace interfaceX
using interface7::task_arena;
#if __TBB_TASK_ISOLATION
namespace this_task_arena {
using namespace interface7::this_task_arena;
}
#endif /* __TBB_TASK_ISOLATION */
namespace this_task_arena {
//! Returns the index, aka slot number, of the calling thread in its current arena
inline int current_thread_index() {
int idx = tbb::task_arena::current_thread_index();
return idx == -1 ? tbb::task_arena::not_initialized : idx;
}
//! Returns the maximal number of threads that can work inside the arena
inline int max_concurrency() {
return tbb::task_arena::internal_max_concurrency(NULL);
}
} // namespace this_task_arena
} // namespace tbb
#endif /* __TBB_task_arena_H */
|