-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
4483f2500825a84382c2a6a9ac60fc77954533d7.patch
297 lines (293 loc) · 9.33 KB
/
4483f2500825a84382c2a6a9ac60fc77954533d7.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
diff -up glibc-2.40/nptl/pthread_mutex_lock_any.c.36~ glibc-2.40/nptl/pthread_mutex_lock_any.c
--- glibc-2.40/nptl/pthread_mutex_lock_any.c.36~ 2024-07-22 15:26:23.220864358 +0200
+++ glibc-2.40/nptl/pthread_mutex_lock_any.c 2024-07-22 15:26:23.220864358 +0200
@@ -0,0 +1,37 @@
+/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/param.h>
+#include <not-cancel.h>
+#include "pthreadP.h"
+#include <atomic.h>
+#include <lowlevellock.h>
+#include <stap-probe.h>
+
+int
+__pthread_mutex_lock_any (pthread_mutex_t *mutexlist, int mutexcount,
+ int *outlocked)
+{
+ return __pthread_mutex_timedlock_any(mutexlist, mutexcount, NULL, outlocked);
+}
+
+weak_alias (__pthread_mutex_lock_any, pthread_mutex_lock_any)
diff -up glibc-2.40/nptl/pthread_mutex_timedlock_any.c.36~ glibc-2.40/nptl/pthread_mutex_timedlock_any.c
--- glibc-2.40/nptl/pthread_mutex_timedlock_any.c.36~ 2024-07-22 15:26:23.220864358 +0200
+++ glibc-2.40/nptl/pthread_mutex_timedlock_any.c 2024-07-22 15:26:23.220864358 +0200
@@ -0,0 +1,193 @@
+/* Copyright (C) 2002-2019 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/param.h>
+#include <not-cancel.h>
+#include "pthreadP.h"
+#include <atomic.h>
+#include <lowlevellock.h>
+#include <stap-probe.h>
+
+/* TODO: this probably comes from a kernel header when upstream? */
+struct futex_wait_block {
+ int *uaddr;
+ int val;
+ int bitset;
+} __attribute__((packed));
+
+int
+__pthread_mutex_timedlock_any (pthread_mutex_t *mutexlist, int mutexcount,
+ const struct timespec *abstime, int *outlocked)
+{
+ /* This requires futex support */
+#ifndef __NR_futex
+ return ENOTSUP;
+#endif
+
+ if (mutexlist == NULL)
+ {
+ /* User is asking us if kernel supports the feature. */
+
+ /* TODO: how does one check if supported?
+ * I was thinking of trying the ioctl once and then returning the static
+ * cached value, is that OK?
+ */
+ return 0;
+ }
+
+ if (mutexlist != NULL && mutexcount <= 0)
+ return EINVAL;
+
+ if (outlocked == NULL)
+ return EINVAL;
+
+ int type = PTHREAD_MUTEX_TYPE (mutexlist);
+
+ for (int i = 1; i < mutexcount; i++)
+ {
+ /* Types have to match, since the PRIVATE flag is OP-global. */
+ if (PTHREAD_MUTEX_TYPE (&mutexlist[i]) != type)
+ return EINVAL;
+ }
+
+ int kind = type & PTHREAD_MUTEX_KIND_MASK_NP;
+
+ /* TODO: implement recursive, errorcheck and adaptive. */
+ if (kind != PTHREAD_MUTEX_NORMAL)
+ return EINVAL;
+
+ /* TODO: implement robust. */
+ if (type & PTHREAD_MUTEX_ROBUST_NORMAL_NP)
+ return EINVAL;
+
+ /* TODO: implement PI. */
+ if (type & PTHREAD_MUTEX_PRIO_INHERIT_NP)
+ return EINVAL;
+
+ /* TODO: implement PP. */
+ if (type & PTHREAD_MUTEX_PRIO_PROTECT_NP)
+ return EINVAL;
+
+ pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
+ int result;
+
+ result = -1;
+
+ for (int i = 0; i < mutexcount; i++)
+ {
+ if (__lll_trylock (&mutexlist[i].__data.__lock) == 0)
+ {
+ result = i;
+ break;
+ }
+ }
+
+ while (result == -1)
+ {
+ for (int i = 0; i < mutexcount; i++)
+ {
+ int oldval = atomic_exchange_acq (&mutexlist[i].__data.__lock, 2);
+
+ if (oldval == 0)
+ {
+ result = i;
+ break;
+ }
+ }
+
+ if (result == -1)
+ {
+ /* Couldn't get one of the locks immediately, we have to sleep now. */
+ struct timespec *timeout = NULL;
+ struct timespec rt;
+
+ if (abstime != NULL)
+ {
+ /* Reject invalid timeouts. */
+ if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
+ return EINVAL;
+
+ struct timeval tv;
+
+ /* Get the current time. */
+ (void) __gettimeofday (&tv, NULL);
+
+ /* Compute relative timeout. */
+ rt.tv_sec = abstime->tv_sec - tv.tv_sec;
+ rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
+ if (rt.tv_nsec < 0)
+ {
+ rt.tv_nsec += 1000000000;
+ --rt.tv_sec;
+ }
+
+ if (rt.tv_sec < 0)
+ return ETIMEDOUT;
+
+ timeout = &rt;
+ }
+
+ struct futex_wait_block waitblock[mutexcount];
+
+ for (int i = 0; i < mutexcount; i++)
+ {
+ waitblock[i].uaddr = &mutexlist[i].__data.__lock;
+ waitblock[i].val = 2;
+ waitblock[i].bitset = ~0;
+ }
+
+ long int __ret;
+
+ /* Safe to use the flag for the first one, since all their types match. */
+ int private_flag = PTHREAD_MUTEX_PSHARED (&mutexlist[0]);
+
+ __ret = lll_futex_timed_wait_multiple (waitblock, mutexcount, timeout,
+ private_flag);
+
+ if (__ret < 0)
+ return -__ret; /* TODO is this correct? */
+
+ /* Have slept, try grabbing the one that woke us up? */
+ if (atomic_exchange_acq (&mutexlist[__ret].__data.__lock, 2) == 0)
+ {
+ /* We got it, done, loop will end below. */
+ result = __ret;
+ }
+ }
+ }
+
+ if (result != -1)
+ {
+ /* Record the ownership. */
+ mutexlist[result].__data.__owner = id;
+ ++mutexlist[result].__data.__nusers;
+
+ /* Let the user know which mutex is now locked. */
+ *outlocked = result;
+
+ result = 0;
+ }
+
+ return result;
+}
+
+weak_alias (__pthread_mutex_timedlock_any, pthread_mutex_timedlock_any)
diff -up glibc-2.40/sysdeps/nptl/lowlevellock-futex.h.36~ glibc-2.40/sysdeps/nptl/lowlevellock-futex.h
--- glibc-2.40/sysdeps/nptl/lowlevellock-futex.h.36~ 2024-07-21 18:33:37.000000000 +0200
+++ glibc-2.40/sysdeps/nptl/lowlevellock-futex.h 2024-07-22 15:26:23.221864434 +0200
@@ -39,6 +39,7 @@
#define FUTEX_WAIT_REQUEUE_PI 11
#define FUTEX_CMP_REQUEUE_PI 12
#define FUTEX_LOCK_PI2 13
+#define FUTEX_WAIT_MULTIPLE 14
#define FUTEX_PRIVATE_FLAG 128
#define FUTEX_CLOCK_REALTIME 256
@@ -62,6 +63,14 @@
? -INTERNAL_SYSCALL_ERRNO (__ret) : 0); \
})
+# define lll_futex_syscall_ret(nargs, futexp, op, ...) \
+ ({ \
+ long int __ret = INTERNAL_SYSCALL (futex, nargs, futexp, op, \
+ __VA_ARGS__); \
+ (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret)) \
+ ? -INTERNAL_SYSCALL_ERRNO (__ret) : __ret); \
+ })
+
/* For most of these macros, the return value is never really used.
Nevertheless, the protocol is that each one returns a negated errno
code for failure or zero for success. (Note that the corresponding
@@ -78,6 +87,11 @@
__lll_private_flag (FUTEX_WAIT, private), \
val, timeout)
+# define lll_futex_timed_wait_multiple(futexp, val, timeout, private) \
+ lll_futex_syscall_ret (4, futexp, \
+ __lll_private_flag (FUTEX_WAIT_MULTIPLE, private), \
+ val, timeout)
+
/* Verify whether the supplied clockid is supported by
lll_futex_clock_wait_bitset. */
# define lll_futex_supported_clockid(clockid) \
diff -up glibc-2.40/sysdeps/nptl/pthread.h.36~ glibc-2.40/sysdeps/nptl/pthread.h
--- glibc-2.40/sysdeps/nptl/pthread.h.36~ 2024-07-22 15:26:23.221864434 +0200
+++ glibc-2.40/sysdeps/nptl/pthread.h 2024-07-22 15:27:31.185133836 +0200
@@ -794,7 +794,17 @@ extern int pthread_mutex_trylock (pthrea
extern int pthread_mutex_lock (pthread_mutex_t *__mutex)
__THROWNL __nonnull ((1));
+/* Lock any one of several mutexes. */
+extern int pthread_mutex_lock_any (pthread_mutex_t *__mutexlist,
+ int mutexcount, int *outlocked);
+
#ifdef __USE_XOPEN2K
+/* Lock any one of several mutexes, with timeout. */
+extern int pthread_mutex_timedlock_any (pthread_mutex_t *__mutexlist,
+ int mutexcount,
+ const struct timespec *__restrict
+ __abstime, int *outlocked);
+
/* Wait until lock becomes available, or specified time passes. */
# ifndef __USE_TIME64_REDIRECTS
extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,