1 /*
2     Copyright (c) 2005-2021 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_governor_H
18 #define _TBB_governor_H
19 
20 #include "rml_tbb.h"
21 
22 #include "misc.h" // for AvailableHwConcurrency
23 #include "tls.h"
24 
25 namespace tbb {
26 namespace detail {
27 namespace r1 {
28 
29 class market;
30 class thread_data;
31 class __TBB_InitOnce;
32 
33 #if __TBB_USE_ITT_NOTIFY
34 //! Defined in profiling.cpp
35 extern bool ITT_Present;
36 #endif
37 
38 typedef std::size_t stack_size_type;
39 
40 //------------------------------------------------------------------------
41 // Class governor
42 //------------------------------------------------------------------------
43 
44 //! The class handles access to the single instance of market, and to TLS to keep scheduler instances.
45 /** It also supports automatic on-demand initialization of the TBB scheduler.
46     The class contains only static data members and methods.*/
47 class governor {
48 private:
49     friend class __TBB_InitOnce;
50     friend class market;
51 
52     // TODO: consider using thread_local (measure performance and side effects)
53     //! TLS for scheduler instances associated with individual threads
54     static basic_tls<thread_data*> theTLS;
55 
56     // TODO (TBB_REVAMP_TODO): reconsider constant names
57     static rml::tbb_factory theRMLServerFactory;
58 
59     static bool UsePrivateRML;
60 
61     // Flags for runtime-specific conditions
62     static cpu_features_type cpu_features;
63     static bool is_rethrow_broken;
64 
65     //! Create key for thread-local storage and initialize RML.
66     static void acquire_resources ();
67 
68     //! Destroy the thread-local storage key and deinitialize RML.
69     static void release_resources ();
70 
71     static rml::tbb_server* create_rml_server ( rml::tbb_client& );
72 
73 public:
default_num_threads()74     static unsigned default_num_threads () {
75         // Caches the maximal level of parallelism supported by the hardware
76         static unsigned num_threads = AvailableHwConcurrency();
77         return num_threads;
78     }
default_page_size()79     static std::size_t default_page_size () {
80         // Caches the size of OS regular memory page
81         static std::size_t page_size = DefaultSystemPageSize();
82         return page_size;
83     }
84     static void one_time_init();
85     //! Processes scheduler initialization request (possibly nested) in an external thread
86     /** If necessary creates new instance of arena and/or local scheduler.
87         The auto_init argument specifies if the call is due to automatic initialization. **/
88     static void init_external_thread();
89 
90     //! The routine to undo automatic initialization.
91     /** The signature is written with void* so that the routine
92         can be the destructor argument to pthread_key_create. */
93     static void auto_terminate(void* tls);
94 
95     //! Obtain the thread-local instance of the thread data.
96     /** If the scheduler has not been initialized yet, initialization is done automatically.
97         Note that auto-initialized scheduler instance is destroyed only when its thread terminates. **/
get_thread_data()98     static thread_data* get_thread_data() {
99         thread_data* td = theTLS.get();
100         if (td) {
101             return td;
102         }
103         init_external_thread();
104         td = theTLS.get();
105         __TBB_ASSERT(td, NULL);
106         return td;
107     }
108 
set_thread_data(thread_data & td)109     static void set_thread_data(thread_data& td) {
110         theTLS.set(&td);
111     }
112 
clear_thread_data()113     static void clear_thread_data() {
114         theTLS.set(nullptr);
115     }
116 
get_thread_data_if_initialized()117     static thread_data* get_thread_data_if_initialized () {
118         return theTLS.get();
119     }
120 
is_thread_data_set(thread_data * td)121     static bool is_thread_data_set(thread_data* td) {
122         return theTLS.get() == td;
123     }
124 
125     //! Undo automatic initialization if necessary; call when a thread exits.
terminate_external_thread()126     static void terminate_external_thread() {
127         auto_terminate(get_thread_data_if_initialized());
128     }
129 
130     static void initialize_rml_factory ();
131 
132     static bool does_client_join_workers (const rml::tbb_client &client);
133 
speculation_enabled()134     static bool speculation_enabled() { return cpu_features.rtm_enabled; }
135 
136 #if __TBB_WAITPKG_INTRINSICS_PRESENT
wait_package_enabled()137     static bool wait_package_enabled() { return cpu_features.waitpkg_enabled; }
138 #endif
139 
rethrow_exception_broken()140     static bool rethrow_exception_broken() { return is_rethrow_broken; }
141 
is_itt_present()142     static bool is_itt_present() {
143 #if __TBB_USE_ITT_NOTIFY
144         return ITT_Present;
145 #else
146         return false;
147 #endif
148     }
149 }; // class governor
150 
151 } // namespace r1
152 } // namespace detail
153 } // namespace tbb
154 
155 #endif /* _TBB_governor_H */
156