git: b7c2db4c772b - main - libthr: add pthread_mutex_clocklock(3)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 00:22:10 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=b7c2db4c772ba3c61f4760eca011332c9c12da1c

commit b7c2db4c772ba3c61f4760eca011332c9c12da1c
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-05 17:23:49 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-10 00:21:18 +0000

    libthr: add pthread_mutex_clocklock(3)
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61253
---
 include/pthread.h             |  5 +++++
 lib/libthr/pthread.map        |  1 +
 lib/libthr/thread/thr_mutex.c | 42 ++++++++++++++++++++++++++++--------------
 lib/libthr/thread/thr_umtx.c  |  6 +++---
 lib/libthr/thread/thr_umtx.h  |  4 ++--
 5 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/include/pthread.h b/include/pthread.h
index 1686b54314ef..9ff9748e699f 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -235,6 +235,11 @@ int		pthread_mutexattr_gettype(
 		    const pthread_mutexattr_t * __restrict, int * __restrict);
 int		pthread_mutexattr_settype(pthread_mutexattr_t *, int);
 int		pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
+#if __POSIX_VISIBLE >= 202405
+int		pthread_mutex_clocklock(pthread_mutex_t * __restrict __mutex,
+		    clockid_t, const struct timespec * __restrict)
+		    __trylocks_exclusive(0, *__mutex);
+#endif
 int		pthread_mutex_consistent(pthread_mutex_t * __mutex)
 		    __requires_exclusive(*__mutex);
 int		pthread_mutex_destroy(pthread_mutex_t * __mutex)
diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map
index 80a07d472a0e..214a7c8b4234 100644
--- a/lib/libthr/pthread.map
+++ b/lib/libthr/pthread.map
@@ -345,6 +345,7 @@ FBSD_1.8 {
 
 FBSD_1.9 {
 	 pthread_cond_clockwait;
+	 pthread_mutex_clocklock;
 	 pthread_query_np;
 	 pthread_query_all_np;
 	 pthread_tryjoin_np;
diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c
index f257a6868360..30ebdd15951f 100644
--- a/lib/libthr/thread/thr_mutex.c
+++ b/lib/libthr/thread/thr_mutex.c
@@ -67,6 +67,8 @@ _Static_assert(offsetof(struct pthread_mutex, m_qe) >= CACHE_LINE_SIZE,
  */
 int	__pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
 		const struct timespec * __restrict abstime);
+int	__pthread_mutex_clocklock(pthread_mutex_t * __restrict mutex,
+		clockid_t clock, const struct timespec * __restrict abstime);
 int	_pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count);
 int	_pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
 int	__pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
@@ -76,10 +78,10 @@ int	__pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count);
 
 static int	mutex_self_trylock(pthread_mutex_t);
 static int	mutex_self_lock(pthread_mutex_t,
-				const struct timespec *abstime);
+		    const struct timespec *abstime, clockid_t clockid);
 static int	mutex_unlock_common(struct pthread_mutex *, bool, int *);
 static int	mutex_lock_sleep(struct pthread *, pthread_mutex_t,
-				const struct timespec *);
+		    const struct timespec *, clockid_t clockid);
 static void	mutex_init_robust(struct pthread *curthread);
 static int	mutex_qidx(struct pthread_mutex *m);
 static bool	is_robust_mutex(struct pthread_mutex *m);
@@ -93,6 +95,7 @@ __weak_reference(__Tthr_mutex_lock, __pthread_mutex_lock);
 __strong_reference(__Tthr_mutex_lock, _pthread_mutex_lock);
 __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
 __strong_reference(__pthread_mutex_timedlock, _pthread_mutex_timedlock);
+__weak_reference(__pthread_mutex_clocklock, pthread_mutex_clocklock);
 __weak_reference(__Tthr_mutex_trylock, pthread_mutex_trylock);
 __weak_reference(__Tthr_mutex_trylock, __pthread_mutex_trylock);
 __strong_reference(__Tthr_mutex_trylock, _pthread_mutex_trylock);
