Skip to content

Commit

Permalink
pythongh-112606: Use sem_clockwait with monotonic time when supported…
Browse files Browse the repository at this point in the history
… in parking_lot.c (pythongh-112733)
  • Loading branch information
mattprodani authored and aisk committed Feb 11, 2024
1 parent f0e4933 commit 141cdf4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Python/parking_lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,19 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
if (timeout >= 0) {
struct timespec ts;

#if defined(CLOCK_MONOTONIC) && defined(HAVE_SEM_CLOCKWAIT)
_PyTime_t deadline = _PyTime_Add(_PyTime_GetMonotonicClock(), timeout);

_PyTime_AsTimespec_clamp(deadline, &ts);

err = sem_clockwait(&sema->platform_sem, CLOCK_MONOTONIC, &ts);
#else
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
_PyTime_AsTimespec(deadline, &ts);

_PyTime_AsTimespec_clamp(deadline, &ts);

err = sem_timedwait(&sema->platform_sem, &ts);
#endif
}
else {
err = sem_wait(&sema->platform_sem);
Expand Down Expand Up @@ -151,7 +160,7 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
struct timespec ts;

_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
_PyTime_AsTimespec(deadline, &ts);
_PyTime_AsTimespec_clamp(deadline, &ts);

err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
}
Expand Down

0 comments on commit 141cdf4

Please sign in to comment.