@@ -408,6 +408,13 @@ static PyStructSequence_Desc struct_time_type_desc = {
408408static int initialized ;
409409static PyTypeObject StructTimeType ;
410410
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
411418
412419static PyObject *
413420tmtotuple (struct tm * p
@@ -2017,6 +2024,23 @@ time_exec(PyObject *module)
20172024 utc_string = tm .tm_zone ;
20182025#endif
20192026
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+
20202044 return 0 ;
20212045}
20222046
@@ -2150,20 +2174,18 @@ pysleep(_PyTime_t timeout)
21502174 // SetWaitableTimer(): a negative due time indicates relative time
21512175 relative_timeout .QuadPart = - timeout_100ns ;
21522176
2153- HANDLE timer = CreateWaitableTimerW (NULL , FALSE, NULL );
2177+ HANDLE timer = CreateWaitableTimerExW (NULL , NULL , timer_flags ,
2178+ TIMER_ALL_ACCESS );
21542179 if (timer == NULL ) {
21552180 PyErr_SetFromWindowsErr (0 );
21562181 return -1 ;
21572182 }
21582183
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
21672189 {
21682190 PyErr_SetFromWindowsErr (0 );
21692191 goto error ;
0 commit comments