Skip to content

Commit e5e1866

Browse files
authored
gh-112606: Use pthread_cond_timedwait_relative_np() in parking_lot.c when available (#112616)
Add a configure define for HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP and replaces pthread_cond_timedwait() with pthread_cond_timedwait_relative_np() for relative time when supported in semaphore waiting logic.
1 parent fda7445 commit e5e1866

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

Python/parking_lot.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,15 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
158158
if (sema->counter == 0) {
159159
if (timeout >= 0) {
160160
struct timespec ts;
161-
161+
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
162+
_PyTime_AsTimespec_clamp(timeout, &ts);
163+
err = pthread_cond_timedwait_relative_np(&sema->cond, &sema->mutex, &ts);
164+
#else
162165
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
163166
_PyTime_AsTimespec_clamp(deadline, &ts);
164167

165168
err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
169+
#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
166170
}
167171
else {
168172
err = pthread_cond_wait(&sema->cond, &sema->mutex);

configure

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

+2-2
Original file line numberDiff line numberDiff line change
@@ -4796,8 +4796,8 @@ AC_CHECK_FUNCS([ \
47964796
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe \
47974797
pipe2 plock poll posix_fadvise posix_fallocate posix_openpt posix_spawn posix_spawnp \
47984798
posix_spawn_file_actions_addclosefrom_np \
4799-
pread preadv preadv2 pthread_condattr_setclock pthread_init pthread_kill ptsname ptsname_r \
4800-
pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
4799+
pread preadv preadv2 pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
4800+
pthread_kill ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
48014801
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
48024802
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
48034803
sem_timedwait sem_unlink sendfile setegid seteuid setgid sethostname \

pyconfig.h.in

+4
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@
936936
/* Define to 1 if you have the `pthread_condattr_setclock' function. */
937937
#undef HAVE_PTHREAD_CONDATTR_SETCLOCK
938938

939+
/* Define to 1 if you have the `pthread_cond_timedwait_relative_np' function.
940+
*/
941+
#undef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
942+
939943
/* Defined for Solaris 2.6 bug in pthread header. */
940944
#undef HAVE_PTHREAD_DESTRUCTOR
941945

0 commit comments

Comments
 (0)