From 2329769829336f50ed5635664e33e59f0135bd93 Mon Sep 17 00:00:00 2001 From: mattprodani Date: Mon, 4 Dec 2023 17:23:33 -0500 Subject: [PATCH 1/3] Use sem_clockwait with monotonic time when supported in parking_lot.c Completes one part of moving `parking_lot.c`'s semaphore waiting to prefer functions that support CLOCK_MONOTONIC. Uses sem_clockwait in the semaphore-based implementation. --- Python/parking_lot.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Python/parking_lot.c b/Python/parking_lot.c index 664e622cc17474..c61cae7337541b 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -118,10 +118,20 @@ _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 + // does not support CLOCK_MONOTONIC, use system clock _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); @@ -151,7 +161,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); } From 4a8dfac21d76ad056beb56ab0a0d6fb7016f882b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 4 Dec 2023 23:51:41 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst new file mode 100644 index 00000000000000..38a090a6d2ee94 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst @@ -0,0 +1 @@ +If ``sem_clockwait`` and ``CLOCK_MONOTONIC`` are supported, ``parking_lot.c`` will use ``sem_clockwait`` with monotonic time rather than system time in ``_PySemaphore_PlatformWait``. From 4271108b013aa771cbf494c65f2ac6ed182886cb Mon Sep 17 00:00:00 2001 From: mattprodani Date: Tue, 5 Dec 2023 22:44:49 -0500 Subject: [PATCH 3/3] Revert blurp and remove comment --- .../2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst | 1 - Python/parking_lot.c | 1 - 2 files changed, 2 deletions(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst deleted file mode 100644 index 38a090a6d2ee94..00000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2023-12-04-23-51-40.gh-issue-112606.pHvfIs.rst +++ /dev/null @@ -1 +0,0 @@ -If ``sem_clockwait`` and ``CLOCK_MONOTONIC`` are supported, ``parking_lot.c`` will use ``sem_clockwait`` with monotonic time rather than system time in ``_PySemaphore_PlatformWait``. diff --git a/Python/parking_lot.c b/Python/parking_lot.c index c61cae7337541b..d44c1b4b93b4d2 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -125,7 +125,6 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout) err = sem_clockwait(&sema->platform_sem, CLOCK_MONOTONIC, &ts); #else - // does not support CLOCK_MONOTONIC, use system clock _PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout); _PyTime_AsTimespec_clamp(deadline, &ts);