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
Next Next commit
Ensure that time.sleep(0) does not accumulate delays.
On non-Windows platforms, this reverts the usage of `clock_nanosleep`
and `nanosleep` introduced by 85a4748 and 7834ff2 respectively,
falling back to a `select(2)` alternative instead.
  • Loading branch information
picnixz committed Dec 26, 2024
commit 3754e9e02f5195243d83f327bb1c9ec1ec8f8780
49 changes: 49 additions & 0 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module time

/* Forward declarations */
static int pysleep(PyTime_t timeout);
#ifndef MS_WINDOWS
static int pysleep_zero_posix(void);
#endif


typedef struct {
Expand Down Expand Up @@ -2213,6 +2216,11 @@ static int
pysleep(PyTime_t timeout)
{
assert(timeout >= 0);
#ifndef MS_WINDOWS
if (timeout == 0) {
return pysleep_zero_posix();
}
Comment thread
picnixz marked this conversation as resolved.
#endif

#ifndef MS_WINDOWS
#ifdef HAVE_CLOCK_NANOSLEEP
Expand Down Expand Up @@ -2390,3 +2398,44 @@ pysleep(PyTime_t timeout)
return -1;
#endif
}


#ifndef MS_WINDWOS
// 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.
static int
pysleep_zero_posix(void)
Comment thread
picnixz marked this conversation as resolved.
Outdated
{
static struct timeval zero = {0, 0};
Comment thread
picnixz marked this conversation as resolved.
Outdated

PyTime_t deadline, monotonic;
if (PyTime_Monotonic(&monotonic) < 0) {
return -1;
}
deadline = monotonic;
do {
int ret, err;
Py_BEGIN_ALLOW_THREADS
ret = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &zero);
Comment thread
picnixz marked this conversation as resolved.
Outdated
err = errno;
Comment thread
picnixz marked this conversation as resolved.
Outdated
Py_END_ALLOW_THREADS
if (ret == 0) {
break;
}
if (err != EINTR) {
errno = err;
Comment thread
picnixz marked this conversation as resolved.
Outdated
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
/* sleep was interrupted by SIGINT */
if (PyErr_CheckSignals()) {
return -1;
}
if (PyTime_Monotonic(&monotonic) < 0) {
return -1;
}
} while (monotonic == deadline);
return 0;
}
#endif