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

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.