Merge pull request #18556 from taosdata/FIX/xsren/TS-2157-winGetTime

fix:concurrency conflicts occur when obtaining windows system time
This commit is contained in:
Shengliang Guan 2022-11-30 14:23:41 +08:00 committed by GitHub
commit 6bfcc2e4c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 11 deletions

View File

@ -554,17 +554,26 @@ int32_t taosClockGetTime(int clock_id, struct timespec *pTS) {
static SYSTEMTIME ss; static SYSTEMTIME ss;
static LARGE_INTEGER offset; static LARGE_INTEGER offset;
ss.wYear = 1970; static int8_t offsetInit = 0;
ss.wMonth = 1; static volatile bool offsetInitFinished = false;
ss.wDay = 1; int8_t old = atomic_val_compare_exchange_8(&offsetInit, 0, 1);
ss.wHour = 0; if (0 == old) {
ss.wMinute = 0; ss.wYear = 1970;
ss.wSecond = 0; ss.wMonth = 1;
ss.wMilliseconds = 0; ss.wDay = 1;
SystemTimeToFileTime(&ss, &ff); ss.wHour = 0;
offset.QuadPart = ff.dwHighDateTime; ss.wMinute = 0;
offset.QuadPart <<= 32; ss.wSecond = 0;
offset.QuadPart |= ff.dwLowDateTime; ss.wMilliseconds = 0;
SystemTimeToFileTime(&ss, &ff);
offset.QuadPart = ff.dwHighDateTime;
offset.QuadPart <<= 32;
offset.QuadPart |= ff.dwLowDateTime;
offsetInitFinished = true;
} else {
while (!offsetInitFinished)
; // Ensure initialization is completed.
}
GetSystemTimeAsFileTime(&f); GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime; t.QuadPart = f.dwHighDateTime;