Skip to content

Commit

Permalink
implement fallback for older Windows versions
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Sep 19, 2022
1 parent 9d3900b commit c9f52a6
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/win_precise_time/_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,25 @@ _sleep_until(LARGE_INTEGER *due_time)
TIMER_ALL_ACCESS);
if (timer == NULL)
{
PyErr_SetFromWindowsErr(0);
return (-1);
DWORD firstError = GetLastError();
if (firstError == 87ul)
{
// try fallback without CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
timer = wpt_CreateWaitableTimerExW(NULL,
NULL,
0,
TIMER_ALL_ACCESS);
if (timer == NULL)
{
PyErr_Format(PyExc_OSError, "CreateWaitableTimerExW failed with error %lu", GetLastError());
return (-1);
}
}
else
{
PyErr_Format(PyExc_OSError, "CreateWaitableTimerExW failed with error %lu", firstError);
return (-1);
}
}

if (!wpt_SetWaitableTimerEx(timer,
Expand All @@ -76,7 +93,7 @@ _sleep_until(LARGE_INTEGER *due_time)
NULL, // no wake context; do not resume from suspend
0)) // no tolerable delay for timer coalescing
{
PyErr_SetFromWindowsErr(0);
PyErr_Format(PyExc_OSError, "SetWaitableTimerEx failed with error %lu", GetLastError());
goto error;
}

Expand Down Expand Up @@ -113,7 +130,7 @@ _sleep_until(LARGE_INTEGER *due_time)
}

if (rc == WAIT_FAILED) {
PyErr_SetFromWindowsErr(0);
PyErr_Format(PyExc_OSError, "WaitForMultipleObjects failed with error %lu", GetLastError());
goto error;
}

Expand Down Expand Up @@ -144,7 +161,7 @@ _sleep_until(LARGE_INTEGER *due_time)
}

if (rc == WAIT_FAILED) {
PyErr_SetFromWindowsErr(0);
PyErr_Format(PyExc_OSError, "WaitForSingleObject failed with error %lu", GetLastError());
goto error;
}

Expand Down

0 comments on commit c9f52a6

Please sign in to comment.