Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In Unix operating systems, :func:`time.sleep` now uses the ``nanosleep()`` function, if ``clock_nanosleep()`` is not available. ``nanosleep()`` allows to sleep with nanosecond precision.
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;
Comment thread
Livius90 marked this conversation as resolved.
Resolved

/* 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);
Comment thread
Livius90 marked this conversation as resolved.
Resolved
}
#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);
Comment thread
Livius90 marked this conversation as resolved.
Resolved
Py_END_ALLOW_THREADS
err = errno;
#endif
Expand Down