]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112606: Use sem_clockwait with monotonic time when supported in parking_lot.c...
authorMatt Prodani <mattp@nyu.edu>
Wed, 6 Dec 2023 06:54:57 +0000 (01:54 -0500)
committerGitHub <noreply@github.com>
Wed, 6 Dec 2023 06:54:57 +0000 (15:54 +0900)
Python/parking_lot.c

index 664e622cc1747411fe59ebcfa1f00d2738ed2972..d44c1b4b93b4d2b1140ad4371461c44b025ae320 100644 (file)
@@ -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);
@@ -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);
         }