Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112606: Use pthread_cond_timedwait_relative_np in parking_lot.c when available #112616

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use pthread_cond_timedwait_relative_np for MacOS futex implementati…
…on in

`parking_lot.c`

Adds 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.
  • Loading branch information
mattprodani committed Jan 29, 2024

Verified

This commit was signed with the committer’s verified signature.
frostming Frost Ming
commit 2983bb7a8fe014dd6fc4051f2874156706455ea0
7 changes: 6 additions & 1 deletion Python/parking_lot.c
Original file line number Diff line number Diff line change
@@ -155,14 +155,19 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
#else
pthread_mutex_lock(&sema->mutex);
int err = 0;

mattprodani marked this conversation as resolved.
Show resolved Hide resolved
if (sema->counter == 0) {
if (timeout >= 0) {
struct timespec ts;

#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
_PyTime_AsTimespec_clamp(timeout, &ts);
err = pthread_cond_timedwait_relative_np(&sema->cond, &sema->mutex, &ts);
#else
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);

err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
}
else {
err = pthread_cond_wait(&sema->cond, &sema->mutex);
6 changes: 6 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -4796,8 +4796,8 @@ AC_CHECK_FUNCS([ \
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe \
pipe2 plock poll posix_fadvise posix_fallocate posix_openpt posix_spawn posix_spawnp \
posix_spawn_file_actions_addclosefrom_np \
pread preadv preadv2 pthread_condattr_setclock pthread_init pthread_kill ptsname ptsname_r \
pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
pread preadv preadv2 pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
pthread_kill ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \
sem_timedwait sem_unlink sendfile setegid seteuid setgid sethostname \
4 changes: 4 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
@@ -936,6 +936,10 @@
/* Define to 1 if you have the `pthread_condattr_setclock' function. */
#undef HAVE_PTHREAD_CONDATTR_SETCLOCK

/* Define to 1 if you have the `pthread_cond_timedwait_relative_np' function.
*/
#undef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP

/* Defined for Solaris 2.6 bug in pthread header. */
#undef HAVE_PTHREAD_DESTRUCTOR