00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _TBB_tbb_misc_H
00030 #define _TBB_tbb_misc_H
00031
00032 #include "tbb/tbb_stddef.h"
00033 #include "tbb/tbb_machine.h"
00034
00035 #if __linux__
00036 #include
00037 #elif __APPLE__
00038 #include
00039 #include
00040 #endif
00041
00042 namespace tbb {
00043
00044 static volatile int number_of_workers = 0;
00045
00046 #if defined(__TBB_DetectNumberOfWorkers)
00047 static inline int DetectNumberOfWorkers() {
00048 return __TBB_DetectNumberOfWorkers();
00049 }
00050 #else
00051 #if _WIN32||_WIN64
00052
00053 static inline int DetectNumberOfWorkers() {
00054 if (!number_of_workers) {
00055 SYSTEM_INFO si;
00056 GetSystemInfo(&si);
00057 number_of_workers = static_castint>(si.dwNumberOfProcessors);
00058 }
00059 return number_of_workers;
00060 }
00061
00062 #elif __linux__
00063
00064 static inline int DetectNumberOfWorkers( void ) {
00065 if (!number_of_workers) {
00066 number_of_workers = get_nprocs();
00067 }
00068 return number_of_workers;
00069 }
00070
00071 #elif __APPLE__
00072
00073 static inline int DetectNumberOfWorkers( void ) {
00074 if (!number_of_workers) {
00075 int name[2] = {CTL_HW, HW_AVAILCPU};
00076 int ncpu;
00077 size_t size = sizeof(ncpu);
00078 sysctl( name, 2, &ncpu, &size, NULL, 0 );
00079 number_of_workers = ncpu;
00080 }
00081 return number_of_workers;
00082 }
00083
00084 #else
00085
00086 #error Unknown OS
00087
00088 #endif
00089
00090 #endif
00091
00092 namespace internal {
00093
00094
00095
00096
00098
00099
00100
00101
00102
00103
00104
00105 void handle_perror( int error_code, const char* what );
00106
00108 bool GetBoolEnvironmentVariable( const char * name );
00109
00111 void PrintVersion();
00112
00114 void PrintExtraVersionInfo( const char* category, const char* description );
00115
00116 typedef void (*PointerToHandler)();
00117
00119 struct DynamicLinkDescriptor {
00121 const char* name;
00123 PointerToHandler* handler;
00124 };
00125
00127
00130 bool FillDynamicLinks( const char* libraryname, const DynamicLinkDescriptor list[], size_t n );
00131
00133
00137 templatetypename T>
00138 static inline T volatile& volatile_cast(T& location) {
00139 return const_castT volatile&>(location);
00140 }
00141
00142 templatetypename T>
00143 static inline T const volatile& volatile_cast(T const& location) {
00144 return const_castT const volatile&>(location);
00145 }
00146
00148
00149 class ExponentialBackoff {
00151
00153 static const uintptr LOOPS_BEFORE_YIELD = 0x10;
00154 uintptr count;
00155 public:
00156 ExponentialBackoff() : count(1) {}
00157
00159 void pause() {
00160 if( count00161 __TBB_Pause(count);
00162
00163 count*=2;
00164 } else {
00165
00166 __TBB_Yield();
00167 }
00168 }
00169 void reset() {
00170 count = 1;
00171 }
00172 };
00173
00175
00176 templatetypename T, typename U>
00177 static inline void SpinwaitWhileEq( const volatile T& location, U value ) {
00178 ExponentialBackoff backoff;
00179 while( location==value ) {
00180 backoff.pause();
00181 }
00182 }
00183
00185
00186 templatetypename T, typename U>
00187 static inline void SpinwaitUntilEq( const volatile T& location, const U value ) {
00188 ExponentialBackoff backoff;
00189 while( location!=value ) {
00190 backoff.pause();
00191 }
00192 }
00193
00194 }
00195
00196 }
00197
00198 #endif