Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb_misc.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 
18 
19 */
20 
21 #ifndef _TBB_tbb_misc_H
22 #define _TBB_tbb_misc_H
23 
24 #include "tbb/tbb_stddef.h"
25 #include "tbb/tbb_machine.h"
26 #include "tbb/atomic.h" // For atomic_xxx definitions
27 
28 #if __linux__ || __FreeBSD__
29 #include <sys/param.h> // __FreeBSD_version
30 #if __FreeBSD_version >= 701000
31 #include <sys/cpuset.h>
32 #endif
33 #endif
34 
35 // Does the operating system have a system call to pin a thread to a set of OS processors?
36 #define __TBB_OS_AFFINITY_SYSCALL_PRESENT ((__linux__ && !__ANDROID__) || (__FreeBSD_version >= 701000))
37 // On IBM* Blue Gene* CNK nodes, the affinity API has restrictions that prevent its usability for TBB,
38 // and also sysconf(_SC_NPROCESSORS_ONLN) already takes process affinity into account.
39 #define __TBB_USE_OS_AFFINITY_SYSCALL (__TBB_OS_AFFINITY_SYSCALL_PRESENT && !__bg__)
40 
41 namespace tbb {
42 namespace internal {
43 
44 const size_t MByte = 1024*1024;
45 
46 #if __TBB_WIN8UI_SUPPORT
47 // In Win8UI mode, TBB uses a thread creation API that does not allow to specify the stack size.
48 // Still, the thread stack size value, either explicit or default, is used by the scheduler.
49 // So here we set the default value to match the platform's default of 1MB.
50 const size_t ThreadStackSize = 1*MByte;
51 #else
52 const size_t ThreadStackSize = (sizeof(uintptr_t) <= 4 ? 2 : 4 )*MByte;
53 #endif
54 
55 #ifndef __TBB_HardwareConcurrency
56 
59 
60 #else
61 
62 inline int AvailableHwConcurrency() {
63  int n = __TBB_HardwareConcurrency();
64  return n > 0 ? n : 1; // Fail safety strap
65 }
66 #endif /* __TBB_HardwareConcurrency */
67 
68 
69 #if _WIN32||_WIN64
70 
72 
73 int NumberOfProcessorGroups();
74 
76 int FindProcessorGroupIndex ( int processorIndex );
77 
79 void MoveThreadIntoProcessorGroup( void* hThread, int groupIndex );
80 
81 #endif /* _WIN32||_WIN64 */
82 
84 void handle_win_error( int error_code );
85 
87 void PrintVersion();
88 
90 void PrintExtraVersionInfo( const char* category, const char* format, ... );
91 
93 void PrintRMLVersionInfo( void* arg, const char* server_info );
94 
95 // For TBB compilation only; not to be used in public headers
96 #if defined(min) || defined(max)
97 #undef min
98 #undef max
99 #endif
100 
102 
105 template<typename T>
106 T min ( const T& val1, const T& val2 ) {
107  return val1 < val2 ? val1 : val2;
108 }
109 
111 
114 template<typename T>
115 T max ( const T& val1, const T& val2 ) {
116  return val1 < val2 ? val2 : val1;
117 }
118 
120 template<int > struct int_to_type {};
121 
122 //------------------------------------------------------------------------
123 // FastRandom
124 //------------------------------------------------------------------------
125 
127 unsigned GetPrime ( unsigned seed );
128 
130 
131 class FastRandom {
132 private:
133 #if __TBB_OLD_PRIMES_RNG
134  unsigned x, a;
135  static const unsigned c = 1;
136 #else
137  unsigned x, c;
138  static const unsigned a = 0x9e3779b1; // a big prime number
139 #endif //__TBB_OLD_PRIMES_RNG
140 public:
142  unsigned short get() {
143  return get(x);
144  }
146  unsigned short get( unsigned& seed ) {
147  unsigned short r = (unsigned short)(seed>>16);
148  __TBB_ASSERT(c&1, "c must be odd for big rng period");
149  seed = seed*a+c;
150  return r;
151  }
153  FastRandom( void* unique_ptr ) { init(uintptr_t(unique_ptr)); }
154  FastRandom( uint32_t seed) { init(seed); }
155  FastRandom( uint64_t seed) { init(seed); }
156  template <typename T>
157  void init( T seed ) {
158  init(seed,int_to_type<sizeof(seed)>());
159  }
160  void init( uint64_t seed , int_to_type<8> ) {
161  init(uint32_t((seed>>32)+seed), int_to_type<4>());
162  }
163  void init( uint32_t seed, int_to_type<4> ) {
164 #if __TBB_OLD_PRIMES_RNG
165  x = seed;
166  a = GetPrime( seed );
167 #else
168  // threads use different seeds for unique sequences
169  c = (seed|1)*0xba5703f5; // c must be odd, shuffle by a prime number
170  x = c^(seed>>1); // also shuffle x for the first get() invocation
171 #endif
172  }
173 };
174 
175 //------------------------------------------------------------------------
176 // Atomic extensions
177 //------------------------------------------------------------------------
178 
180 
181 template<typename T1, typename T2, class Pred>
182 T1 atomic_update ( tbb::atomic<T1>& dst, T2 newValue, Pred compare ) {
183  T1 oldValue = dst;
184  while ( compare(oldValue, newValue) ) {
185  if ( dst.compare_and_swap((T1)newValue, oldValue) == oldValue )
186  break;
187  oldValue = dst;
188  }
189  return oldValue;
190 }
191 
198 };
199 
201 
208 template <typename F>
209 void atomic_do_once ( const F& initializer, atomic<do_once_state>& state ) {
210  // tbb::atomic provides necessary acquire and release fences.
211  // The loop in the implementation is necessary to avoid race when thread T2
212  // that arrived in the middle of initialization attempt by another thread T1
213  // has just made initialization possible.
214  // In such a case T2 has to rely on T1 to initialize, but T1 may already be past
215  // the point where it can recognize the changed conditions.
216  while ( state != do_once_executed ) {
217  if( state == do_once_uninitialized ) {
219  run_initializer( initializer, state );
220  break;
221  }
222  }
224  }
225 }
226 
227 // Run the initializer which can not fail
228 inline void run_initializer( void (*f)(), atomic<do_once_state>& state ) {
229  f();
230  state = do_once_executed;
231 }
232 
233 // Run the initializer which can require repeated call
234 inline void run_initializer( bool (*f)(), atomic<do_once_state>& state ) {
235  state = f() ? do_once_executed : do_once_uninitialized;
236 }
237 
238 #if __TBB_USE_OS_AFFINITY_SYSCALL
239  #if __linux__
240  typedef cpu_set_t basic_mask_t;
241  #elif __FreeBSD_version >= 701000
242  typedef cpuset_t basic_mask_t;
243  #else
244  #error affinity_helper is not implemented in this OS
245  #endif
246  class affinity_helper : no_copy {
247  basic_mask_t* threadMask;
248  int is_changed;
249  public:
250  affinity_helper() : threadMask(NULL), is_changed(0) {}
251  ~affinity_helper();
252  void protect_affinity_mask( bool restore_process_mask );
253  void dismiss();
254  };
255  void destroy_process_mask();
256 #else
258  public:
259  void protect_affinity_mask( bool ) {}
260  void dismiss() {}
261  };
262  inline void destroy_process_mask(){}
263 #endif /* __TBB_USE_OS_AFFINITY_SYSCALL */
264 
265 bool cpu_has_speculation();
267 void fix_broken_rethrow();
268 
269 } // namespace internal
270 } // namespace tbb
271 
272 #endif /* _TBB_tbb_misc_H */
bool gcc_rethrow_exception_broken()
Definition: tbb_misc.cpp:189
Primary template for atomic.
Definition: atomic.h:407
void run_initializer(void(*f)(), atomic< do_once_state > &state)
Definition: tbb_misc.h:228
do_once_state
One-time initialization states.
Definition: tbb_misc.h:193
Utility helper structure to ease overload resolution.
Definition: tbb_misc.h:120
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
FastRandom(uint64_t seed)
Definition: tbb_misc.h:155
void PrintRMLVersionInfo(void *arg, const char *server_info)
A callback routine to print RML version information on stderr.
Definition: tbb_misc.cpp:213
#define __TBB_HardwareConcurrency()
Definition: macos_common.h:43
void init(uint64_t seed, int_to_type< 8 >)
Definition: tbb_misc.h:160
T min(const T &val1, const T &val2)
Utility template function returning lesser of the two values.
Definition: tbb_misc.h:106
void PrintExtraVersionInfo(const char *category, const char *format,...)
Prints arbitrary extra TBB version information on stderr.
Definition: tbb_misc.cpp:202
void handle_win_error(int error_code)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info...
Do-once routine has been executed.
Definition: tbb_misc.h:196
const size_t ThreadStackSize
Definition: tbb_misc.h:52
FastRandom(void *unique_ptr)
Construct a random number generator.
Definition: tbb_misc.h:153
const size_t MByte
Definition: tbb_misc.h:44
T1 atomic_update(tbb::atomic< T1 > &dst, T2 newValue, Pred compare)
Atomically replaces value of dst with newValue if they satisfy condition of compare predicate...
Definition: tbb_misc.h:182
T max(const T &val1, const T &val2)
Utility template function returning greater of the two values.
Definition: tbb_misc.h:115
void PrintVersion()
Prints TBB version information on stderr.
Definition: tbb_misc.cpp:197
void fix_broken_rethrow()
Definition: tbb_misc.cpp:188
value_type compare_and_swap(value_type value, value_type comparand)
Definition: atomic.h:289
The graph class.
void destroy_process_mask()
Definition: tbb_misc.h:262
void spin_wait_while_eq(const volatile T &location, U value)
Spin WHILE the value of the variable is equal to a given value.
Definition: tbb_machine.h:398
int AvailableHwConcurrency()
Returns maximal parallelism level supported by the current OS configuration.
void atomic_do_once(const F &initializer, atomic< do_once_state > &state)
One-time initialization function.
Definition: tbb_misc.h:209
unsigned GetPrime(unsigned seed)
bool cpu_has_speculation()
check for transaction support.
Definition: tbb_misc.cpp:221
No execution attempts have been undertaken yet.
Definition: tbb_misc.h:194
FastRandom(uint32_t seed)
Definition: tbb_misc.h:154
void init(uint32_t seed, int_to_type< 4 >)
Definition: tbb_misc.h:163
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:335
A thread is executing associated do-once routine.
Definition: tbb_misc.h:195
A fast random number generator.
Definition: tbb_misc.h:131

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.