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

Skip to content

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

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

Merged
merged 8 commits into from
Sep 25, 2021
Merged
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
Prev Previous commit
Next Next commit
revert some changes
  • Loading branch information
Livius90 authored and vstinner committed Sep 24, 2021
commit b5142601a80d3cff121569762f0ab1d1967f4c05
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 timespec tsdeadline, tsdelay;
struct timeval now;
struct timeval now, tvdeadline, tvdelay;

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

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 (tsdeadline.tv_sec < now.tv_sec ||
(tsdeadline.tv_sec == now.tv_sec &&
tsdeadline.tv_nsec <= (now.tv_usec*1000))) {
if (tvdeadline.tv_sec < now.tv_sec ||
(tvdeadline.tv_sec == now.tv_sec &&
tvdeadline.tv_usec <= now.tv_usec)) {
errno = ETIMEDOUT;
return MP_STANDARD_ERROR;
}

/* calculate how much time is left */
difference = (tsdeadline.tv_sec - now.tv_sec) * 1000000 +
((tsdeadline.tv_nsec/1000) - now.tv_usec);
difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
(tvdeadline.tv_usec - 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 */
tsdelay.tv_sec = delay / 1000000;
tsdelay.tv_nsec = (delay % 1000000) * 1000;
if (nanosleep(&tsdelay, NULL) < 0)
tvdelay.tv_sec = delay / 1000000;
tvdelay.tv_usec = delay % 1000000;
if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
return MP_STANDARD_ERROR;

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

Expand Down
0