Skip to content

Commit ff06516

Browse files
committed
implement platform compile-time dependent time.sleep(0)
The implementations of `time.sleep(0)` on Windows and POSIX are now handled by the same function. Previously, `time.sleep(0)` on Windows was handled by `pysleep()` while on POSIX platforms, it was handled by `pysleep_zero_posix()`.
1 parent 434da31 commit ff06516

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

Modules/timemodule.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ module time
7272

7373
/* Forward declarations */
7474
static int pysleep(PyTime_t timeout);
75-
#ifndef MS_WINDOWS
76-
static int pysleep_zero_posix(void); // see gh-125997
77-
#endif
75+
static int pysleep_zero(void); // see gh-125997
7876

7977

8078
typedef struct {
@@ -2219,7 +2217,14 @@ pysleep(PyTime_t timeout)
22192217
assert(!PyErr_Occurred());
22202218
#ifndef MS_WINDOWS
22212219
if (timeout == 0) { // gh-125997
2222-
return pysleep_zero_posix();
2220+
return pysleep_zero();
2221+
}
2222+
#else
2223+
PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout,
2224+
_PyTime_ROUND_CEILING);
2225+
// Maintain Windows Sleep() semantics for time.sleep(0)
2226+
if (timeout_100ns == 0) {
2227+
return pysleep_zero();
22232228
}
22242229
#endif
22252230

@@ -2300,21 +2305,6 @@ pysleep(PyTime_t timeout)
23002305

23012306
return 0;
23022307
#else // MS_WINDOWS
2303-
PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout,
2304-
_PyTime_ROUND_CEILING);
2305-
2306-
// Maintain Windows Sleep() semantics for time.sleep(0)
2307-
if (timeout_100ns == 0) {
2308-
Py_BEGIN_ALLOW_THREADS
2309-
// A value of zero causes the thread to relinquish the remainder of its
2310-
// time slice to any other thread that is ready to run. If there are no
2311-
// other threads ready to run, the function returns immediately, and
2312-
// the thread continues execution.
2313-
Sleep(0);
2314-
Py_END_ALLOW_THREADS
2315-
return 0;
2316-
}
2317-
23182308
LARGE_INTEGER relative_timeout;
23192309
// No need to check for integer overflow, both types are signed
23202310
assert(sizeof(relative_timeout) == sizeof(timeout_100ns));
@@ -2401,7 +2391,6 @@ pysleep(PyTime_t timeout)
24012391
}
24022392

24032393

2404-
#ifndef MS_WINDOWS
24052394
// time.sleep(0) optimized implementation.
24062395
// On error, raise an exception and return -1.
24072396
// On success, return 0.
@@ -2412,10 +2401,10 @@ pysleep(PyTime_t timeout)
24122401
// skip some calls to `PyTime_Monotonic()` and other checks when the timeout
24132402
// is zero. For details, see https://github.com/python/cpython/pull/128274.
24142403
static int
2415-
pysleep_zero_posix(void)
2404+
pysleep_zero(void)
24162405
{
24172406
assert(!PyErr_Occurred());
2418-
2407+
#ifndef MS_WINDOWS
24192408
int ret, err;
24202409
Py_BEGIN_ALLOW_THREADS
24212410
#ifdef HAVE_CLOCK_NANOSLEEP
@@ -2453,6 +2442,14 @@ pysleep_zero_posix(void)
24532442
if (PyErr_CheckSignals()) {
24542443
return -1;
24552444
}
2445+
#else
2446+
Py_BEGIN_ALLOW_THREADS
2447+
// A value of zero causes the thread to relinquish the remainder of its
2448+
// time slice to any other thread that is ready to run. If there are no
2449+
// other threads ready to run, the function returns immediately, and
2450+
// the thread continues execution.
2451+
Sleep(0);
2452+
Py_END_ALLOW_THREADS
2453+
#endif
24562454
return 0;
24572455
}
2458-
#endif

0 commit comments

Comments
 (0)