commit a32dd91

Michael Forney  ·  2026-03-03 22:01:37 +0000 UTC
parent c5fca8b
musl: Add patch to implement pthread_{cond,mutex}_timedwait
5 files changed,  +313, -1
+2, -0
 1@@ -1142,6 +1142,7 @@ return {
 2 		'src/thread/pthread_cleanup_push.c',
 3 		'src/thread/pthread_cond_broadcast.c',
 4 		'src/thread/pthread_cond_destroy.c',
 5+		'src/thread/pthread_cond_clockwait.c',
 6 		'src/thread/pthread_cond_init.c',
 7 		'src/thread/pthread_cond_signal.c',
 8 		'src/thread/pthread_cond_timedwait.c',
 9@@ -1162,6 +1163,7 @@ return {
10 		'src/thread/pthread_join.c',
11 		'src/thread/pthread_key_create.c',
12 		'src/thread/pthread_kill.c',
13+		'src/thread/pthread_mutex_clocklock.c',
14 		'src/thread/pthread_mutex_consistent.c',
15 		'src/thread/pthread_mutex_destroy.c',
16 		'src/thread/pthread_mutex_getprioceiling.c',
+56, -0
 1@@ -0,0 +1,56 @@
 2+From 167003894e360f7a868fbd9439e87c6b17902376 Mon Sep 17 00:00:00 2001
 3+From: Yonggang Luo <luoyonggang@gmail.com>
 4+Date: Tue, 20 Jun 2023 22:36:59 +0800
 5+Subject: [PATCH] trim spaces of pthread_cond_timedwait.c and
 6+ pthread_mutex_timedlock.c
 7+
 8+Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
 9+---
10+ src/thread/pthread_cond_timedwait.c  | 6 +++---
11+ src/thread/pthread_mutex_timedlock.c | 2 +-
12+ 2 files changed, 4 insertions(+), 4 deletions(-)
13+
14+diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c
15+index 6b761455..c5b35a6c 100644
16+--- a/src/thread/pthread_cond_timedwait.c
17++++ b/src/thread/pthread_cond_timedwait.c
18+@@ -121,12 +121,12 @@ int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restri
19+ 		 * via the futex notify below. */
20+ 
21+ 		lock(&c->_c_lock);
22+-		
23++
24+ 		if (c->_c_head == &node) c->_c_head = node.next;
25+ 		else if (node.prev) node.prev->next = node.next;
26+ 		if (c->_c_tail == &node) c->_c_tail = node.prev;
27+ 		else if (node.next) node.next->prev = node.prev;
28+-		
29++
30+ 		unlock(&c->_c_lock);
31+ 
32+ 		if (node.notify) {
33+@@ -156,7 +156,7 @@ relock:
34+ 		if (val>0) a_cas(&m->_m_lock, val, val|0x80000000);
35+ 		unlock_requeue(&node.prev->barrier, &m->_m_lock, m->_m_type & (8|128));
36+ 	} else if (!(m->_m_type & 8)) {
37+-		a_dec(&m->_m_waiters);		
38++		a_dec(&m->_m_waiters);
39+ 	}
40+ 
41+ 	/* Since a signal was consumed, cancellation is not permitted. */
42+diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_timedlock.c
43+index 9279fc54..87f89287 100644
44+--- a/src/thread/pthread_mutex_timedlock.c
45++++ b/src/thread/pthread_mutex_timedlock.c
46+@@ -66,7 +66,7 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
47+ 	if (r != EBUSY) return r;
48+ 
49+ 	if (type&8) return pthread_mutex_timedlock_pi(m, at);
50+-	
51++
52+ 	int spins = 100;
53+ 	while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
54+ 
55+-- 
56+2.49.0
57+
+25, -0
 1@@ -0,0 +1,25 @@
 2+From 2bcfd41fe21f396f528fb863afeaf1328341a681 Mon Sep 17 00:00:00 2001
 3+From: Yonggang Luo <luoyonggang@gmail.com>
 4+Date: Tue, 20 Jun 2023 22:37:00 +0800
 5+Subject: [PATCH] Rename files for implement pthread_mutex_clocklock and
 6+ pthread_cond_clockwait
 7+
 8+Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
 9+---
10+ src/thread/{pthread_cond_timedwait.c => pthread_cond_clockwait.c} | 0
11+ .../{pthread_mutex_timedlock.c => pthread_mutex_clocklock.c}      | 0
12+ 2 files changed, 0 insertions(+), 0 deletions(-)
13+ rename src/thread/{pthread_cond_timedwait.c => pthread_cond_clockwait.c} (100%)
14+ rename src/thread/{pthread_mutex_timedlock.c => pthread_mutex_clocklock.c} (100%)
15+
16+diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_clockwait.c
17+similarity index 100%
18+rename from src/thread/pthread_cond_timedwait.c
19+rename to src/thread/pthread_cond_clockwait.c
20+diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_clocklock.c
21+similarity index 100%
22+rename from src/thread/pthread_mutex_timedlock.c
23+rename to src/thread/pthread_mutex_clocklock.c
24+-- 
25+2.49.0
26+
+229, -0
  1@@ -0,0 +1,229 @@
  2+From b5f9694c693d4f3fad53191a9e5d3983255feef6 Mon Sep 17 00:00:00 2001
  3+From: Yonggang Luo <luoyonggang@gmail.com>
  4+Date: Tue, 20 Jun 2023 22:37:01 +0800
  5+Subject: [PATCH] add pthread_mutex_clocklock and pthread_cond_clockdwait
  6+
  7+These two functions are already implemented in glibc, android bionic libc, qnx libc
  8+
  9+Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
 10+---
 11+ compat/time32/pthread_cond_clockwait_time32.c  |  9 +++++++++
 12+ compat/time32/pthread_mutex_clocklock_time32.c |  9 +++++++++
 13+ compat/time32/time32.h                         |  2 ++
 14+ include/pthread.h                              |  4 ++++
 15+ src/include/pthread.h                          |  2 ++
 16+ src/thread/pthread_cond_clockwait.c            |  8 ++++----
 17+ src/thread/pthread_cond_timedwait.c            |  9 +++++++++
 18+ src/thread/pthread_mutex_clocklock.c           | 12 ++++++------
 19+ src/thread/pthread_mutex_timedlock.c           |  8 ++++++++
 20+ 9 files changed, 53 insertions(+), 10 deletions(-)
 21+ create mode 100644 compat/time32/pthread_cond_clockwait_time32.c
 22+ create mode 100644 compat/time32/pthread_mutex_clocklock_time32.c
 23+ create mode 100644 src/thread/pthread_cond_timedwait.c
 24+ create mode 100644 src/thread/pthread_mutex_timedlock.c
 25+
 26+diff --git a/compat/time32/pthread_cond_clockwait_time32.c b/compat/time32/pthread_cond_clockwait_time32.c
 27+new file mode 100644
 28+index 00000000..e6864f08
 29+--- /dev/null
 30++++ b/compat/time32/pthread_cond_clockwait_time32.c
 31+@@ -0,0 +1,9 @@
 32++#include "time32.h"
 33++#include <time.h>
 34++#include <pthread.h>
 35++
 36++int __pthread_cond_clockwait_time32(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, clockid_t clock, const struct timespec32 *restrict ts32)
 37++{
 38++	return pthread_cond_clockwait(c, m, clock, !ts32 ? 0 : (&(struct timespec){
 39++		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}));
 40++}
 41+diff --git a/compat/time32/pthread_mutex_clocklock_time32.c b/compat/time32/pthread_mutex_clocklock_time32.c
 42+new file mode 100644
 43+index 00000000..d54e85a7
 44+--- /dev/null
 45++++ b/compat/time32/pthread_mutex_clocklock_time32.c
 46+@@ -0,0 +1,9 @@
 47++#include "time32.h"
 48++#include <time.h>
 49++#include <pthread.h>
 50++
 51++int __pthread_mutex_clocklock_time32(pthread_mutex_t *restrict m, clockid_t clk, const struct timespec32 *restrict ts32)
 52++{
 53++	return pthread_mutex_clocklock(m, clk, !ts32 ? 0 : (&(struct timespec){
 54++		.tv_sec = ts32->tv_sec, .tv_nsec = ts32->tv_nsec}));
 55++}
 56+diff --git a/compat/time32/time32.h b/compat/time32/time32.h
 57+index fdec17c3..1267f255 100644
 58+--- a/compat/time32/time32.h
 59++++ b/compat/time32/time32.h
 60+@@ -59,7 +59,9 @@ int __mtx_timedlock_time32() __asm__("mtx_timedlock");
 61+ int __nanosleep_time32() __asm__("nanosleep");
 62+ int __ppoll_time32() __asm__("ppoll");
 63+ int __pselect_time32() __asm__("pselect");
 64++int __pthread_cond_clockwait_time32() __asm__("pthread_cond_clockwait");
 65+ int __pthread_cond_timedwait_time32() __asm__("pthread_cond_timedwait");
 66++int __pthread_mutex_clocklock_time32() __asm__("pthread_mutex_clocklock");
 67+ int __pthread_mutex_timedlock_time32() __asm__("pthread_mutex_timedlock");
 68+ int __pthread_rwlock_timedrdlock_time32() __asm__("pthread_rwlock_timedrdlock");
 69+ int __pthread_rwlock_timedwrlock_time32() __asm__("pthread_rwlock_timedwrlock");
 70+diff --git a/include/pthread.h b/include/pthread.h
 71+index 89fd9ff7..7fb2d162 100644
 72+--- a/include/pthread.h
 73++++ b/include/pthread.h
 74+@@ -107,6 +107,7 @@ int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *_
 75+ int pthread_mutex_lock(pthread_mutex_t *);
 76+ int pthread_mutex_unlock(pthread_mutex_t *);
 77+ int pthread_mutex_trylock(pthread_mutex_t *);
 78++int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
 79+ int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict);
 80+ int pthread_mutex_destroy(pthread_mutex_t *);
 81+ int pthread_mutex_consistent(pthread_mutex_t *);
 82+@@ -117,6 +118,7 @@ int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restri
 83+ int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict);
 84+ int pthread_cond_destroy(pthread_cond_t *);
 85+ int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict);
 86++int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
 87+ int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict);
 88+ int pthread_cond_broadcast(pthread_cond_t *);
 89+ int pthread_cond_signal(pthread_cond_t *);
 90+@@ -229,7 +231,9 @@ int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
 91+ #endif
 92+ 
 93+ #if _REDIR_TIME64
 94++__REDIR(pthread_mutex_clocklock, __pthread_mutex_clocklock_time64);
 95+ __REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64);
 96++__REDIR(pthread_cond_clockwait, __pthread_cond_clockwait_time64);
 97+ __REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64);
 98+ __REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64);
 99+ __REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64);
