2 Copyright (c) 2005-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _TBB_governor_H
18 #define _TBB_governor_H
20 #include "tbb/task_scheduler_init.h"
21 #include "../rml/include/rml_tbb.h"
23 #include "tbb_misc.h" // for AvailableHwConcurrency
26 #if __TBB_SURVIVE_THREAD_SWITCH
27 #include "cilk-tbb-interop.h"
28 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
34 class generic_scheduler;
41 //------------------------------------------------------------------------
43 //------------------------------------------------------------------------
45 //! The class handles access to the single instance of market, and to TLS to keep scheduler instances.
46 /** It also supports automatic on-demand initialization of the TBB scheduler.
47 The class contains only static data members and methods.*/
50 friend class __TBB_InitOnce;
53 //! TLS for scheduler instances associated with individual threads
54 static basic_tls<uintptr_t> theTLS;
56 //! Caches the maximal level of parallelism supported by the hardware
57 static unsigned DefaultNumberOfThreads;
59 //! Caches the size of OS regular memory page
60 static size_t DefaultPageSize;
62 static rml::tbb_factory theRMLServerFactory;
64 static bool UsePrivateRML;
66 // Flags for runtime-specific conditions
67 static bool is_speculation_enabled;
68 static bool is_rethrow_broken;
70 //! Create key for thread-local storage and initialize RML.
71 static void acquire_resources ();
73 //! Destroy the thread-local storage key and deinitialize RML.
74 static void release_resources ();
76 static rml::tbb_server* create_rml_server ( rml::tbb_client& );
78 //! The internal routine to undo automatic initialization.
79 /** The signature is written with void* so that the routine
80 can be the destructor argument to pthread_key_create. */
81 static void auto_terminate(void* scheduler);
84 static unsigned default_num_threads () {
85 // No memory fence required, because at worst each invoking thread calls AvailableHwConcurrency once.
86 return DefaultNumberOfThreads ? DefaultNumberOfThreads :
87 DefaultNumberOfThreads = AvailableHwConcurrency();
89 static size_t default_page_size () {
90 return DefaultPageSize ? DefaultPageSize :
91 DefaultPageSize = DefaultSystemPageSize();
93 static void one_time_init();
94 //! Processes scheduler initialization request (possibly nested) in a master thread
95 /** If necessary creates new instance of arena and/or local scheduler.
96 The auto_init argument specifies if the call is due to automatic initialization. **/
97 static generic_scheduler* init_scheduler( int num_threads, stack_size_type stack_size, bool auto_init );
99 //! Automatic initialization of scheduler in a master thread with default settings without arena
100 static generic_scheduler* init_scheduler_weak();
102 //! Processes scheduler termination request (possibly nested) in a master thread
103 static bool terminate_scheduler( generic_scheduler* s, bool blocking );
105 //! Register TBB scheduler instance in thread-local storage.
106 static void sign_on( generic_scheduler* s );
108 //! Unregister TBB scheduler instance from thread-local storage.
109 static void sign_off( generic_scheduler* s );
111 //! Used to check validity of the local scheduler TLS contents.
112 static bool is_set( generic_scheduler* s );
114 //! Temporarily set TLS slot to the given scheduler
115 static void assume_scheduler( generic_scheduler* s );
117 //! Computes the value of the TLS
118 static uintptr_t tls_value_of( generic_scheduler* s );
120 // TODO IDEA: refactor bit manipulations over pointer types to a class?
121 //! Converts TLS value to the scheduler pointer
122 static generic_scheduler* tls_scheduler_of( uintptr_t v ) {
123 return (generic_scheduler*)(v & ~uintptr_t(1));
126 //! Obtain the thread-local instance of the TBB scheduler.
127 /** If the scheduler has not been initialized yet, initialization is done automatically.
128 Note that auto-initialized scheduler instance is destroyed only when its thread terminates. **/
129 static generic_scheduler* local_scheduler () {
130 uintptr_t v = theTLS.get();
131 return (v&1) ? tls_scheduler_of(v) : init_scheduler( task_scheduler_init::automatic, 0, /*auto_init=*/true );
134 static generic_scheduler* local_scheduler_weak () {
135 uintptr_t v = theTLS.get();
136 return v ? tls_scheduler_of(v) : init_scheduler_weak();
139 static generic_scheduler* local_scheduler_if_initialized () {
140 return tls_scheduler_of( theTLS.get() );
143 //! Undo automatic initialization if necessary; call when a thread exits.
144 static void terminate_auto_initialized_scheduler() {
145 auto_terminate( local_scheduler_if_initialized() );
148 static void print_version_info ();
150 static void initialize_rml_factory ();
152 static bool does_client_join_workers (const tbb::internal::rml::tbb_client &client);
154 #if __TBB_SURVIVE_THREAD_SWITCH
155 static __cilk_tbb_retcode stack_op_handler( __cilk_tbb_stack_op op, void* );
156 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
158 static bool speculation_enabled() { return is_speculation_enabled; }
159 static bool rethrow_exception_broken() { return is_rethrow_broken; }
163 } // namespace internal
166 #endif /* _TBB_governor_H */