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