8000 bpo-21302: Add nanosleep() implementation for time.sleep() in Unix by Livius90 · Pull Request #28526 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-21302: Add nanosleep() implementation for time.sleep() in Unix #28526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
replace select() to nanosleep()
Replace outdated select() to use nanosleep() for sleep implementation in Unix
  • Loading branch information
Livius90 committed Sep 22, 2021
commit 0f39807d206644c25d1c5cfcbfc04f51cc965b41
22 changes: 11 additions & 11 deletions Modules/_multiprocessing/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
{
int res;
unsigned long delay, difference;
struct timeval now, tvdeadline, tvdelay;
struct timespec tsdeadline, tsdelay;
struct timeval now;

errno = 0;
tvdeadline.tv_sec = deadline->tv_sec;
tvdeadline.tv_usec = deadline->tv_nsec / 1000;
tsdeadline = *deadline;

for (delay = 0 ; ; delay += 1000) {
/* poll */
Expand All @@ -253,16 +253,16 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
return MP_STANDARD_ERROR;

/* check for timeout */
if (tvdeadline.tv_sec < now.tv_sec ||
(tvdeadline.tv_sec == now.tv_sec &&
tvdeadline.tv_usec <= now.tv_usec)) {
if (tsdeadline.tv_sec < now.tv_sec ||
(tsdeadline.tv_sec == now.tv_sec &&
tsdeadline.tv_nsec <= (now.tv_usec*1000))) {
errno = ETIMEDOUT;
return MP_STANDARD_ERROR;
}

/* calculate how much time is left */
difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
(tvdeadline.tv_usec - now.tv_usec);
difference = (tsdeadline.tv_sec - now.tv_sec) * 1000000 +
((tsdeadline.tv_nsec/1000) - now.tv_usec);

/* check delay not too long -- maximum is 20 msecs */
if (delay > 20000)
Expand All @@ -271,9 +271,9 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
delay = difference;

/* sleep */
tvdelay.tv_sec = delay / 1000000;
tvdelay.tv_usec = delay % 1000000;
if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
tsdelay.tv_sec = delay / 1000000;
tsdelay.tv_nsec = (delay % 1000000) * 1000;
if (nanosleep(&tsdelay, NULL) < 0)
return MP_STANDARD_ERROR;

/* check for signals */
Expand Down
7 changes: 3 additions & 4 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,10 @@ static int Tkinter_busywaitinterval = 20;
static void
Sleep(int milli)
{
/* XXX Too bad if you don't have select(). */
struct timeval t;
struct timespec t;
t.tv_sec = milli/1000;
t.tv_usec = (milli%1000) * 1000;
select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
t.tv_nsec = (milli%1000) * 1000000;
nanosleep(&t, NULL);
}
#endif /* MS_WINDOWS */

Expand Down
6 changes: 3 additions & 3 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ pysleep(_PyTime_t secs)
#ifdef HAVE_CLOCK_NANOSLEEP
struct timespec timeout_abs;
#else
struct timeval timeout;
struct timespec timeout;
#endif
_PyTime_t deadline, monotonic;
int err = 0;
Expand All @@ -2075,7 +2075,7 @@ pysleep(_PyTime_t secs)

do {
#ifndef HAVE_CLOCK_NANOSLEEP
if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_CEILING) < 0) {
if (_PyTime_AsTimespec(secs, &timeout) < 0) {
return -1;
}
#endif
Expand All @@ -2088,7 +2088,7 @@ pysleep(_PyTime_t secs)
err = ret;
#else
Py_BEGIN_ALLOW_THREADS
ret = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
ret = nanosleep(&timeout, NULL);
Py_END_ALLOW_THREADS
err = errno;
#endif
Expand Down
0