@@ -408,6 +408,13 @@ static PyStructSequence_Desc struct_time_type_desc = {
408
408
static int initialized ;
409
409
static PyTypeObject StructTimeType ;
410
410
411
+ #if defined(MS_WINDOWS )
412
+ #ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
413
+ #define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
414
+ #endif
415
+
416
+ static DWORD timer_flags = (DWORD )- 1 ;
417
+ #endif
411
418
412
419
static PyObject *
413
420
tmtotuple (struct tm * p
@@ -2017,6 +2024,23 @@ time_exec(PyObject *module)
2017
2024
utc_string = tm .tm_zone ;
2018
2025
#endif
2019
2026
2027
+ #if defined(MS_WINDOWS )
2028
+ if (timer_flags == (DWORD )- 1 ) {
2029
+ DWORD test_flags = CREATE_WAITABLE_TIMER_HIGH_RESOLUTION ;
2030
+ HANDLE timer = CreateWaitableTimerExW (NULL , NULL , test_flags ,
2031
+ TIMER_ALL_ACCESS );
2032
+ if (timer == NULL ) {
2033
+ // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION is not supported.
2034
+ timer_flags = 0 ;
2035
+ }
2036
+ else {
2037
+ // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION is supported.
2038
+ timer_flags = CREATE_WAITABLE_TIMER_HIGH_RESOLUTION ;
2039
+ CloseHandle (timer );
2040
+ }
2041
+ }
2042
+ #endif
2043
+
2020
2044
return 0 ;
2021
2045
}
2022
2046
@@ -2150,20 +2174,18 @@ pysleep(_PyTime_t timeout)
2150
2174
// SetWaitableTimer(): a negative due time indicates relative time
2151
2175
relative_timeout .QuadPart = - timeout_100ns ;
2152
2176
2153
- HANDLE timer = CreateWaitableTimerW (NULL , FALSE, NULL );
2177
+ HANDLE timer = CreateWaitableTimerExW (NULL , NULL , timer_flags ,
2178
+ TIMER_ALL_ACCESS );
2154
2179
if (timer == NULL ) {
2155
2180
PyErr_SetFromWindowsErr (0 );
2156
2181
return -1 ;
2157
2182
}
2158
2183
2159
- if (!SetWaitableTimer (timer , & relative_timeout ,
2160
- // period: the timer is signaled once
2161
- 0 ,
2162
- // no completion routine
2163
- NULL , NULL ,
2164
- // Don't restore a system in suspended power
2165
- // conservation mode when the timer is signaled.
2166
- FALSE))
2184
+ if (!SetWaitableTimerEx (timer , & relative_timeout ,
2185
+ 0 , // no period; the timer is signaled once
2186
+ NULL , NULL , // no completion routine
2187
+ NULL , // no wake context; do not resume from suspend
2188
+ 0 )) // no tolerable delay for timer coalescing
2167
2189
{
2168
2190
PyErr_SetFromWindowsErr (0 );
2169
2191
goto error ;
0 commit comments