63
63
#define SEC_TO_NS (1000 * 1000 * 1000)
64
64
65
65
/* Forward declarations */
66
- static int pysleep (_PyTime_t );
66
+ static int pysleep (_PyTime_t timeout );
67
67
68
68
69
69
static PyObject *
@@ -357,17 +357,17 @@ Return the clk_id of a thread's CPU time clock.");
357
357
#endif /* HAVE_PTHREAD_GETCPUCLOCKID */
358
358
359
359
static PyObject *
360
- time_sleep (PyObject * self , PyObject * obj )
360
+ time_sleep (PyObject * self , PyObject * timeout_obj )
361
361
{
362
- _PyTime_t secs ;
363
- if (_PyTime_FromSecondsObject (& secs , obj , _PyTime_ROUND_TIMEOUT ))
362
+ _PyTime_t timeout ;
363
+ if (_PyTime_FromSecondsObject (& timeout , timeout_obj , _PyTime_ROUND_TIMEOUT ))
364
364
return NULL ;
365
- if (secs < 0 ) {
365
+ if (timeout < 0 ) {
366
366
PyErr_SetString (PyExc_ValueError ,
367
367
"sleep length must be non-negative" );
368
368
return NULL ;
369
369
}
370
- if (pysleep (secs ) != 0 ) {
370
+ if (pysleep (timeout ) != 0 ) {
371
371
return NULL ;
372
372
}
373
373
Py_RETURN_NONE ;
@@ -2050,48 +2050,57 @@ PyInit_time(void)
2050
2050
// On error, raise an exception and return -1.
2051
2051
// On success, return 0.
2052
2052
static int
2053
- pysleep (_PyTime_t secs )
2053
+ pysleep (_PyTime_t timeout )
2054
2054
{
2055
- assert (secs >= 0 );
2055
+ assert (timeout >= 0 );
2056
2056
2057
2057
#ifndef MS_WINDOWS
2058
2058
#ifdef HAVE_CLOCK_NANOSLEEP
2059
2059
struct timespec timeout_abs ;
2060
+ #elif defined(HAVE_NANOSLEEP )
2061
+ struct timespec timeout_ts ;
2060
2062
#else
2061
- struct timeval timeout ;
2063
+ struct timeval timeout_tv ;
2062
2064
#endif
2063
2065
_PyTime_t deadline , monotonic ;
2064
2066
int err = 0 ;
2065
2067
2066
2068
if (get_monotonic (& monotonic ) < 0 ) {
2067
2069
return -1 ;
2068
2070
}
2069
- deadline = monotonic + secs ;
2071
+ deadline = monotonic + timeout ;
2070
2072
#ifdef HAVE_CLOCK_NANOSLEEP
2071
2073
if (_PyTime_AsTimespec (deadline , & timeout_abs ) < 0 ) {
2072
2074
return -1 ;
2073
2075
}
2074
2076
#endif
2075
2077
2076
2078
do {
2077
- #ifndef HAVE_CLOCK_NANOSLEEP
2078
- if (_PyTime_AsTimeval (secs , & timeout , _PyTime_ROUND_CEILING ) < 0 ) {
2079
+ #ifdef HAVE_CLOCK_NANOSLEEP
2080
+ // use timeout_abs
2081
+ #elif defined(HAVE_NANOSLEEP )
2082
+ if (_PyTime_AsTimespec (timeout , & timeout_ts ) < 0 ) {
2083
+ return -1 ;
2084
+ }
2085
+ #else
2086
+ if (_PyTime_AsTimeval (timeout , & timeout_tv , _PyTime_ROUND_CEILING ) < 0 ) {
2079
2087
return -1 ;
2080
2088
}
2081
2089
#endif
2082
2090
2083
2091
int ret ;
2084
- #ifdef HAVE_CLOCK_NANOSLEEP
2085
2092
Py_BEGIN_ALLOW_THREADS
2093
+ #ifdef HAVE_CLOCK_NANOSLEEP
2086
2094
ret = clock_nanosleep (CLOCK_MONOTONIC , TIMER_ABSTIME , & timeout_abs , NULL );
2087
- Py_END_ALLOW_THREADS
2088
2095
err = ret ;
2096
+ #elif defined(HAVE_NANOSLEEP )
2097
+ ret = nanosleep (& timeout_ts , NULL );
2098
+ err = errno ;
2089
2099
#else
2090
- Py_BEGIN_ALLOW_THREADS
2091
- ret = select (0 , (fd_set * )0 , (fd_set * )0 , (fd_set * )0 , & timeout );
2092
- Py_END_ALLOW_THREADS
2100
+ ret = select (0 , (fd_set * )0 , (fd_set * )0 , (fd_set * )0 , & timeout_tv );
2093
2101
err = errno ;
2094
2102
#endif
2103
+ Py_END_ALLOW_THREADS
2095
2104
2096
2105
if (ret == 0 ) {
2097
2106
break ;
@@ -2112,8 +2121,8 @@ pysleep(_PyTime_t secs)
2112
2121
if (get_monotonic (& monotonic ) < 0 ) {
2113
2122
return -1 ;
2114
2123
}
2115
- secs = deadline - monotonic ;
2116
- if (secs < 0 ) {
2124
+ timeout = deadline - monotonic ;
2125
+ if (timeout < 0 ) {
2117
2126
break ;
2118
2127
}
2119
2128
/* retry with the recomputed delay */
@@ -2122,10 +2131,11 @@ pysleep(_PyTime_t secs)
2122
2131
2123
2132
return 0 ;
2124
2133
#else // MS_WINDOWS
2125
- _PyTime_t timeout = _PyTime_As100Nanoseconds (secs , _PyTime_ROUND_CEILING );
2134
+ _PyTime_t timeout_100ns = _PyTime_As100Nanoseconds (timeout ,
2135
+ _PyTime_ROUND_CEILING );
2126
2136
2127
2137
// Maintain Windows Sleep() semantics for time.sleep(0)
2128
- if (timeout == 0 ) {
2138
+ if (timeout_100ns == 0 ) {
2129
2139
Py_BEGIN_ALLOW_THREADS
2130
2140
// A value of zero causes the thread to relinquish the remainder of its
2131
2141
// time slice to any other thread that is ready to run. If there are no
@@ -2138,9 +2148,9 @@ pysleep(_PyTime_t secs)
2138
2148
2139
2149
LARGE_INTEGER relative_timeout ;
2140
2150
// No need to check for integer overflow, both types are signed
2141
- assert (sizeof (relative_timeout ) == sizeof (timeout ));
2151
+ assert (sizeof (relative_timeout ) == sizeof (timeout_100ns ));
2142
2152
// SetWaitableTimer(): a negative due time indicates relative time
2143
- relative_timeout .QuadPart = - timeout ;
2153
+ relative_timeout .QuadPart = - timeout_100ns ;
2144
2154
2145
2155
HANDLE timer = CreateWaitableTimerW (NULL , FALSE, NULL );
2146
2156
if (timer == NULL ) {
0 commit comments