Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
governor.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2018 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_governor_H
22 #define _TBB_governor_H
23 
25 #include "../rml/include/rml_tbb.h"
26 
27 #include "tbb_misc.h" // for AvailableHwConcurrency
28 #include "tls.h"
29 
30 #if __TBB_SURVIVE_THREAD_SWITCH
31 #include "cilk-tbb-interop.h"
32 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
33 
34 namespace tbb {
35 namespace internal {
36 
37 class market;
38 class generic_scheduler;
39 class __TBB_InitOnce;
40 
41 namespace rml {
42 class tbb_client;
43 }
44 
45 //------------------------------------------------------------------------
46 // Class governor
47 //------------------------------------------------------------------------
48 
50 
52 class governor {
53 private:
54  friend class __TBB_InitOnce;
55  friend class market;
56 
59 
61  static unsigned DefaultNumberOfThreads;
62 
63  static rml::tbb_factory theRMLServerFactory;
64 
65  static bool UsePrivateRML;
66 
67  // Flags for runtime-specific conditions
69  static bool is_rethrow_broken;
70 
72  static void acquire_resources ();
73 
75  static void release_resources ();
76 
77  static rml::tbb_server* create_rml_server ( rml::tbb_client& );
78 
80 
82  static void auto_terminate(void* scheduler);
83 
84 public:
85  static unsigned default_num_threads () {
86  // No memory fence required, because at worst each invoking thread calls AvailableHwConcurrency once.
89  }
90  static void one_time_init();
92 
94  static generic_scheduler* init_scheduler( int num_threads, stack_size_type stack_size, bool auto_init );
95 
98 
100  static bool terminate_scheduler( generic_scheduler* s, bool blocking );
101 
103  static void sign_on( generic_scheduler* s );
104 
106  static void sign_off( generic_scheduler* s );
107 
109  static bool is_set( generic_scheduler* s );
110 
112  static void assume_scheduler( generic_scheduler* s );
113 
115  static uintptr_t tls_value_of( generic_scheduler* s );
116 
117  // TODO IDEA: refactor bit manipulations over pointer types to a class?
119  static generic_scheduler* tls_scheduler_of( uintptr_t v ) {
120  return (generic_scheduler*)(v & ~uintptr_t(1));
121  }
122 
124 
127  uintptr_t v = theTLS.get();
128  return (v&1) ? tls_scheduler_of(v) : init_scheduler( task_scheduler_init::automatic, 0, /*auto_init=*/true );
129  }
130 
132  uintptr_t v = theTLS.get();
133  return v ? tls_scheduler_of(v) : init_scheduler_weak();
134  }
135 
137  return tls_scheduler_of( theTLS.get() );
138  }
139 
143  }
144 
145  static void print_version_info ();
146 
147  static void initialize_rml_factory ();
148 
149  static bool does_client_join_workers (const tbb::internal::rml::tbb_client &client);
150 
151 #if __TBB_SURVIVE_THREAD_SWITCH
152  static __cilk_tbb_retcode stack_op_handler( __cilk_tbb_stack_op op, void* );
153 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
154 
155  static bool speculation_enabled() { return is_speculation_enabled; }
156  static bool rethrow_exception_broken() { return is_rethrow_broken; }
157 
158 }; // class governor
159 
160 } // namespace internal
161 } // namespace tbb
162 
163 #endif /* _TBB_governor_H */
int AvailableHwConcurrency()
Returns maximal parallelism level supported by the current OS configuration.
static void sign_on(generic_scheduler *s)
Register TBB scheduler instance in thread-local storage.
Definition: governor.cpp:128
static bool rethrow_exception_broken()
Definition: governor.h:156
static unsigned default_num_threads()
Definition: governor.h:85
Work stealing task scheduler.
Definition: scheduler.h:115
void const char const char int ITT_FORMAT __itt_group_sync s
static bool does_client_join_workers(const tbb::internal::rml::tbb_client &client)
Definition: market.cpp:296
static void release_resources()
Destroy the thread-local storage key and deinitialize RML.
Definition: governor.cpp:83
static bool is_rethrow_broken
Definition: governor.h:69
static generic_scheduler * init_scheduler(int num_threads, stack_size_type stack_size, bool auto_init)
Processes scheduler initialization request (possibly nested) in a master thread.
Definition: governor.cpp:176
static unsigned DefaultNumberOfThreads
Caches the maximal level of parallelism supported by the hardware.
Definition: governor.h:61
static const int automatic
Typedef for number of threads that is automatic.
static bool speculation_enabled()
Definition: governor.h:155
static void print_version_info()
Definition: governor.cpp:231
static bool is_speculation_enabled
Definition: governor.h:68
static generic_scheduler * local_scheduler()
Obtain the thread-local instance of the TBB scheduler.
Definition: governor.h:126
static void assume_scheduler(generic_scheduler *s)
Temporarily set TLS slot to the given scheduler.
Definition: governor.cpp:120
static generic_scheduler * local_scheduler_if_initialized()
Definition: governor.h:136
static bool is_set(generic_scheduler *s)
Used to check validity of the local scheduler TLS contents.
Definition: governor.cpp:124
static generic_scheduler * local_scheduler_weak()
Definition: governor.h:131
static bool terminate_scheduler(generic_scheduler *s, bool blocking)
Processes scheduler termination request (possibly nested) in a master thread.
Definition: governor.cpp:207
static rml::tbb_server * create_rml_server(rml::tbb_client &)
Definition: governor.cpp:96
std::size_t stack_size_type
__cilk_tbb_stack_op
static void auto_terminate(void *scheduler)
The internal routine to undo automatic initialization.
Definition: governor.cpp:217
static void acquire_resources()
Create key for thread-local storage and initialize RML.
Definition: governor.cpp:71
Class that supports TBB initialization.
Definition: tbb_main.h:40
static rml::tbb_factory theRMLServerFactory
Definition: governor.h:63
static generic_scheduler * init_scheduler_weak()
Automatic initialization of scheduler in a master thread with default settings without arena.
Definition: governor.cpp:168
static basic_tls< uintptr_t > theTLS
TLS for scheduler instances associated with individual threads.
Definition: governor.h:58
The graph class.
static generic_scheduler * tls_scheduler_of(uintptr_t v)
Converts TLS value to the scheduler pointer.
Definition: governor.h:119
static bool UsePrivateRML
Definition: governor.h:65
static void sign_off(generic_scheduler *s)
Unregister TBB scheduler instance from thread-local storage.
Definition: governor.cpp:149
static uintptr_t tls_value_of(generic_scheduler *s)
Computes the value of the TLS.
Definition: governor.cpp:114
static void one_time_init()
Definition: governor.cpp:160
int __cilk_tbb_retcode
The class handles access to the single instance of market, and to TLS to keep scheduler instances.
Definition: governor.h:52
static void initialize_rml_factory()
Definition: governor.cpp:244
static void terminate_auto_initialized_scheduler()
Undo automatic initialization if necessary; call when a thread exits.
Definition: governor.h:141

Copyright © 2005-2018 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.