Skip to content

Commit a2a46f9

Browse files
authored
gh-112606: Use sem_clockwait with monotonic time when supported in parking_lot.c (gh-112733)
1 parent d384813 commit a2a46f9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Diff for: Python/parking_lot.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,19 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
118118
if (timeout >= 0) {
119119
struct timespec ts;
120120

121+
#if defined(CLOCK_MONOTONIC) && defined(HAVE_SEM_CLOCKWAIT)
122+
_PyTime_t deadline = _PyTime_Add(_PyTime_GetMonotonicClock(), timeout);
123+
124+
_PyTime_AsTimespec_clamp(deadline, &ts);
125+
126+
err = sem_clockwait(&sema->platform_sem, CLOCK_MONOTONIC, &ts);
127+
#else
121128
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
122-
_PyTime_AsTimespec(deadline, &ts);
129+
130+
_PyTime_AsTimespec_clamp(deadline, &ts);
123131

124132
err = sem_timedwait(&sema->platform_sem, &ts);
133+
#endif
125134
}
126135
else {
127136
err = sem_wait(&sema->platform_sem);
@@ -151,7 +160,7 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
151160
struct timespec ts;
152161

153162
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
154-
_PyTime_AsTimespec(deadline, &ts);
163+
_PyTime_AsTimespec_clamp(deadline, &ts);
155164

156165
err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
157166
}

0 commit comments

Comments
 (0)