Skip to content

Commit

Permalink
Reduce scopes where mutex is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Mar 8, 2024
1 parent 4eb080e commit 2d9008b
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions timeskew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ DWORD WINAPI SkewedSleepEx(DWORD dwMilliseconds, BOOL bAlertable) {

void SkewedGetSystemTime(LPSYSTEMTIME lpSystemTime) {
log("SkewedGetSystemTime");
WaitForSingleObject(ghMutex, INFINITE);
TrueGetSystemTime(lpSystemTime);
FILETIME fileTime;
SystemTimeToFileTime(lpSystemTime, &fileTime);
QWORD dateTime = fileTime.dwHighDateTime * (1ULL << 32) + fileTime.dwLowDateTime;
WaitForSingleObject(ghMutex, INFINITE);
if (lastTrueDateTime == 0) {
lastTrueDateTime = dateTime;
lastSkewedDateTime = dateTime;
Expand All @@ -263,17 +263,17 @@ void SkewedGetSystemTime(LPSYSTEMTIME lpSystemTime) {
fileTime.dwLowDateTime = (DWORD) dateTime;
fileTime.dwHighDateTime = dateTime >> 32;
}
FileTimeToSystemTime(&fileTime, lpSystemTime);
ReleaseMutex(ghMutex);
FileTimeToSystemTime(&fileTime, lpSystemTime);
}

void SkewedGetLocalTime(LPSYSTEMTIME lpSystemTime) {
log("SkewedGetLocalTime");
WaitForSingleObject(ghMutex, INFINITE);
// NOTE: FileTime is based on UTC, but, hopefully, that still works for our purposes
TrueGetLocalTime(lpSystemTime);
FILETIME fileTime;
SystemTimeToFileTime(lpSystemTime, &fileTime);
WaitForSingleObject(ghMutex, INFINITE);
static QWORD lastLocalTrueDateTime = 0;
static QWORD lastLocalSkewedDateTime = 0;
QWORD dateTime = fileTime.dwHighDateTime * (1ULL << 32) + fileTime.dwLowDateTime;
Expand All @@ -288,16 +288,16 @@ void SkewedGetLocalTime(LPSYSTEMTIME lpSystemTime) {
fileTime.dwLowDateTime = (DWORD)dateTime;
fileTime.dwHighDateTime = dateTime >> 32;
}
FileTimeToSystemTime(&fileTime, lpSystemTime);
ReleaseMutex(ghMutex);
FileTimeToSystemTime(&fileTime, lpSystemTime);
}


void SkewedGetSystemTimeAsFileTime(LPFILETIME lpSystemTimeAsFileTime) {
log("SkewedGetSystemTimeAsFileTime");
WaitForSingleObject(ghMutex, INFINITE);
TrueGetSystemTimeAsFileTime(lpSystemTimeAsFileTime);
QWORD dateTime = lpSystemTimeAsFileTime->dwHighDateTime * (1ULL << 32) + lpSystemTimeAsFileTime->dwLowDateTime;
WaitForSingleObject(ghMutex, INFINITE);
if (lastTrueDateTime == 0) {
lastTrueDateTime = dateTime;
lastSkewedDateTime = dateTime;
Expand Down Expand Up @@ -408,8 +408,8 @@ DWORD SkewedWaitForInputIdle(HANDLE hProcess, DWORD dwMilliseconds) {

DWORD SkewedGetTickCount() {
log("SkewedGetTickCount");
WaitForSingleObject(ghMutex, INFINITE);
DWORD tickCount = TrueGetTickCount();
WaitForSingleObject(ghMutex, INFINITE);
static DWORD lastTrueTickCount= 0;
static DWORD lastSkewedTickCount = 0;
if (lastTrueTickCount == 0) {
Expand All @@ -427,8 +427,8 @@ DWORD SkewedGetTickCount() {

ULONGLONG SkewedGetTickCount64() {
log("SkewedGetTickCount64");
WaitForSingleObject(ghMutex, INFINITE);
ULONGLONG tickCount = TrueGetTickCount64();
WaitForSingleObject(ghMutex, INFINITE);
static ULONGLONG lastTrueTickCount = 0;
static ULONGLONG lastSkewedTickCount = 0;
if (lastTrueTickCount == 0) {
Expand All @@ -446,12 +446,11 @@ ULONGLONG SkewedGetTickCount64() {

BOOL SkewedQueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount) {
log("SkewedQueryPerformanceCounter");
WaitForSingleObject(ghMutex, INFINITE);
BOOL ret = TrueQueryPerformanceCounter(lpPerformanceCount);
if (ret == 0) {
ReleaseMutex(ghMutex);
return ret;
}
WaitForSingleObject(ghMutex, INFINITE);
static LONGLONG lastTrue = 0;
static LONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -469,8 +468,8 @@ BOOL SkewedQueryPerformanceCounter(LARGE_INTEGER* lpPerformanceCount) {

void SkewedQueryInterruptTime(PULONGLONG lpInterruptTime) {
log("SkewedQueryInterruptTime");
WaitForSingleObject(ghMutex, INFINITE);
TrueQueryInterruptTime(lpInterruptTime);
WaitForSingleObject(ghMutex, INFINITE);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -487,8 +486,8 @@ void SkewedQueryInterruptTime(PULONGLONG lpInterruptTime) {

void SkewedQueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise) {
log("SkewedQueryInterruptTimePrecise");
WaitForSingleObject(ghMutex, INFINITE);
TrueQueryInterruptTimePrecise(lpInterruptTimePrecise);
WaitForSingleObject(ghMutex, INFINITE);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -505,12 +504,11 @@ void SkewedQueryInterruptTimePrecise(PULONGLONG lpInterruptTimePrecise) {

BOOL SkewedQueryUnbiasedInterruptTime(PULONGLONG UnbiasedTime) {
log("SkewedQueryUnbiasedInterruptTime");
WaitForSingleObject(ghMutex, INFINITE);
BOOL ret = TrueQueryUnbiasedInterruptTime(UnbiasedTime);
if (ret == 0) {
ReleaseMutex(ghMutex);
return ret;
}
WaitForSingleObject(ghMutex, INFINITE);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand All @@ -528,8 +526,8 @@ BOOL SkewedQueryUnbiasedInterruptTime(PULONGLONG UnbiasedTime) {

void SkewedQueryUnbiasedInterruptTimePrecise(PULONGLONG lpUnbiasedInterruptTimePrecise) {
log("SkewedQueryUnbiasedInterruptTimePrecise");
WaitForSingleObject(ghMutex, INFINITE);
TrueQueryUnbiasedInterruptTimePrecise(lpUnbiasedInterruptTimePrecise);
WaitForSingleObject(ghMutex, INFINITE);
static ULONGLONG lastTrue = 0;
static ULONGLONG lastSkewed = 0;
if (lastTrue == 0) {
Expand Down

0 comments on commit 2d9008b

Please sign in to comment.