git: be6740dcad3c - stable/15 - libthr: add pthread_rwlock_clock{rd,wr}lock(3)

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 01:09:49 UTC
The branch stable/15 has been updated by kib:

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

commit be6740dcad3ca21068b9a185fa94e50e8f331350
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-05 17:24:12 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-17 00:25:02 +0000

    libthr: add pthread_rwlock_clock{rd,wr}lock(3)
    
    (cherry picked from commit 5577567bd0d0c695c25ddb4ea32b8c8231d8b0fe)
---
 include/pthread.h              | 10 ++++++++++
 lib/libthr/pthread.map         |  2 ++
 lib/libthr/thread/thr_rwlock.c | 42 ++++++++++++++++++++++++++++++++++--------
 lib/libthr/thread/thr_umtx.c   | 14 ++++++++------
 lib/libthr/thread/thr_umtx.h   |  8 ++++----
 5 files changed, 58 insertions(+), 18 deletions(-)

diff --git a/include/pthread.h b/include/pthread.h
index 9ff9748e699f..9ee552f2a8b2 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -257,6 +257,16 @@ int		pthread_mutex_timedlock(pthread_mutex_t * __restrict __mutex,
 int		pthread_mutex_unlock(pthread_mutex_t * __mutex)
 		    __unlocks(*__mutex);
 int		pthread_once(pthread_once_t *, void (*) (void));
+#if __POSIX_VISIBLE >= 202405
+int		pthread_rwlock_clockrdlock(
+		    pthread_rwlock_t * __restrict __rwlock,
+		    clockid_t, const struct timespec * __restrict)
+		    __trylocks_shared(0, *__rwlock);
+int		pthread_rwlock_clockwrlock(
+		    pthread_rwlock_t * __restrict __rwlock,
+		    clockid_t, const struct timespec * __restrict)
+		    __trylocks_exclusive(0, *__rwlock);
+#endif
 int		pthread_rwlock_destroy(pthread_rwlock_t * __rwlock)
 		    __requires_unlocked(*__rwlock);
 int		pthread_rwlock_init(pthread_rwlock_t * __restrict __rwlock,
diff --git a/lib/libthr/pthread.map b/lib/libthr/pthread.map
index 214a7c8b4234..8dc565a95858 100644
--- a/lib/libthr/pthread.map
+++ b/lib/libthr/pthread.map
@@ -348,5 +348,7 @@ FBSD_1.9 {
 	 pthread_mutex_clocklock;
 	 pthread_query_np;
 	 pthread_query_all_np;
+	 pthread_rwlock_clockrdlock;
+	 pthread_rwlock_clockwrlock;
 	 pthread_tryjoin_np;
 };
diff --git a/lib/libthr/thread/thr_rwlock.c b/lib/libthr/thread/thr_rwlock.c
index 5fd1e1dcb14a..96e534431553 100644
--- a/lib/libthr/thread/thr_rwlock.c
+++ b/lib/libthr/thread/thr_rwlock.c
@@ -39,6 +39,11 @@
 _Static_assert(sizeof(struct pthread_rwlock) <= THR_PAGE_SIZE_MIN,
     "pthread_rwlock is too large for off-page");
 
+int _pthread_rwlock_clockrdlock(pthread_rwlock_t * __restrict rwlock,
+    clockid_t clockid, const struct timespec * __restrict abstime);
+int _pthread_rwlock_clockwrlock(pthread_rwlock_t * __restrict rwlock,
+    clockid_t clockid, const struct timespec * __restrict abstime);
+
 __weak_reference(_thr_rwlock_destroy, pthread_rwlock_destroy);
 __weak_reference(_thr_rwlock_destroy, _pthread_rwlock_destroy);
 __weak_reference(_thr_rwlock_init, pthread_rwlock_init);
@@ -46,6 +51,7 @@ __weak_reference(_thr_rwlock_init, _pthread_rwlock_init);
 __weak_reference(_Tthr_rwlock_rdlock, pthread_rwlock_rdlock);
 __weak_reference(_Tthr_rwlock_rdlock, _pthread_rwlock_rdlock);
 __weak_reference(_pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock);
+__weak_reference(_pthread_rwlock_clockrdlock, pthread_rwlock_clockrdlock);
 __weak_reference(_Tthr_rwlock_tryrdlock, pthread_rwlock_tryrdlock);
 __weak_reference(_Tthr_rwlock_tryrdlock, _pthread_rwlock_tryrdlock);
 __weak_reference(_Tthr_rwlock_trywrlock, pthread_rwlock_trywrlock);
@@ -55,6 +61,7 @@ __weak_reference(_Tthr_rwlock_unlock, _pthread_rwlock_unlock);
 __weak_reference(_Tthr_rwlock_wrlock, pthread_rwlock_wrlock);
 __weak_reference(_Tthr_rwlock_wrlock, _pthread_rwlock_wrlock);
 __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock);
+__weak_reference(_pthread_rwlock_clockwrlock, pthread_rwlock_clockwrlock);
 
 static int init_static(struct pthread *thread, pthread_rwlock_t *rwlock);
 static int init_rwlock(pthread_rwlock_t *rwlock, pthread_rwlock_t *rwlock_out);
@@ -194,7 +201,8 @@ abstime_invalid(const struct timespec *abstime)
 }
 
 static int
-rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
+rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime,
+    clockid_t clockid)
 {
 	struct pthread *curthread = _get_curthread();
 	pthread_rwlock_t prwlock;
@@ -222,7 +230,8 @@ rwlock_rdlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
 
 	for (;;) {
 		/* goto kernel and lock it */
-		ret = __thr_rwlock_rdlock(&prwlock->lock, flags, abstime);
+		ret = __thr_rwlock_rdlock(&prwlock->lock, flags, abstime,
+		    clockid);
 		if (ret != EINTR)
 			break;
 
@@ -241,7 +250,7 @@ int
 _Tthr_rwlock_rdlock(pthread_rwlock_t *rwlock)
 {
 	_thr_check_init();
-	return (rwlock_rdlock_common(rwlock, NULL));
+	return (rwlock_rdlock_common(rwlock, NULL, CLOCK_REALTIME));
 }
 
 int
@@ -249,7 +258,15 @@ _pthread_rwlock_timedrdlock(pthread_rwlock_t * __restrict rwlock,
     const struct timespec * __restrict abstime)
 {
 	_thr_check_init();
-	return (rwlock_rdlock_common(rwlock, abstime));
+	return (rwlock_rdlock_common(rwlock, abstime, CLOCK_REALTIME));
+}
+
+int
+_pthread_rwlock_clockrdlock(pthread_rwlock_t * __restrict rwlock,
+    clockid_t clockid, const struct timespec * __restrict abstime)
+{
+	_thr_check_init();
+	return (rwlock_rdlock_common(rwlock, abstime, clockid));
 }
 
 int
@@ -291,7 +308,8 @@ _Tthr_rwlock_trywrlock(pthread_rwlock_t *rwlock)
 }
 
 static int
-rwlock_wrlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
+rwlock_wrlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime,
+    clockid_t clockid)
 {
 	struct pthread *curthread = _get_curthread();
 	pthread_rwlock_t prwlock;
@@ -316,7 +334,7 @@ rwlock_wrlock_common(pthread_rwlock_t *rwlock, const struct timespec *abstime)
 
 	for (;;) {
 		/* goto kernel and lock it */
-		ret = __thr_rwlock_wrlock(&prwlock->lock, abstime);
+		ret = __thr_rwlock_wrlock(&prwlock->lock, abstime, clockid);
 		if (ret == 0) {
 			prwlock->owner = TID(curthread);
 			break;
@@ -339,7 +357,7 @@ int
 _Tthr_rwlock_wrlock(pthread_rwlock_t *rwlock)
 {
 	_thr_check_init();
-	return (rwlock_wrlock_common(rwlock, NULL));
+	return (rwlock_wrlock_common(rwlock, NULL, CLOCK_REALTIME));
 }
 
 int
@@ -347,7 +365,15 @@ _pthread_rwlock_timedwrlock(pthread_rwlock_t * __restrict rwlock,
     const struct timespec * __restrict abstime)
 {
 	_thr_check_init();
-	return (rwlock_wrlock_common(rwlock, abstime));
+	return (rwlock_wrlock_common(rwlock, abstime, CLOCK_REALTIME));
+}
+
+int
+_pthread_rwlock_clockwrlock(pthread_rwlock_t * __restrict rwlock,
+    clockid_t clockid, const struct timespec * __restrict abstime)
+{
+	_thr_check_init();
+	return (rwlock_wrlock_common(rwlock, abstime, clockid));
 }
 
 int
diff --git a/lib/libthr/thread/thr_umtx.c b/lib/libthr/thread/thr_umtx.c
index 410e9c181235..5095cc2328ba 100644
--- a/lib/libthr/thread/thr_umtx.c
+++ b/lib/libthr/thread/thr_umtx.c
@@ -292,7 +292,7 @@ _thr_ucond_broadcast(struct ucond *cv)
 
 int
 __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
-	const struct timespec *tsp)
+    const struct timespec *tsp, clockid_t clockid)
 {
 	struct _umtx_time timeout, *tm_p;
 	size_t tm_size;
@@ -303,7 +303,7 @@ __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
 	} else {
 		timeout._timeout = *tsp;
 		timeout._flags = UMTX_ABSTIME;
-		timeout._clockid = CLOCK_REALTIME;
+		timeout._clockid = clockid;
 		tm_p = &timeout;
 		tm_size = sizeof(timeout);
 	}
@@ -312,7 +312,8 @@ __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
 }
 
 int
