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 && (_WIN32_WINNT < 0x0A00)
47 // In Win8UI mode (Windows 8 Store* applications), TBB uses a thread creation API
48 // that does not allow to specify the stack size.
49 // Still, the thread stack size value, either explicit or default, is used by the scheduler.
50 // So here we set the default value to match the platform's default of 1MB.
51 const size_t ThreadStackSize = 1*MByte;
52 #else
53 const size_t ThreadStackSize = (sizeof(uintptr_t) <= 4 ? 2 : 4 )*MByte;
54 #endif
55 
56 #ifndef __TBB_HardwareConcurrency
57 
60 
61 #else
62 
63 inline int AvailableHwConcurrency() {
64  int n = __TBB_HardwareConcurrency();
65  return n > 0 ? n : 1; // Fail safety strap
66 }
67 #endif /* __TBB_HardwareConcurrency */
68 
69 
70 #if _WIN32||_WIN64
71 
73 
74 int NumberOfProcessorGroups();
75 
77 int FindProcessorGroupIndex ( int processorIndex );
78 
80 void MoveThreadIntoProcessorGroup( void* hThread, int groupIndex );
81 
82 #endif /* _WIN32||_WIN64 */
83 
85 void handle_win_error( int error_code );
86 
88 void PrintVersion();
89 
91 void PrintExtraVersionInfo( const char* category, const char* format, ... );
92 
94 void PrintRMLVersionInfo( void* arg, const char* server_info );
95 
96 // For TBB compilation only; not to be used in public headers
97 #if defined(min) || defined(max)
98 #undef min
99 #undef max
100 #endif
101 
103 
106 template<typename T>
107 T min ( const T& val1, const T& val2 ) {
108  return val1 < val2 ? val1 : val2;
109 }
110 
112 
115 template<typename T>
116 T max ( const T& val1, const T& val2 ) {
117  return val1 < val2 ? val2 : val1;
118 }
119 
121 template<int > struct int_to_type {};
122 
123 //------------------------------------------------------------------------
124 // FastRandom
125 //------------------------------------------------------------------------
126 
128 unsigned GetPrime ( unsigned seed );
129 
131 
132 class FastRandom {
133 private:
134 #if __TBB_OLD_PRIMES_RNG
135  unsigned x, a;
136  static const unsigned c = 1;
137 #else
138  unsigned x, c;
139  static const unsigned a = 0x9e3779b1; // a big prime number
140 #endif //__TBB_OLD_PRIMES_RNG
141 public:
143  unsigned short get() {
144  return get(x);
145  }
147  unsigned short get( unsigned& seed ) {
148  unsigned short r = (unsigned short)(seed>>16);
149  __TBB_ASSERT(c&1, "c must be odd for big rng period");
150  seed = seed*a+c;
151  return r;
152  }
154  FastRandom( void* unique_ptr ) { init(uintptr_t(unique_ptr)); }
155  FastRandom( uint32_t seed) { init(seed); }
156  FastRandom( uint64_t seed) { init(seed); }
157  template <typename T>
158  void init( T seed ) {
159  init(seed,int_to_type<sizeof(seed)>());
160  }
161  void init( uint64_t seed , int_to_type<8> ) {
162  init(uint32_t((seed>>32)+seed), int_to_type<4>());
163  }
164  void init( uint32_t seed, int_to_type<4> ) {
165 #if __TBB_OLD_PRIMES_RNG
166  x = seed;
167  a = GetPrime( seed );
168 #else
169  // threads use different seeds for unique sequences
170  c = (seed|1)*0xba5703f5; // c must be odd, shuffle by a prime number
171  x = c^(seed>>1); // also shuffle x for the first get() invocation
172 #endif
173  }
174 };
175 
176 //------------------------------------------------------------------------
177 // Atomic extensions
178 //------------------------------------------------------------------------
179 
181 
182 template<typename T1, typename T2, class Pred>
183 T1 atomic_update ( tbb::atomic<T1>& dst, T2 newValue, Pred compare ) {
184  T1 oldValue = dst;
185  while ( compare(oldValue, newValue) ) {
186  if ( dst.compare_and_swap((T1)newValue, oldValue) == oldValue )
187  break;
188  oldValue = dst;
189  }
190  return oldValue;
191 }
192 
199 };
200 
202 
209 template <typename F>
210 void atomic_do_once ( const F& initializer, atomic<do_once_state>& state ) {
211  // tbb::atomic provides necessary acquire and release fences.
212  // The loop in the implementation is necessary to avoid race when thread T2
213  // that arrived in the middle of initialization attempt by another thread T1
214  // has just made initialization possible.
215  // In such a case T2 has to rely on T1 to initialize, but T1 may already be past
216  // the point where it can recognize the changed conditions.
217  while ( state != do_once_executed ) {
218  if( state == do_once_uninitialized ) {
220  run_initializer( initializer, state );
221  break;
222  }
223  }
225  }
226 }
227 
228 // Run the initializer which can not fail
229 inline void run_initializer( void (*f)(), atomic<do_once_state>& state ) {
230  f();
231  state = do_once_executed;
232 }
233 
234 // Run the initializer which can require repeated call
235 inline void run_initializer( bool (*f)(), atomic<do_once_state>& state ) {
236  state = f() ? do_once_executed : do_once_uninitialized;
237 }
238 
239 #if __TBB_USE_OS_AFFINITY_SYSCALL
240  #if __linux__
241  typedef cpu_set_t basic_mask_t;
242  #elif __FreeBSD_version >= 701000
243  typedef cpuset_t basic_mask_t;
244  #else
245  #error affinity_helper is not implemented in this OS
246  #endif
247  class affinity_helper : no_copy {
248  basic_mask_t* threadMask;
249  int is_changed;
250  public:
251  affinity_helper() : threadMask(NULL), is_changed(0) {}
252  ~affinity_helper();
253  void protect_affinity_mask( bool restore_process_mask );
254  void dismiss();
255  };
256  void destroy_process_mask();
257 #else
259  public:
260  void protect_affinity_mask( bool ) {}
261  void dismiss() {}
262  };
263  inline void destroy_process_mask(){}
264 #endif /* __TBB_USE_OS_AFFINITY_SYSCALL */
265 
266 bool cpu_has_speculation();
268 void fix_broken_rethrow();
269 
270 } // namespace internal
271 } // namespace tbb
272 
273 #endif /* _TBB_tbb_misc_H */
bool gcc_rethrow_exception_broken()
Definition: tbb_misc.cpp:189
void run_initializer(void(*f)(), atomic< do_once_state > &state)
Definition: tbb_misc.h:229
do_once_state
One-time initialization states.
Definition: tbb_misc.h:194
FastRandom(uint64_t seed)
Definition: tbb_misc.h:156
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
A fast random number generator.
Definition: tbb_misc.h:132
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
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:335
T min(const T &val1, const T &val2)
Utility template function returning lesser of the two values.
Definition: tbb_misc.h:107
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:197
const size_t ThreadStackSize
Definition: tbb_misc.h:53
void init(uint64_t seed, int_to_type< 8 >)
Definition: tbb_misc.h:161
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:183
T max(const T &val1, const T &val2)
Utility template function returning greater of the two values.
Definition: tbb_misc.h:116
void init(uint32_t seed, int_to_type< 4 >)
Definition: tbb_misc.h:164
void PrintVersion()
Prints TBB version information on stderr.
Definition: tbb_misc.cpp:197
void fix_broken_rethrow()
Definition: tbb_misc.cpp:188
The graph class.
unsigned short get()
Get a random number.
Definition: tbb_misc.h:143
void destroy_process_mask()
Definition: tbb_misc.h:263
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:395
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:210
Primary template for atomic.
Definition: atomic.h:407
FastRandom(void *unique_ptr)
Construct a random number generator.
Definition: tbb_misc.h:154
unsigned GetPrime(unsigned seed)
bool cpu_has_speculation()
check for transaction support.
Definition: tbb_misc.cpp:221
Utility helper structure to ease overload resolution.
Definition: tbb_misc.h:121
value_type compare_and_swap(value_type value, value_type comparand)
Definition: atomic.h:289
FastRandom(uint32_t seed)
Definition: tbb_misc.h:155
No execution attempts have been undertaken yet.
Definition: tbb_misc.h:195
unsigned short get(unsigned &seed)
Get a random number for the given seed; update the seed for next use.
Definition: tbb_misc.h:147
A thread is executing associated do-once routine.
Definition: tbb_misc.h:196
static const unsigned a
Definition: tbb_misc.h:139

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.