Skip to content

Commit c7aa281

Browse files
committed
Use pthread_cond_timedwait_relative_np for MacOS futex implementation 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.
1 parent b920d6c commit c7aa281

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

Python/parking_lot.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,19 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, _PyTime_t timeout)
155155
#else
156156
pthread_mutex_lock(&sema->mutex);
157157
int err = 0;
158+
158159
if (sema->counter == 0) {
159160
if (timeout >= 0) {
160161
struct timespec ts;
161-
162+
#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
163+
_PyTime_AsTimespec_clamp(timeout, &ts);
164+
err = pthread_cond_timedwait_relative_np(&sema->cond, &sema->mutex, &ts);
165+
#else
162166
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
163167
_PyTime_AsTimespec_clamp(deadline, &ts);
164168

165169
err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
170+
#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
166171
}
167172
else {
168173
err = pthread_cond_wait(&sema->cond, &sema->mutex);

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,9 +4753,9 @@ AC_CHECK_FUNCS([ \
47534753
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
47544754
getspnam getuid getwd if_nameindex initgroups kill killpg lchown linkat \
47554755
lockf lstat lutimes madvise mbrtowc memrchr mkdirat mkfifo mkfifoat \
4756-
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe \
4757-
pipe2 plock poll posix_fadvise posix_fallocate posix_spawn posix_spawnp \
4758-
pread preadv preadv2 pthread_condattr_setclock pthread_init pthread_kill \
4756+
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe pipe2 \
4757+
plock poll posix_fadvise posix_fallocate posix_spawn posix_spawnp pread preadv \
4758+
preadv2 pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init pthread_kill \
47594759
pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
47604760
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
47614761
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \

pyconfig.h.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@
923923
/* Define to 1 if you have the `pthread_condattr_setclock' function. */
924924
#undef HAVE_PTHREAD_CONDATTR_SETCLOCK
925925

926+
/* Define to 1 if you have the `pthread_cond_timedwait_relative_np' function.
927+
*/
928+
#undef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
929+
926930
/* Defined for Solaris 2.6 bug in pthread header. */
927931
#undef HAVE_PTHREAD_DESTRUCTOR
928932

0 commit comments

Comments
 (0)