Skip to content

Commit

Permalink
Replace Mutex with CriticalSection
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Mar 8, 2024
1 parent 2d9008b commit 94f93f4
Showing 1 changed file with 30 additions and 32 deletions.
62 changes: 30 additions & 32 deletions timeskew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ static void(WINAPI* TrueQueryInterruptTimePrecise)(PULONGLONG lpInterruptTimePre
static BOOL(WINAPI* TrueQueryUnbiasedInterruptTime)(PULONGLONG UnbiasedTime) = QueryUnbiasedInterruptTime;
static void(WINAPI* TrueQueryUnbiasedInterruptTimePrecise)(PULONGLONG lpUnbiasedInterruptTimePrecise) = QueryUnbiasedInterruptTimePrecise;

// TODO: can we use critical sections instead?
// TODO: use separate mutexes for separate functions
HANDLE ghMutex;
// TODO: use separate CriticalSection for each function
CRITICAL_SECTION CriticalSection;

static QWORD lastTrueDateTime = 0;
static QWORD lastSkewedDateTime = 0;
Expand Down Expand Up @@ -251,7 +250,7 @@ void SkewedGetSystemTime(LPSYSTEMTIME lpSystemTime) {
FILETIME fileTime;
SystemTimeToFileTime(lpSystemTime, &fileTime);
QWORD dateTime = fileTime.dwHighDateTime * (1ULL << 32) + fileTime.dwLowDateTime;
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
if (lastTrueDateTime == 0) {
lastTrueDateTime = dateTime;
lastSkewedDateTime = dateTime;
Expand All @@ -263,7 +262,7 @@ void SkewedGetSystemTime(LPSYSTEMTIME lpSystemTime) {
fileTime.dwLowDateTime = (DWORD) dateTime;
fileTime.dwHighDateTime = dateTime >> 32;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
FileTimeToSystemTime(&fileTime, lpSystemTime);
}

Expand All @@ -273,7 +272,7 @@ void SkewedGetLocalTime(LPSYSTEMTIME lpSystemTime) {
TrueGetLocalTime(lpSystemTime);
FILETIME fileTime;
SystemTimeToFileTime(lpSystemTime, &fileTime);
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static QWORD lastLocalTrueDateTime = 0;
static QWORD lastLocalSkewedDateTime = 0;
QWORD dateTime = fileTime.dwHighDateTime * (1ULL << 32) + fileTime.dwLowDateTime;
Expand All @@ -288,7 +287,7 @@ void SkewedGetLocalTime(LPSYSTEMTIME lpSystemTime) {
fileTime.dwLowDateTime = (DWORD)dateTime;
fileTime.dwHighDateTime = dateTime >> 32;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
FileTimeToSystemTime(&fileTime, lpSystemTime);
}

Expand All @@ -297,7 +296,7 @@ void SkewedGetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) {
log("SkewedGetSystemTimeAsFileTime");
TrueGetSystemTimeAsFileTime(lpSystemTimeAsFileTime);
QWORD dateTime = lpSystemTimeAsFileTime->dwHighDateTime * (1ULL << 32) + lpSystemTimeAsFileTime->dwLowDateTime;
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
if (lastTrueDateTime == 0) {
lastTrueDateTime = dateTime;
lastSkewedDateTime = dateTime;
Expand All @@ -309,7 +308,7 @@ void SkewedGetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) {
lpSystemTimeAsFileTime->dwLowDateTime = (DWORD)dateTime;
lpSystemTimeAsFileTime->dwHighDateTime = dateTime >> 32;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
}

UINT_PTR SkewedSetTimer(HWND hWnd, UINT_PTR nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc) {
Expand Down Expand Up @@ -345,12 +344,12 @@ DWORD SkewedWaitForMultipleObjectsEx(DWORD nCount, const HANDLE* lpHandles, BOOL
return ret;
}

BOOL SkewedSleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection, DWORD dwMilliseconds) {
BOOL SkewedSleepConditionVariableCS(PCONDITION_VARIABLE ConditionVariable, PCRITICAL_SECTION CriticalSection2, DWORD dwMilliseconds) {
log("SkewedSleepConditionVariableCS");
if (dwMilliseconds > 0) {
dwMilliseconds = dwMilliseconds * denom / num;
}
return TrueSleepConditionVariableCS(ConditionVariable, CriticalSection, dwMilliseconds);
return TrueSleepConditionVariableCS(ConditionVariable, CriticalSection2, dwMilliseconds);
}

BOOL SkewedSleepConditionVariableSRW(PCONDITION_VARIABLE ConditionVariable, PSRWLOCK SRWLock, DWORD dwMilliseconds, ULONG Flags) {
Expand Down Expand Up @@ -409,7 +408,7 @@ DWORD SkewedWaitForInputIdle(HANDLE hProcess, DWORD dwMilliseconds) {
DWORD SkewedGetTickCount() {
log("SkewedGetTickCount");
DWORD tickCount = TrueGetTickCount();
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static DWORD lastTrueTickCount= 0;
static DWORD lastSkewedTickCount = 0;
if (lastTrueTickCount == 0) {
Expand All @@ -421,14 +420,14 @@ DWORD SkewedGetTickCount() {
tickCount = lastSkewedTickCount + delta * num / denom;
lastSkewedTickCount = tickCount;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
return tickCount;
}

ULONGLONG SkewedGetTickCount64() {
log("SkewedGetTickCount64");
ULONGLONG tickCount = TrueGetTickCount64();
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static ULONGLONG lastTrueTickCount = 0;
static ULONGLONG lastSkewedTickCount = 0;
if (lastTrueTickCount == 0) {
Expand All @@ -440,7 +439,7 @@ ULONGLONG SkewedGetTickCount64() {
tickCount = lastSkewedTickCount + delta * num / denom;
lastSkewedTickCount = tickCount;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
return tickCount;
}

Expand All @@ -450,7 +449,7 @@ BOOL SkewedQueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount) {
if (ret == 0) {
return ret;
}
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static LONGLONG lastTrue = 0;
static LONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -462,14 +461,14 @@ BOOL SkewedQueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount) {
lpPerformanceCount->QuadPart = lastSkewed + delta * num / denom;
lastSkewed = lpPerformanceCount->QuadPart;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
return ret;
}

void SkewedQueryInterruptTime(PULONGLONG lpInterruptTime) {
log("SkewedQueryInterruptTime");
TrueQueryInterruptTime(lpInterruptTime);
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -481,13 +480,13 @@ void SkewedQueryInterruptTime(PULONGLONG lpInterruptTime) {
*lpInterruptTime = lastSkewed + delta * num / denom;
lastSkewed = *lpInterruptTime;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
}

void SkewedQueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise) {
log("SkewedQueryInterruptTimePrecise");
TrueQueryInterruptTimePrecise(lpInterruptTimePrecise);
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -499,7 +498,7 @@ void SkewedQueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise) {
*lpInterruptTimePrecise = lastSkewed + delta * num / denom;
lastSkewed = *lpInterruptTimePrecise;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
}

BOOL SkewedQueryUnbiasedInterruptTime(PULONGLONG UnbiasedTime) {
Expand All @@ -508,7 +507,7 @@ BOOL SkewedQueryUnbiasedInterruptTime(PULONGLONG UnbiasedTime) {
if (ret == 0) {
return ret;
}
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -520,14 +519,14 @@ BOOL SkewedQueryUnbiasedInterruptTime(PULONGLONG UnbiasedTime) {
*UnbiasedTime = lastSkewed + delta * num / denom;
lastSkewed = *UnbiasedTime;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
return ret;
}

void SkewedQueryUnbiasedInterruptTimePrecise(PULONGLONG lpUnbiasedInterruptTimePrecise) {
log("SkewedQueryUnbiasedInterruptTimePrecise");
TrueQueryUnbiasedInterruptTimePrecise(lpUnbiasedInterruptTimePrecise);
WaitForSingleObject(ghMutex, INFINITE);
EnterCriticalSection(&CriticalSection);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -539,7 +538,7 @@ void SkewedQueryUnbiasedInterruptTimePrecise(PULONGLONG lpUnbiasedInterruptTimeP
*lpUnbiasedInterruptTimePrecise = lastSkewed + delta * num / denom;
lastSkewed = *lpUnbiasedInterruptTimePrecise;
}
ReleaseMutex(ghMutex);
LeaveCriticalSection(&CriticalSection);
}

BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) {
Expand All @@ -549,15 +548,14 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) {
return TRUE;
}

// Create Mutex
ghMutex = CreateMutex(NULL, FALSE, NULL);
if (ghMutex == NULL) {
fprintf(stderr, "CreateMutex error: %ld\n", GetLastError());
return 1;
}

// Replace/restore time-related functions
if (dwReason == DLL_PROCESS_ATTACH) {
// Create CriticalSection
if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 1024) ) {
fprintf(stderr, "InitializeCriticalSectionAndSpinCount error: %ld\n", GetLastError());
exit(1);
}

init_logging();
init_timeskew();
init_server();
Expand Down

0 comments on commit 94f93f4

Please sign in to comment.