From nobody Tue Nov 10 00:22:10 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jNnQp4CzWz70lLb for ; Tue, 10 Nov 2026 00:22:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jNnQp1xJwz3QyR for ; Tue, 10 Nov 2026 00:22:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794270130; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8mPc0aGf8UstA5yQV88JiHvNcn+Z1MmuGAZTj0J0JVg=; b=SCBlSVOVQfckeg/Ha+w3Ys8YLt4VMtLpDnfTWqGny4ya9Fvpv986do6sHH+4AECmuBa74a +ej11162+ZnO8tGgfgWuWqA84rNymwvDdw6z1UtwjgoJB/thC3p8IJXNZ82Q0JTq5Rl4pM JgH+kOLeCAzBJlSUt3gACXgkqBcAr2QB5fO0TPkVLinkWWmrbP3NIX1l6JXG+YUOnf2QOG 3NaHvv5X+T85DzsyzaH7xLAdxESQYfEKuUY+djoJm5wvABTsXUUfbKdBHeJnlxP5rGTehZ w3Am73vzUaccN+cOS0ImLO1fNds9Zrbfr6KV6JHfTJkwbVOBkvjSQ2bmh2dBuw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794270130; b=GFZ2Ei0jA5tNdmLbwHa9EvfqROi+xI1zzZP9a6CQh4SjHBN5U/WvFVYGNhudXxFBL6CZhl 2YQX9G++Ouhy0O280k36zxfWqmG2Ck8pHgq0ZMteAx9z2FyK7Df7dLlx5HSPavX2ca/0om ATXtnVjsyzgG7cp9YNu7b+twe3R2UeKIbFYshdrXwozEQdf4+M/ytU/uYxM4Yjh7w9JTui XJAjL3KPAPIk4UwyESOO2TwnG5/UeywimGcbjUYLJVEZqf4mzAams5cxDEipUugBwUPwzY HZpe4PQAm0vrL44zRPfwlX4Y5UogoGs1aJj5lPhvjYx+BvY5BbEGnGRtnQp4SA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794270130; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8mPc0aGf8UstA5yQV88JiHvNcn+Z1MmuGAZTj0J0JVg=; b=sVM1k3CXMHyxGA67I48NmJ+UTgcV2k2aW0kJLVRXYuIknRG7fGRrqJ5ANF2dYwfaQrkLuN BjISzWW2K+oMDAi6k9qJcrTI0fRrZrDe9CQsu7fnPaQT9dw/f4yNGJwf8EvhwILvy5+37O Aq7mwLuCyR5Z0QdewO+Nrkdbd5YzbL9nnZVM0+sKP0BVH77MWO3Unusn7v/ithUyXEZxS0 /vQmE+AtIrsBVOdTDXCyrvrGRdpZX3RRswG6eSICS0EJOHeD4f5FiwSIq3vXLHR7sFnuSs Mm8h017I7ryc3YbjL7lxSyVEJbebH6sMAh3EuZYmvFVBZAIFHQ5927uxghpSVw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jNnQp1CBXz13sx for ; Tue, 10 Nov 2026 00:22:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id f379 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 10 Nov 2026 00:22:10 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: b7c2db4c772b - main - libthr: add pthread_mutex_clocklock(3) List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: Commit messages for the main branch of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b7c2db4c772ba3c61f4760eca011332c9c12da1c Auto-Submitted: auto-generated Date: Tue, 10 Nov 2026 00:22:10 +0000 Message-Id: <6af263b2.f379.266b9e47@gitrepo.freebsd.org> The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=b7c2db4c772ba3c61f4760eca011332c9c12da1c commit b7c2db4c772ba3c61f4760eca011332c9c12da1c Author: Konstantin Belousov AuthorDate: 2026-11-05 17:23:49 +0000 Commit: Konstantin Belousov 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