43 #if !defined(WIN32) && defined(PLATFORM_X86)
44 void cpuID(
unsigned i,
unsigned regs[4])
47 (
"cpuid" :
"=a" (regs[0]),
"=b" (regs[1]),
"=c" (regs[2]),
"=d" (regs[3])
56 SYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer[32];
57 DWORD returnLength = 32 *
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
59 GetLogicalProcessorInformation(
63 SYSTEM_LOGICAL_PROCESSOR_INFORMATION* ptr = buffer;
65 uint32 processorCoreCount = 0;
67 for (
uint32 byteOffset = 0;
68 byteOffset +
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength;
69 byteOffset +=
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION))
71 switch (ptr->Relationship)
73 case RelationProcessorCore:
80 return processorCoreCount;
81 #elif defined(PLATFORM_X86)
87 memcpy(&vendor[0], ®s[1],
sizeof(
unsigned));
88 memcpy(&vendor[1], ®s[3],
sizeof(
unsigned));
89 memcpy(&vendor[2], ®s[2],
sizeof(
unsigned));
90 string cpuVendor = string(vendor, 12);
98 unsigned logical = (regs[1] >> 16) & 0xff;
99 unsigned cores = logical;
101 if (cpuVendor ==
"GenuineIntel")
105 cores = ((regs[0] >> 26) & 0x3f) + 1;
107 else if (cpuVendor ==
"AuthenticAMD")
110 cpuID(0x80000008, regs);
111 cores = ((unsigned)(regs[2] & 0xff)) + 1;
122 GetSystemInfo( &sysinfo );
123 return uint32( sysinfo.dwNumberOfProcessors );
125 return uint32( sysconf( _SC_NPROCESSORS_ONLN ) );
132 struct ThreadBase::Impl
137 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new Impl )
142 ThreadBase::~ThreadBase()
147 void ThreadBase::create(
void* (*func)(
void*),
void* arg)
152 void ThreadBase::join()
161 Mutex::Mutex() : m_impl( new Impl )
168 void Mutex::lock() {}
169 void Mutex::unlock() {}
179 typedef void* (*FuncType)(
void*);
184 DWORD __stdcall CallFunc(
void* arg)
186 Func* func =
reinterpret_cast<Func*
>(arg);
187 func->m_func( func->m_arg );
193 struct ThreadBase::Impl
195 Impl() : m_handle(0), m_tid(0) {}
199 CloseHandle( m_handle );
208 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new Impl )
213 ThreadBase::~ThreadBase()
218 void ThreadBase::create(
void* (*func)(
void*),
void* arg)
221 m_impl->m_func_arg.m_func = func;
222 m_impl->m_func_arg.m_arg = arg;
225 m_impl->m_handle = CreateThread(
234 ResumeThread( m_impl->m_handle );
237 void ThreadBase::join()
239 WaitForSingleObject( m_impl->m_handle, INFINITE );
245 Impl() { InitializeCriticalSection( &m_mutex ); }
246 ~Impl() { DeleteCriticalSection( &m_mutex ); }
248 CRITICAL_SECTION m_mutex;
251 Mutex::Mutex() : m_impl( new Impl )
258 void Mutex::lock() { EnterCriticalSection( &m_impl->m_mutex ); }
259 void Mutex::unlock() { LeaveCriticalSection( &m_impl->m_mutex ); }
271 ThreadBase::ThreadBase() : m_id( 0u ), m_impl( new
Impl )
283 pthread_create( &m_impl->m_thread, NULL, func, arg );
288 pthread_join( m_impl->m_thread, NULL );