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-2019 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 
21 #include "../rml/include/rml_tbb.h"
22 
23 #include "tbb_misc.h" // for AvailableHwConcurrency
24 #include "tls.h"
25 
26 #if __TBB_SURVIVE_THREAD_SWITCH
27 #include "cilk-tbb-interop.h"
28 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
29 
30 namespace tbb {
31 namespace internal {
32 
33 class market;
34 class generic_scheduler;
35 class __TBB_InitOnce;
36 
37 namespace rml {
38 class tbb_client;
39 }
40 
41 //------------------------------------------------------------------------
42 // Class governor
43 //------------------------------------------------------------------------
44 
46 
48 class governor {
49 private:
50  friend class __TBB_InitOnce;
51  friend class market;
52 
55 
57  static unsigned DefaultNumberOfThreads;
58 
59  static rml::tbb_factory theRMLServerFactory;
60 
61  static bool UsePrivateRML;
62 
63  // Flags for runtime-specific conditions
65  static bool is_rethrow_broken;
66 
68  static void acquire_resources ();
69 
71  static void release_resources ();
72 
73  static rml::tbb_server* create_rml_server ( rml::tbb_client& );
74 
76 
78  static void auto_terminate(void* scheduler);
79 
80 public:
81  static unsigned default_num_threads () {
82  // No memory fence required, because at worst each invoking thread calls AvailableHwConcurrency once.
83  return DefaultNumberOfThreads ? DefaultNumberOfThreads :
84  DefaultNumberOfThreads = AvailableHwConcurrency();
85  }
86  static void one_time_init();
88 
90  static generic_scheduler* init_scheduler( int num_threads, stack_size_type stack_size, bool auto_init );
91 
93  static generic_scheduler* init_scheduler_weak();
94 
96  static bool terminate_scheduler( generic_scheduler* s, bool blocking );
97 
99  static void sign_on( generic_scheduler* s );
100 
102  static void sign_off( generic_scheduler* s );
103 
105  static bool is_set( generic_scheduler* s );
106 
108  static void assume_scheduler( generic_scheduler* s );
109 
111  static uintptr_t tls_value_of( generic_scheduler* s );
112 
113  // TODO IDEA: refactor bit manipulations over pointer types to a class?
115  static generic_scheduler* tls_scheduler_of( uintptr_t v ) {
116  return (generic_scheduler*)(v & ~uintptr_t(1));
117  }
118 
120 
123  uintptr_t v = theTLS.get();
124  return (v&1) ? tls_scheduler_of(v) : init_scheduler( task_scheduler_init::automatic, 0, /*auto_init=*/true );
125  }
126 
128  uintptr_t v = theTLS.get();
129  return v ? tls_scheduler_of(v) : init_scheduler_weak();
130  }
131 
133  return tls_scheduler_of( theTLS.get() );
134  }
135 
138  auto_terminate( local_scheduler_if_initialized() );
139  }
140 
141  static void print_version_info ();
142 
143  static void initialize_rml_factory ();
144 
145  static bool does_client_join_workers (const tbb::internal::rml::tbb_client &client);
146 
147 #if __TBB_SURVIVE_THREAD_SWITCH
148  static __cilk_tbb_retcode stack_op_handler( __cilk_tbb_stack_op op, void* );
149 #endif /* __TBB_SURVIVE_THREAD_SWITCH */
150 
151  static bool speculation_enabled() { return is_speculation_enabled; }
152  static bool rethrow_exception_broken() { return is_rethrow_broken; }
153 
154 }; // class governor
155 
156 } // namespace internal
157 } // namespace tbb
158 
159 #endif /* _TBB_governor_H */
int AvailableHwConcurrency()
Returns maximal parallelism level supported by the current OS configuration.
static bool is_speculation_enabled
Definition: governor.h:64
void const char const char int ITT_FORMAT __itt_group_sync s
static basic_tls< uintptr_t > theTLS
TLS for scheduler instances associated with individual threads.
Definition: governor.h:54
__cilk_tbb_stack_op
static generic_scheduler * local_scheduler_weak()
Definition: governor.h:127
The graph class.
static const int automatic
Typedef for number of threads that is automatic.
static bool is_rethrow_broken
Definition: governor.h:65
static generic_scheduler * local_scheduler()
Obtain the thread-local instance of the TBB scheduler.
Definition: governor.h:122
Class that supports TBB initialization.
Definition: tbb_main.h:37
The class handles access to the single instance of market, and to TLS to keep scheduler instances...
Definition: governor.h:48
static generic_scheduler * local_scheduler_if_initialized()
Definition: governor.h:132
static bool speculation_enabled()
Definition: governor.h:151
static void terminate_auto_initialized_scheduler()
Undo automatic initialization if necessary; call when a thread exits.
Definition: governor.h:137
static bool rethrow_exception_broken()
Definition: governor.h:152
Work stealing task scheduler.
Definition: scheduler.h:120
static bool UsePrivateRML
Definition: governor.h:61
static unsigned default_num_threads()
Definition: governor.h:81
int __cilk_tbb_retcode
std::size_t stack_size_type
static unsigned DefaultNumberOfThreads
Caches the maximal level of parallelism supported by the hardware.
Definition: governor.h:57
static generic_scheduler * tls_scheduler_of(uintptr_t v)
Converts TLS value to the scheduler pointer.
Definition: governor.h:115
static rml::tbb_factory theRMLServerFactory
Definition: governor.h:59

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