?? common.cpp
字號:
/*****************************************************************************Copyright (c) 2001 - 2008, The Board of Trustees of the University of Illinois.All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* Neither the name of the University of Illinois nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER ORCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*****************************************************************************//*****************************************************************************written by Yunhong Gu, last updated 12/05/2008*****************************************************************************/#ifndef WIN32 #include <cstring> #include <cerrno>#else #include <winsock2.h> #include <ws2tcpip.h> #include <wspiapi.h>#endif#include <cmath>#include "md5.h"#include "common.h"uint64_t CTimer::s_ullCPUFrequency = CTimer::readCPUFrequency();#ifndef WIN32 pthread_mutex_t CTimer::m_EventLock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t CTimer::m_EventCond = PTHREAD_COND_INITIALIZER;#else pthread_mutex_t CTimer::m_EventLock = CreateMutex(NULL, false, NULL); pthread_cond_t CTimer::m_EventCond = CreateEvent(NULL, false, false, NULL);#endifCTimer::CTimer(){ #ifndef WIN32 pthread_mutex_init(&m_TickLock, NULL); pthread_cond_init(&m_TickCond, NULL); #else m_TickLock = CreateMutex(NULL, false, NULL); m_TickCond = CreateEvent(NULL, false, false, NULL); #endif}CTimer::~CTimer(){ #ifndef WIN32 pthread_mutex_destroy(&m_TickLock); pthread_cond_destroy(&m_TickCond); #else CloseHandle(m_TickLock); CloseHandle(m_TickCond); #endif}void CTimer::rdtsc(uint64_t &x){ #ifdef WIN32 //HANDLE hCurThread = ::GetCurrentThread(); //DWORD_PTR dwOldMask = ::SetThreadAffinityMask(hCurThread, 1); BOOL ret = QueryPerformanceCounter((LARGE_INTEGER *)&x); //SetThreadAffinityMask(hCurThread, dwOldMask); if (!ret) x = getTime() * s_ullCPUFrequency; #elif IA32 uint32_t lval, hval; //asm volatile ("push %eax; push %ebx; push %ecx; push %edx"); //asm volatile ("xor %eax, %eax; cpuid"); asm volatile ("rdtsc" : "=a" (lval), "=d" (hval)); //asm volatile ("pop %edx; pop %ecx; pop %ebx; pop %eax"); x = hval; x = (x << 32) | lval; #elif IA64 asm ("mov %0=ar.itc" : "=r"(x) :: "memory"); #elif AMD64 uint32_t lval, hval; asm ("rdtsc" : "=a" (lval), "=d" (hval)); x = hval; x = (x << 32) | lval; #else // use system call to read time clock for other archs timeval t; gettimeofday(&t, 0); x = (uint64_t)t.tv_sec * (uint64_t)1000000 + (uint64_t)t.tv_usec; #endif}uint64_t CTimer::readCPUFrequency(){ #ifdef WIN32 int64_t ccf; if (QueryPerformanceFrequency((LARGE_INTEGER *)&ccf)) return ccf / 1000000; else return 1; #elif IA32 || IA64 || AMD64 uint64_t t1, t2; rdtsc(t1); timespec ts; ts.tv_sec = 0; ts.tv_nsec = 100000000; nanosleep(&ts, NULL); rdtsc(t2); // CPU clocks per microsecond return (t2 - t1) / 100000; #else return 1; #endif}uint64_t CTimer::getCPUFrequency(){ return s_ullCPUFrequency;}void CTimer::sleep(const uint64_t& interval){ uint64_t t; rdtsc(t); // sleep next "interval" time sleepto(t + interval);}void CTimer::sleepto(const uint64_t& nexttime){ // Use class member such that the method can be interrupted by others m_ullSchedTime = nexttime; uint64_t t; rdtsc(t); while (t < m_ullSchedTime) { #ifndef NO_BUSY_WAITING #ifdef IA32 __asm__ volatile ("pause; rep; nop; nop; nop; nop; nop;"); #elif IA64 __asm__ volatile ("nop 0; nop 0; nop 0; nop 0; nop 0;"); #elif AMD64 __asm__ volatile ("nop; nop; nop; nop; nop;"); #endif #else #ifndef WIN32 timeval now; timespec timeout; gettimeofday(&now, 0); if (now.tv_usec < 990000) { timeout.tv_sec = now.tv_sec; timeout.tv_nsec = (now.tv_usec + 10000) * 1000; } else { timeout.tv_sec = now.tv_sec + 1; timeout.tv_nsec = (now.tv_usec + 10000 - 1000000) * 1000; } pthread_mutex_lock(&m_TickLock); pthread_cond_timedwait(&m_TickCond, &m_TickLock, &timeout); pthread_mutex_unlock(&m_TickLock); #else WaitForSingleObject(m_TickCond, 1); #endif #endif rdtsc(t); }}void CTimer::interrupt(){ // schedule the sleepto time to the current CCs, so that it will stop rdtsc(m_ullSchedTime); tick();}void CTimer::tick(){ #ifndef WIN32 pthread_cond_signal(&m_TickCond); #else SetEvent(m_TickCond); #endif}uint64_t CTimer::getTime(){ #ifndef WIN32 timeval t; gettimeofday(&t, 0); return t.tv_sec * 1000000ULL + t.tv_usec; #else LARGE_INTEGER ccf; HANDLE hCurThread = ::GetCurrentThread(); DWORD_PTR dwOldMask = ::SetThreadAffinityMask(hCurThread, 1); if (QueryPerformanceFrequency(&ccf)) { LARGE_INTEGER cc; if (QueryPerformanceCounter(&cc)) { SetThreadAffinityMask(hCurThread, dwOldMask); return (cc.QuadPart * 1000000ULL / ccf.QuadPart); } } SetThreadAffinityMask(hCurThread, dwOldMask); return GetTickCount() * 1000ULL; #endif}void CTimer::triggerEvent(){ #ifndef WIN32 pthread_cond_signal(&m_EventCond); #else SetEvent(m_EventCond); #endif}void CTimer::waitForEvent(){ #ifndef WIN32 timeval now; timespec timeout; gettimeofday(&now, 0); if (now.tv_usec < 990000) { timeout.tv_sec = now.tv_sec; timeout.tv_nsec = (now.tv_usec + 10000) * 1000; } else { timeout.tv_sec = now.tv_sec + 1; timeout.tv_nsec = (now.tv_usec + 10000 - 1000000) * 1000; } pthread_mutex_lock(&m_EventLock); pthread_cond_timedwait(&m_EventCond, &m_EventLock, &timeout); pthread_mutex_unlock(&m_EventLock); #else WaitForSingleObject(m_EventCond, 1); #endif}//// Automatically lock in constructorCGuard::CGuard(pthread_mutex_t& lock):m_Mutex(lock){ #ifndef WIN32 m_iLocked = pthread_mutex_lock(&m_Mutex); #else m_iLocked = WaitForSingleObject(m_Mutex, INFINITE); #endif}// Automatically unlock in destructorCGuard::~CGuard(){ #ifndef WIN32 if (0 == m_iLocked) pthread_mutex_unlock(&m_Mutex); #else if (WAIT_FAILED != m_iLocked) ReleaseMutex(m_Mutex); #endif}void CGuard::enterCS(pthread_mutex_t& lock){ #ifndef WIN32 pthread_mutex_lock(&lock); #else WaitForSingleObject(lock, INFINITE); #endif}void CGuard::leaveCS(pthread_mutex_t& lock){ #ifndef WIN32 pthread_mutex_unlock(&lock); #else ReleaseMutex(lock); #endif}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -