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