@@ -656,14 +659,14 @@ __Tthr_mutex_trylock(pthread_mutex_t *mutex)
 
 static int
 mutex_lock_sleep(struct pthread *curthread, struct pthread_mutex *m,
-    const struct timespec *abstime)
+    const struct timespec *abstime, clockid_t clockid)
 {
 	uint32_t id, owner;
 	int count, ret;
 
 	id = TID(curthread);
 	if (PMUTEX_OWNER_ID(m) == id)
-		return (mutex_self_lock(m, abstime));
+		return (mutex_self_lock(m, abstime, clockid));
 
 	/*
 	 * For adaptive mutexes, spin for a bit in the expectation
@@ -709,10 +712,11 @@ sleep_in_kernel:
 	if (abstime == NULL)
 		ret = __thr_umutex_lock(&m->m_lock, id);
 	else if (__predict_false(abstime->tv_nsec < 0 ||
-	    abstime->tv_nsec >= 1000000000))
+	    abstime->tv_nsec >= 1000000000) ||
+	    _thr_check_clockid(clockid) != 0)
 		ret = EINVAL;
 	else
-		ret = __thr_umutex_timedlock(&m->m_lock, id, abstime);
+		ret = __thr_umutex_timedlock(&m->m_lock, id, abstime, clockid);
 done:
 	if (ret == 0 || ret == EOWNERDEAD) {
 		enqueue_mutex(curthread, m, ret);
@@ -724,7 +728,7 @@ done:
 
 static __always_inline int
 mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime,
-    bool cvattach, bool rb_onlist)
+    clockid_t clockid, bool cvattach, bool rb_onlist)
 {
 	struct pthread *curthread;
 	int ret, robust;
@@ -741,7 +745,7 @@ mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime,
 		if (ret == EOWNERDEAD)
 			m->m_lock.m_flags |= UMUTEX_NONCONSISTENT;
 	} else {
-		ret = mutex_lock_sleep(curthread, m, abstime);
+		ret = mutex_lock_sleep(curthread, m, abstime, clockid);
 	}
 	if (!rb_onlist && robust)
 		_mutex_leave_robust(curthread, m);
@@ -760,12 +764,12 @@ __Tthr_mutex_lock(pthread_mutex_t *mutex)
 	_thr_check_init();
 	ret = check_and_init_mutex(mutex, &m);
 	if (ret == 0)
-		ret = mutex_lock_common(m, NULL, false, false);
+		ret = mutex_lock_common(m, NULL, CLOCK_REALTIME, false, false);
 	return (ret);
 }
 
 int
-__pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
+__pthread_mutex_clocklock(pthread_mutex_t * __restrict mutex, clockid_t clockid,
     const struct timespec * __restrict abstime)
 {
 	struct pthread_mutex *m;
@@ -774,10 +778,17 @@ __pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
 	_thr_check_init();
 	ret = check_and_init_mutex(mutex, &m);
 	if (ret == 0)
-		ret = mutex_lock_common(m, abstime, false, false);
+		ret = mutex_lock_common(m, abstime, clockid, false, false);
 	return (ret);
 }
 
+int
+__pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
+    const struct timespec * __restrict abstime)
+{
+	return (__pthread_mutex_clocklock(mutex, CLOCK_REALTIME, abstime));
+}
+
 int
 _thr_mutex_unlock(pthread_mutex_t *mutex)
 {
@@ -799,7 +810,7 @@ _mutex_cv_lock(struct pthread_mutex *m, int count, bool rb_onlist)
 {
 	int error;
 
-	error = mutex_lock_common(m, NULL, true, rb_onlist);
+	error = mutex_lock_common(m, NULL, CLOCK_REALTIME, true, rb_onlist);
 	if (error == 0 || error == EOWNERDEAD)
 		m->m_count = count;
 	return (error);
@@ -891,7 +902,8 @@ mutex_self_trylock(struct pthread_mutex *m)
 }
 
 static int
-mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime)
+mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime,
+    clockid_t clockid)
 {
 	struct timespec	ts1, ts2;
 	int ret;
@@ -916,8 +928,10 @@ mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime)
 			if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
 			    abstime->tv_nsec >= 1000000000) {
 				ret = EINVAL;
+			} else if (_thr_check_clockid(clockid) != 0 ||
+			    clock_gettime(clockid, &ts1) == -1) {
+				ret = EINVAL;
 			} else {
-				clock_gettime(CLOCK_REALTIME, &ts1);
 				TIMESPEC_SUB(&ts2, abstime, &ts1);
 				__sys_nanosleep(&ts2, NULL);
 				ret = ETIMEDOUT;
diff --git a/lib/libthr/thread/thr_umtx.c b/lib/libthr/thread/thr_umtx.c
index edd0dacdaff1..410e9c181235 100644
--- a/lib/libthr/thread/thr_umtx.c
+++ b/lib/libthr/thread/thr_umtx.c
@@ -108,7 +108,7 @@ __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id)
 
 int
 __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
-	const struct timespec *abstime)
+    const struct timespec *abstime, clockid_t clockid)
 {
 	struct _umtx_time *tm_p, timeout;
 	size_t tm_size;
@@ -119,7 +119,7 @@ __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
 		tm_p = NULL;
 		tm_size = 0;
 	} else {
-		timeout._clockid = CLOCK_REALTIME;
+		timeout._clockid = clockid;
 		timeout._flags = UMTX_ABSTIME;
 		timeout._timeout = *abstime;
 		tm_p = &timeout;
@@ -151,7 +151,7 @@ __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
 			    ret == ENOTRECOVERABLE)
 				break;
 		}
-		if (ret == ETIMEDOUT)
+		if (ret == ETIMEDOUT || ret == EINVAL)
 			break;
 	}
 	return (ret);
diff --git a/lib/libthr/thread/thr_umtx.h b/lib/libthr/thread/thr_umtx.h
index eada8ab9297f..2ddc28af1df1 100644
--- a/lib/libthr/thread/thr_umtx.h
+++ b/lib/libthr/thread/thr_umtx.h
@@ -42,7 +42,7 @@
 int __thr_umutex_lock(struct umutex *mtx, uint32_t id) __hidden;
 int __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id) __hidden;
 int __thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
-	const struct timespec *timeout) __hidden;
+	const struct timespec *timeout, clockid_t clockid) __hidden;
 int __thr_umutex_unlock(struct umutex *mtx) __hidden;
 int __thr_umutex_trylock(struct umutex *mtx) __hidden;
 int __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling,
@@ -145,7 +145,7 @@ _thr_umutex_timedlock(struct umutex *mtx, uint32_t id,
 
 	if (_thr_umutex_trylock2(mtx, id) == 0)
 		return (0);
-	return (__thr_umutex_timedlock(mtx, id, timeout));
+	return (__thr_umutex_timedlock(mtx, id, timeout, CLOCK_REALTIME));
 }
 
 static inline int