-__thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp)
+__thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp,
+    clockid_t clockid)
 {
 	struct _umtx_time timeout, *tm_p;
 	size_t tm_size;
@@ -323,7 +324,7 @@ __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp)
 	} else {
 		timeout._timeout = *tsp;
 		timeout._flags = UMTX_ABSTIME;
-		timeout._clockid = CLOCK_REALTIME;
+		timeout._clockid = clockid;
 		tm_p = &timeout;
 		tm_size = sizeof(timeout);
 	}
@@ -346,7 +347,8 @@ _thr_rwl_rdlock(struct urwlock *rwlock)
 	for (;;) {
 		if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0)
 			return;
-		ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL);
+		ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL,
+		    CLOCK_REALTIME);
 		if (ret == 0)
 			return;
 		if (ret != EINTR)
@@ -362,7 +364,7 @@ _thr_rwl_wrlock(struct urwlock *rwlock)
 	for (;;) {
 		if (_thr_rwlock_trywrlock(rwlock) == 0)
 			return;
-		ret = __thr_rwlock_wrlock(rwlock, NULL);
+		ret = __thr_rwlock_wrlock(rwlock, NULL, CLOCK_REALTIME);
 		if (ret == 0)
 			return;
 		if (ret != EINTR)
diff --git a/lib/libthr/thread/thr_umtx.h b/lib/libthr/thread/thr_umtx.h
index 2ddc28af1df1..bd68e7c2a0fd 100644
--- a/lib/libthr/thread/thr_umtx.h
+++ b/lib/libthr/thread/thr_umtx.h
@@ -66,9 +66,9 @@ int _thr_ucond_signal(struct ucond *cv) __hidden;
 int _thr_ucond_broadcast(struct ucond *cv) __hidden;
 
 int __thr_rwlock_rdlock(struct urwlock *rwlock, int flags,
-	const struct timespec *tsp) __hidden;
+	const struct timespec *tsp, clockid_t clockid) __hidden;
 int __thr_rwlock_wrlock(struct urwlock *rwlock,
-	const struct timespec *tsp) __hidden;
+	const struct timespec *tsp, clockid_t clockid) __hidden;
 int __thr_rwlock_unlock(struct urwlock *rwlock) __hidden;
 
 /* Internal used only */
@@ -233,7 +233,7 @@ _thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp)
 
 	if (_thr_rwlock_tryrdlock(rwlock, flags) == 0)
 		return (0);
-	return (__thr_rwlock_rdlock(rwlock, flags, tsp));
+	return (__thr_rwlock_rdlock(rwlock, flags, tsp, CLOCK_REALTIME));
 }
 
 static inline int
@@ -242,7 +242,7 @@ _thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp)
 
 	if (_thr_rwlock_trywrlock(rwlock) == 0)
 		return (0);
-	return (__thr_rwlock_wrlock(rwlock, tsp));
+	return (__thr_rwlock_wrlock(rwlock, tsp, CLOCK_REALTIME));
 }
 
 static inline int