git: 1b4779eef93c - main - libthr mutex_self_lock(): handle signals and clock steps

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

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

commit 1b4779eef93c6e3c9af1887b941d7700ef143951
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-10 03:07:01 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-10 19:53:27 +0000

    libthr mutex_self_lock(): handle signals and clock steps
    
    Relocking an owned NORMAL mutex with an absolute timeout has two issues:
    - nanosleep(2) is never restarted, so any caught signal causes
      an early ETIMEDOUT.
    - If CLOCK_REALTIME or CLOCK_TAI are stepped backward during the sleep,
      the call also returns ETIMEDOUT before the clock reaches the deadline.
    
    The kernel already polls CPU-time clocks and handles realtime steps.
    Use _thr_umtx_timedwait_uint() in a loop to handle both issues.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61381
---
 lib/libthr/thread/thr_mutex.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c
index 30ebdd15951f..346396d15acf 100644
--- a/lib/libthr/thread/thr_mutex.c
+++ b/lib/libthr/thread/thr_mutex.c
@@ -905,8 +905,9 @@ static int
 mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime,
     clockid_t clockid)
 {
-	struct timespec	ts1, ts2;
+	struct timespec	ts1;
 	int ret;
+	u_int dummy;
 
 	switch (PMUTEX_TYPE(m->m_flags)) {
 	case PTHREAD_MUTEX_ERRORCHECK:
@@ -932,8 +933,11 @@ mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime,
 			    clock_gettime(clockid, &ts1) == -1) {
 				ret = EINVAL;
 			} else {
-				TIMESPEC_SUB(&ts2, abstime, &ts1);
-				__sys_nanosleep(&ts2, NULL);
+				dummy = 0;
+				do {
+					ret = _thr_umtx_timedwait_uint(&dummy,
+					    0, clockid, abstime, 0);
+				} while (ret == 0 || ret == EINTR);
 				ret = ETIMEDOUT;
 			}
 		} else {