From 6e6eb242188e8312a6a8a78e6be0d11cd1036610 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 18 Mar 2026 21:17:10 +0000 Subject: MacOS doesn't have pthread_mutex_timedlock -_- --- src/libposixplz/pthread_mutex_tailock.c | 41 +++++++++++++++++++++++++++++++++ src/libstddjb/pthread_mutex_tailock.c | 28 ---------------------- src/sysdeps/trypthreadmutexclocklock.c | 4 ++-- src/sysdeps/trypthreadmutextimedlock.c | 11 +++++++++ 4 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 src/libposixplz/pthread_mutex_tailock.c delete mode 100644 src/libstddjb/pthread_mutex_tailock.c create mode 100644 src/sysdeps/trypthreadmutextimedlock.c (limited to 'src') diff --git a/src/libposixplz/pthread_mutex_tailock.c b/src/libposixplz/pthread_mutex_tailock.c new file mode 100644 index 0000000..736921c --- /dev/null +++ b/src/libposixplz/pthread_mutex_tailock.c @@ -0,0 +1,41 @@ +/* ISC license. */ + +#include +#include +#include + +#include +#include +#include + +#ifdef SKALIBS_HASPTHREADMUTEXTIMEDLOCK + +int pthread_mutex_tailock (pthread_mutex_t *mtx, tain const *deadline, tain *stamp) +{ + int e ; + struct timespec ts ; + tain tto ; + if (!stamp) return pthread_mutex_lock(mtx) ; + tain_sub(&tto, deadline, stamp) ; + if (!timespec_from_tain_relative(&ts, &tto)) return errno ; +#ifdef SKALIBS_HASPTHREADMUTEXCLOCKLOCK + if (&tain_now != &tain_wallclock_read) + e = pthread_mutex_clocklock(mtx, CLOCK_MONOTONIC, &ts) ; + else +#else + e = pthread_mutex_timedlock(mtx, &ts) ; +#endif + tain_now(stamp) ; + return e ; +} + +#else + +int pthread_mutex_tailock (pthread_mutex_t *mtx, tain const *deadline, tain *stamp) +{ + (void)deadline ; + (void)stamp ; + return pthread_mutex_lock(mtx) ; +} + +#endif diff --git a/src/libstddjb/pthread_mutex_tailock.c b/src/libstddjb/pthread_mutex_tailock.c deleted file mode 100644 index 8f8fbf3..0000000 --- a/src/libstddjb/pthread_mutex_tailock.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -#include -#include -#include - -int pthread_mutex_tailock (pthread_mutex_t *mtx, tain const *deadline, tain *stamp) -{ - int e ; - struct timespec ts ; - tain tto ; - if (!stamp) return pthread_mutex_lock(mtx) ; - tain_sub(&tto, deadline, stamp) ; - if (!timespec_from_tain_relative(&ts, &tto)) return errno ; -#ifdef SKALIBS_HASPTHREADMUTEXCLOCKLOCK - if (&tain_now != &tain_wallclock_read) - e = pthread_mutex_clocklock(mtx, CLOCK_MONOTONIC, &ts) ; - else -#else - e = pthread_mutex_timedlock(mtx, &ts) ; -#endif - tain_now(stamp) ; - return e ; -} diff --git a/src/sysdeps/trypthreadmutexclocklock.c b/src/sysdeps/trypthreadmutexclocklock.c index b102db2..4a84091 100644 --- a/src/sysdeps/trypthreadmutexclocklock.c +++ b/src/sysdeps/trypthreadmutexclocklock.c @@ -5,7 +5,7 @@ int main (void) { - pthread_mutex_t mtx = PTHREAD_MUTEX_INIT ; - strict timespec ts = { .tv_sec = 0, .tv_nsec = 1 } ; + pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER ; + struct timespec ts = { .tv_sec = 0, .tv_nsec = 1 } ; return pthread_mutex_clocklock(&mtx, CLOCK_MONOTONIC, &ts) ; } diff --git a/src/sysdeps/trypthreadmutextimedlock.c b/src/sysdeps/trypthreadmutextimedlock.c new file mode 100644 index 0000000..f76f198 --- /dev/null +++ b/src/sysdeps/trypthreadmutextimedlock.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include + +int main (void) +{ + pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER ; + struct timespec ts = { .tv_sec = 0, .tv_nsec = 1 } ; + return pthread_mutex_timedlock(&mtx, &ts) ; +} -- cgit v1.3.1