100+diff --git a/src/include/pthread.h b/src/include/pthread.h
101+index 7167d3e1..d7fe7974 100644
102+--- a/src/include/pthread.h
103++++ b/src/include/pthread.h
104+@@ -12,9 +12,11 @@ hidden int __pthread_join(pthread_t, void **);
105+ hidden int __pthread_mutex_lock(pthread_mutex_t *);
106+ hidden int __pthread_mutex_trylock(pthread_mutex_t *);
107+ hidden int __pthread_mutex_trylock_owner(pthread_mutex_t *);
108++hidden int __pthread_mutex_clocklock(pthread_mutex_t *restrict, clockid_t, const struct timespec *restrict);
109+ hidden int __pthread_mutex_timedlock(pthread_mutex_t *restrict, const struct timespec *restrict);
110+ hidden int __pthread_mutex_unlock(pthread_mutex_t *);
111+ hidden int __private_cond_signal(pthread_cond_t *, int);
112++hidden int __pthread_cond_clockwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, clockid_t, const struct timespec *restrict);
113+ hidden int __pthread_cond_timedwait(pthread_cond_t *restrict, pthread_mutex_t *restrict, const struct timespec *restrict);
114+ hidden int __pthread_key_create(pthread_key_t *, void (*)(void *));
115+ hidden int __pthread_key_delete(pthread_key_t);
116+diff --git a/src/thread/pthread_cond_clockwait.c b/src/thread/pthread_cond_clockwait.c
117+index c5b35a6c..4d178c27 100644
118+--- a/src/thread/pthread_cond_clockwait.c
119++++ b/src/thread/pthread_cond_clockwait.c
120+@@ -59,10 +59,10 @@ enum {
121+ 	LEAVING,
122+ };
123+ 
124+-int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
125++int __pthread_cond_clockwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, clockid_t clk, const struct timespec *restrict ts)
126+ {
127+ 	struct waiter node = { 0 };
128+-	int e, seq, clock = c->_c_clock, cs, shared=0, oldstate, tmp;
129++	int e, seq, cs, shared=0, oldstate, tmp;
130+ 	volatile int *fut;
131+ 
132+ 	if ((m->_m_type&15) && (m->_m_lock&INT_MAX) != __pthread_self()->tid)
133+@@ -97,7 +97,7 @@ int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restri
134+ 	__pthread_setcancelstate(PTHREAD_CANCEL_MASKED, &cs);
135+ 	if (cs == PTHREAD_CANCEL_DISABLE) __pthread_setcancelstate(cs, 0);
136+ 
137+-	do e = __timedwait_cp(fut, seq, clock, ts, !shared);
138++	do e = __timedwait_cp(fut, seq, clk, ts, !shared);
139+ 	while (*fut==seq && (!e || e==EINTR));
140+ 	if (e == EINTR) e = 0;
141+ 
142+@@ -210,4 +210,4 @@ int __private_cond_signal(pthread_cond_t *c, int n)
143+ 	return 0;
144+ }
145+ 
146+-weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
147++weak_alias(__pthread_cond_clockwait, pthread_cond_clockwait);
148+diff --git a/src/thread/pthread_cond_timedwait.c b/src/thread/pthread_cond_timedwait.c
149+new file mode 100644
150+index 00000000..da803df7
151+--- /dev/null
152++++ b/src/thread/pthread_cond_timedwait.c
153+@@ -0,0 +1,9 @@
154++#include "pthread_impl.h"
155++
156++int __pthread_cond_timedwait(pthread_cond_t *restrict c, pthread_mutex_t *restrict m, const struct timespec *restrict ts)
157++{
158++	int clock = c->_c_clock;
159++	return __pthread_cond_clockwait(c, m, clock, ts);
160++}
161++
162++weak_alias(__pthread_cond_timedwait, pthread_cond_timedwait);
163+diff --git a/src/thread/pthread_mutex_clocklock.c b/src/thread/pthread_mutex_clocklock.c
164+index 87f89287..4a0a945f 100644
165+--- a/src/thread/pthread_mutex_clocklock.c
166++++ b/src/thread/pthread_mutex_clocklock.c
167+@@ -18,7 +18,7 @@ static int __futex4(volatile void *addr, int op, int val, const struct timespec
168+ 	return __syscall(SYS_futex, addr, op, val, to);
169+ }
170+ 
171+-static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct timespec *restrict at)
172++static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, clockid_t clk, const struct timespec *restrict at)
173+ {
174+ 	int type = m->_m_type;
175+ 	int priv = (type & 128) ^ 128;
176+@@ -48,12 +48,12 @@ static int pthread_mutex_timedlock_pi(pthread_mutex_t *restrict m, const struct
177+ 	case EDEADLK:
178+ 		if ((type&3) == PTHREAD_MUTEX_ERRORCHECK) return e;
179+ 	}
180+-	do e = __timedwait(&(int){0}, 0, CLOCK_REALTIME, at, 1);
181++	do e = __timedwait(&(int){0}, 0, clk, at, 1);
182+ 	while (e != ETIMEDOUT);
183+ 	return e;
184+ }
185+ 
186+-int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
187++int __pthread_mutex_clocklock(pthread_mutex_t *restrict m, clockid_t clk, const struct timespec *restrict at)
188+ {
189+ 	if ((m->_m_type&15) == PTHREAD_MUTEX_NORMAL
190+ 	    && !a_cas(&m->_m_lock, 0, EBUSY))
191+@@ -65,7 +65,7 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
192+ 	r = __pthread_mutex_trylock(m);
193+ 	if (r != EBUSY) return r;
194+ 
195+-	if (type&8) return pthread_mutex_timedlock_pi(m, at);
196++	if (type&8) return pthread_mutex_timedlock_pi(m, clk, at);
197+ 
198+ 	int spins = 100;
199+ 	while (spins-- && m->_m_lock && !m->_m_waiters) a_spin();
200+@@ -82,11 +82,11 @@ int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec
201+ 		a_inc(&m->_m_waiters);
202+ 		t = r | 0x80000000;
203+ 		a_cas(&m->_m_lock, r, t);
204+-		r = __timedwait(&m->_m_lock, t, CLOCK_REALTIME, at, priv);
205++		r = __timedwait(&m->_m_lock, t, clk, at, priv);
206+ 		a_dec(&m->_m_waiters);
207+ 		if (r && r != EINTR) break;
208+ 	}
209+ 	return r;
210+ }
211+ 
212+-weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
213++weak_alias(__pthread_mutex_clocklock, pthread_mutex_clocklock);
214+diff --git a/src/thread/pthread_mutex_timedlock.c b/src/thread/pthread_mutex_timedlock.c
215+new file mode 100644
216+index 00000000..14a87065
217+--- /dev/null
218++++ b/src/thread/pthread_mutex_timedlock.c
219+@@ -0,0 +1,8 @@
220++#include "pthread_impl.h"
221++
222++int __pthread_mutex_timedlock(pthread_mutex_t *restrict m, const struct timespec *restrict at)
223++{
224++	return __pthread_mutex_clocklock(m, CLOCK_REALTIME, at);
225++}
226++
227++weak_alias(__pthread_mutex_timedlock, pthread_mutex_timedlock);
228+-- 
229+2.49.0
230+
+1, -1
1@@ -1 +1 @@
2-1.2.5 r1
3+1.2.5 r2