Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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()`.
  • Loading branch information
picnixz committed Jan 6, 2025
commit ff06516bfef7484795c28da777eedf596efc7724
43 changes: 20 additions & 23 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ module time

/* Forward declarations */
static int pysleep(PyTime_t timeout);
#ifndef MS_WINDOWS
static int pysleep_zero_posix(void); // see gh-125997
#endif
static int pysleep_zero(void); // see gh-125997


typedef struct {
Expand Down Expand Up @@ -2219,7 +2217,14 @@ pysleep(PyTime_t timeout)
assert(!PyErr_Occurred());
#ifndef MS_WINDOWS
if (timeout == 0) { // gh-125997
Comment thread
picnixz marked this conversation as resolved.
return pysleep_zero_posix();
return pysleep_zero();
}
Comment thread
picnixz marked this conversation as resolved.
#else
PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout,
_PyTime_ROUND_CEILING);
// Maintain Windows Sleep() semantics for time.sleep(0)
if (timeout_100ns == 0) {
return pysleep_zero();
}
#endif

Expand Down Expand Up @@ -2300,21 +2305,6 @@ pysleep(PyTime_t timeout)

return 0;
#else // MS_WINDOWS
PyTime_t timeout_100ns = _PyTime_As100Nanoseconds(timeout,
_PyTime_ROUND_CEILING);

// Maintain Windows Sleep() semantics for time.sleep(0)
if (timeout_100ns == 0) {
Py_BEGIN_ALLOW_THREADS
// A value of zero causes the thread to relinquish the remainder of its
// time slice to any other thread that is ready to run. If there are no
// other threads ready to run, the function returns immediately, and
// the thread continues execution.
Sleep(0);
Py_END_ALLOW_THREADS
return 0;
}

LARGE_INTEGER relative_timeout;
// No need to check for integer overflow, both types are signed
assert(sizeof(relative_timeout) == sizeof(timeout_100ns));
Expand Down Expand Up @@ -2401,7 +2391,6 @@ pysleep(PyTime_t timeout)
}


#ifndef MS_WINDOWS
// time.sleep(0) optimized implementation.
// On error, raise an exception and return -1.
// On success, return 0.
Comment thread
picnixz marked this conversation as resolved.
Expand All @@ -2412,10 +2401,10 @@ pysleep(PyTime_t timeout)
// skip some calls to `PyTime_Monotonic()` and other checks when the timeout
// is zero. For details, see https://github.com/python/cpython/pull/128274.
static int
pysleep_zero_posix(void)
pysleep_zero(void)
{
assert(!PyErr_Occurred());

#ifndef MS_WINDOWS
int ret, err;
Py_BEGIN_ALLOW_THREADS
#ifdef HAVE_CLOCK_NANOSLEEP
Expand Down Expand Up @@ -2453,6 +2442,14 @@ pysleep_zero_posix(void)
if (PyErr_CheckSignals()) {
return -1;
}
#else
Comment thread
picnixz marked this conversation as resolved.
Outdated
Py_BEGIN_ALLOW_THREADS
// A value of zero causes the thread to relinquish the remainder of its
// time slice to any other thread that is ready to run. If there are no
// other threads ready to run, the function returns immediately, and
// the thread continues execution.
Sleep(0);
Py_END_ALLOW_THREADS
#endif
return 0;
}
#endif