pthread_mutex.c revision 1.77 1 1.77 ad /* $NetBSD: pthread_mutex.c,v 1.77 2020/05/16 22:53:37 ad Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.77 ad * Copyright (c) 2001, 2003, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.27 ad * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej *
19 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.2 thorpej */
31 1.2 thorpej
32 1.49 ad /*
33 1.49 ad * To track threads waiting for mutexes to be released, we use lockless
34 1.49 ad * lists built on atomic operations and memory barriers.
35 1.49 ad *
36 1.49 ad * A simple spinlock would be faster and make the code easier to
37 1.49 ad * follow, but spinlocks are problematic in userspace. If a thread is
38 1.49 ad * preempted by the kernel while holding a spinlock, any other thread
39 1.49 ad * attempting to acquire that spinlock will needlessly busy wait.
40 1.49 ad *
41 1.49 ad * There is no good way to know that the holding thread is no longer
42 1.49 ad * running, nor to request a wake-up once it has begun running again.
43 1.49 ad * Of more concern, threads in the SCHED_FIFO class do not have a
44 1.49 ad * limited time quantum and so could spin forever, preventing the
45 1.49 ad * thread holding the spinlock from getting CPU time: it would never
46 1.49 ad * be released.
47 1.49 ad */
48 1.49 ad
49 1.2 thorpej #include <sys/cdefs.h>
50 1.77 ad __RCSID("$NetBSD: pthread_mutex.c,v 1.77 2020/05/16 22:53:37 ad Exp $");
51 1.40 ad
52 1.40 ad #include <sys/types.h>
53 1.44 ad #include <sys/lwpctl.h>
54 1.60 christos #include <sys/sched.h>
55 1.51 matt #include <sys/lock.h>
56 1.10 lukem
57 1.2 thorpej #include <errno.h>
58 1.2 thorpej #include <limits.h>
59 1.2 thorpej #include <stdlib.h>
60 1.56 christos #include <time.h>
61 1.6 scw #include <string.h>
62 1.44 ad #include <stdio.h>
63 1.2 thorpej
64 1.2 thorpej #include "pthread.h"
65 1.2 thorpej #include "pthread_int.h"
66 1.56 christos #include "reentrant.h"
67 1.2 thorpej
68 1.44 ad #define MUTEX_WAITERS_BIT ((uintptr_t)0x01)
69 1.44 ad #define MUTEX_RECURSIVE_BIT ((uintptr_t)0x02)
70 1.60 christos #define MUTEX_PROTECT_BIT ((uintptr_t)0x08)
71 1.60 christos #define MUTEX_THREAD ((uintptr_t)~0x0f)
72 1.44 ad
73 1.44 ad #define MUTEX_HAS_WAITERS(x) ((uintptr_t)(x) & MUTEX_WAITERS_BIT)
74 1.44 ad #define MUTEX_RECURSIVE(x) ((uintptr_t)(x) & MUTEX_RECURSIVE_BIT)
75 1.60 christos #define MUTEX_PROTECT(x) ((uintptr_t)(x) & MUTEX_PROTECT_BIT)
76 1.44 ad #define MUTEX_OWNER(x) ((uintptr_t)(x) & MUTEX_THREAD)
77 1.44 ad
78 1.60 christos #define MUTEX_GET_TYPE(x) \
79 1.60 christos ((int)(((uintptr_t)(x) & 0x000000ff) >> 0))
80 1.60 christos #define MUTEX_SET_TYPE(x, t) \
81 1.60 christos (x) = (void *)(((uintptr_t)(x) & ~0x000000ff) | ((t) << 0))
82 1.60 christos #define MUTEX_GET_PROTOCOL(x) \
83 1.60 christos ((int)(((uintptr_t)(x) & 0x0000ff00) >> 8))
84 1.60 christos #define MUTEX_SET_PROTOCOL(x, p) \
85 1.60 christos (x) = (void *)(((uintptr_t)(x) & ~0x0000ff00) | ((p) << 8))
86 1.60 christos #define MUTEX_GET_CEILING(x) \
87 1.60 christos ((int)(((uintptr_t)(x) & 0x00ff0000) >> 16))
88 1.60 christos #define MUTEX_SET_CEILING(x, c) \
89 1.60 christos (x) = (void *)(((uintptr_t)(x) & ~0x00ff0000) | ((c) << 16))
90 1.60 christos
91 1.44 ad #if __GNUC_PREREQ__(3, 0)
92 1.44 ad #define NOINLINE __attribute ((noinline))
93 1.44 ad #else
94 1.44 ad #define NOINLINE /* nothing */
95 1.44 ad #endif
96 1.44 ad
97 1.44 ad static void pthread__mutex_wakeup(pthread_t, pthread_mutex_t *);
98 1.60 christos static int pthread__mutex_lock_slow(pthread_mutex_t *,
99 1.60 christos const struct timespec *);
100 1.44 ad static void pthread__mutex_pause(void);
101 1.2 thorpej
102 1.39 ad int _pthread_mutex_held_np(pthread_mutex_t *);
103 1.39 ad pthread_t _pthread_mutex_owner_np(pthread_mutex_t *);
104 1.39 ad
105 1.39 ad __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np)
106 1.39 ad __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np)
107 1.39 ad
108 1.2 thorpej __strong_alias(__libc_mutex_init,pthread_mutex_init)
109 1.2 thorpej __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
110 1.2 thorpej __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
111 1.2 thorpej __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
112 1.2 thorpej __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
113 1.4 thorpej
114 1.4 thorpej __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
115 1.4 thorpej __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
116 1.5 thorpej __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
117 1.2 thorpej
118 1.2 thorpej int
119 1.44 ad pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr)
120 1.2 thorpej {
121 1.60 christos uintptr_t type, proto, val, ceil;
122 1.2 thorpej
123 1.76 kamil #if 0
124 1.65 christos /*
125 1.65 christos * Always initialize the mutex structure, maybe be used later
126 1.65 christos * and the cost should be minimal.
127 1.65 christos */
128 1.56 christos if (__predict_false(__uselibcstub))
129 1.56 christos return __libc_mutex_init_stub(ptm, attr);
130 1.76 kamil #endif
131 1.56 christos
132 1.72 kamil pthread__error(EINVAL, "Invalid mutes attribute",
133 1.72 kamil attr == NULL || attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
134 1.72 kamil
135 1.60 christos if (attr == NULL) {
136 1.44 ad type = PTHREAD_MUTEX_NORMAL;
137 1.60 christos proto = PTHREAD_PRIO_NONE;
138 1.60 christos ceil = 0;
139 1.60 christos } else {
140 1.60 christos val = (uintptr_t)attr->ptma_private;
141 1.2 thorpej
142 1.60 christos type = MUTEX_GET_TYPE(val);
143 1.60 christos proto = MUTEX_GET_PROTOCOL(val);
144 1.60 christos ceil = MUTEX_GET_CEILING(val);
145 1.60 christos }
146 1.44 ad switch (type) {
147 1.44 ad case PTHREAD_MUTEX_ERRORCHECK:
148 1.51 matt __cpu_simple_lock_set(&ptm->ptm_errorcheck);
149 1.44 ad ptm->ptm_owner = NULL;
150 1.44 ad break;
151 1.44 ad case PTHREAD_MUTEX_RECURSIVE:
152 1.51 matt __cpu_simple_lock_clear(&ptm->ptm_errorcheck);
153 1.44 ad ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT;
154 1.44 ad break;
155 1.44 ad default:
156 1.51 matt __cpu_simple_lock_clear(&ptm->ptm_errorcheck);
157 1.44 ad ptm->ptm_owner = NULL;
158 1.44 ad break;
159 1.2 thorpej }
160 1.60 christos switch (proto) {
161 1.60 christos case PTHREAD_PRIO_PROTECT:
162 1.60 christos val = (uintptr_t)ptm->ptm_owner;
163 1.60 christos val |= MUTEX_PROTECT_BIT;
164 1.60 christos ptm->ptm_owner = (void *)val;
165 1.60 christos break;
166 1.2 thorpej
167 1.60 christos }
168 1.44 ad ptm->ptm_magic = _PT_MUTEX_MAGIC;
169 1.44 ad ptm->ptm_waiters = NULL;
170 1.45 ad ptm->ptm_recursed = 0;
171 1.60 christos ptm->ptm_ceiling = (unsigned char)ceil;
172 1.2 thorpej
173 1.2 thorpej return 0;
174 1.2 thorpej }
175 1.2 thorpej
176 1.2 thorpej int
177 1.44 ad pthread_mutex_destroy(pthread_mutex_t *ptm)
178 1.2 thorpej {
179 1.2 thorpej
180 1.56 christos if (__predict_false(__uselibcstub))
181 1.56 christos return __libc_mutex_destroy_stub(ptm);
182 1.56 christos
183 1.14 nathanw pthread__error(EINVAL, "Invalid mutex",
184 1.44 ad ptm->ptm_magic == _PT_MUTEX_MAGIC);
185 1.14 nathanw pthread__error(EBUSY, "Destroying locked mutex",
186 1.44 ad MUTEX_OWNER(ptm->ptm_owner) == 0);
187 1.2 thorpej
188 1.44 ad ptm->ptm_magic = _PT_MUTEX_DEAD;
189 1.2 thorpej return 0;
190 1.2 thorpej }
191 1.2 thorpej
192 1.2 thorpej int
193 1.44 ad pthread_mutex_lock(pthread_mutex_t *ptm)
194 1.2 thorpej {
195 1.27 ad pthread_t self;
196 1.44 ad void *val;
197 1.2 thorpej
198 1.56 christos if (__predict_false(__uselibcstub))
199 1.56 christos return __libc_mutex_lock_stub(ptm);
200 1.56 christos
201 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
202 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
203 1.70 kamil
204 1.27 ad self = pthread__self();
205 1.44 ad val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
206 1.44 ad if (__predict_true(val == NULL)) {
207 1.44 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
208 1.44 ad membar_enter();
209 1.44 ad #endif
210 1.44 ad return 0;
211 1.2 thorpej }
212 1.60 christos return pthread__mutex_lock_slow(ptm, NULL);
213 1.60 christos }
214 1.60 christos
215 1.60 christos int
216 1.60 christos pthread_mutex_timedlock(pthread_mutex_t* ptm, const struct timespec *ts)
217 1.60 christos {
218 1.60 christos pthread_t self;
219 1.60 christos void *val;
220 1.60 christos
221 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
222 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
223 1.70 kamil
224 1.60 christos self = pthread__self();
225 1.60 christos val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
226 1.60 christos if (__predict_true(val == NULL)) {
227 1.60 christos #ifndef PTHREAD__ATOMIC_IS_MEMBAR
228 1.60 christos membar_enter();
229 1.60 christos #endif
230 1.60 christos return 0;
231 1.60 christos }
232 1.60 christos return pthread__mutex_lock_slow(ptm, ts);
233 1.44 ad }
234 1.2 thorpej
235 1.44 ad /* We want function call overhead. */
236 1.44 ad NOINLINE static void
237 1.44 ad pthread__mutex_pause(void)
238 1.44 ad {
239 1.2 thorpej
240 1.44 ad pthread__smt_pause();
241 1.2 thorpej }
242 1.2 thorpej
243 1.44 ad /*
244 1.44 ad * Spin while the holder is running. 'lwpctl' gives us the true
245 1.66 ad * status of the thread.
246 1.44 ad */
247 1.44 ad NOINLINE static void *
248 1.44 ad pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner)
249 1.44 ad {
250 1.44 ad pthread_t thread;
251 1.44 ad unsigned int count, i;
252 1.44 ad
253 1.44 ad for (count = 2;; owner = ptm->ptm_owner) {
254 1.44 ad thread = (pthread_t)MUTEX_OWNER(owner);
255 1.44 ad if (thread == NULL)
256 1.44 ad break;
257 1.66 ad if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE)
258 1.44 ad break;
259 1.44 ad if (count < 128)
260 1.44 ad count += count;
261 1.44 ad for (i = count; i != 0; i--)
262 1.44 ad pthread__mutex_pause();
263 1.44 ad }
264 1.2 thorpej
265 1.44 ad return owner;
266 1.44 ad }
267 1.44 ad
268 1.44 ad NOINLINE static int
269 1.60 christos pthread__mutex_lock_slow(pthread_mutex_t *ptm, const struct timespec *ts)
270 1.2 thorpej {
271 1.77 ad void *waiters, *newval, *owner, *next;
272 1.44 ad pthread_t self;
273 1.57 christos int serrno;
274 1.60 christos int error;
275 1.2 thorpej
276 1.44 ad owner = ptm->ptm_owner;
277 1.44 ad self = pthread__self();
278 1.77 ad serrno = errno;
279 1.77 ad
280 1.77 ad pthread__assert(!self->pt_willpark);
281 1.13 nathanw
282 1.44 ad /* Recursive or errorcheck? */
283 1.44 ad if (MUTEX_OWNER(owner) == (uintptr_t)self) {
284 1.44 ad if (MUTEX_RECURSIVE(owner)) {
285 1.45 ad if (ptm->ptm_recursed == INT_MAX)
286 1.44 ad return EAGAIN;
287 1.45 ad ptm->ptm_recursed++;
288 1.44 ad return 0;
289 1.29 ad }
290 1.51 matt if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck))
291 1.44 ad return EDEADLK;
292 1.44 ad }
293 1.29 ad
294 1.60 christos /* priority protect */
295 1.60 christos if (MUTEX_PROTECT(owner) && _sched_protect(ptm->ptm_ceiling) == -1) {
296 1.77 ad error = errno;
297 1.77 ad errno = serrno;
298 1.77 ad return error;
299 1.60 christos }
300 1.44 ad
301 1.77 ad for (;;) {
302 1.44 ad /* If it has become free, try to acquire it again. */
303 1.44 ad if (MUTEX_OWNER(owner) == 0) {
304 1.77 ad newval = (void *)((uintptr_t)self | (uintptr_t)owner);
305 1.77 ad next = atomic_cas_ptr(&ptm->ptm_owner, owner, newval);
306 1.77 ad if (__predict_false(next != owner)) {
307 1.77 ad owner = next;
308 1.77 ad continue;
309 1.77 ad }
310 1.77 ad errno = serrno;
311 1.44 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
312 1.77 ad membar_enter();
313 1.44 ad #endif
314 1.77 ad return 0;
315 1.77 ad } else if (MUTEX_OWNER(owner) != (uintptr_t)self) {
316 1.77 ad /* Spin while the owner is running. */
317 1.77 ad owner = pthread__mutex_spin(ptm, owner);
318 1.77 ad if (MUTEX_OWNER(owner) == 0) {
319 1.77 ad continue;
320 1.77 ad }
321 1.44 ad }
322 1.21 chs
323 1.2 thorpej /*
324 1.44 ad * Nope, still held. Add thread to the list of waiters.
325 1.50 ad * Issue a memory barrier to ensure mutexwait/mutexnext
326 1.44 ad * are visible before we enter the waiters list.
327 1.2 thorpej */
328 1.50 ad self->pt_mutexwait = 1;
329 1.44 ad for (waiters = ptm->ptm_waiters;; waiters = next) {
330 1.50 ad self->pt_mutexnext = waiters;
331 1.77 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
332 1.44 ad membar_producer();
333 1.77 ad #endif
334 1.44 ad next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self);
335 1.44 ad if (next == waiters)
336 1.44 ad break;
337 1.44 ad }
338 1.66 ad
339 1.77 ad /*
340 1.77 ad * Try to set the waiters bit. If the mutex has become free
341 1.77 ad * since entering self onto the waiters list, need to wake
342 1.77 ad * everybody up (including self) and retry. It's possible
343 1.77 ad * to race with the unlocking thread, so self may have
344 1.77 ad * already been awoken.
345 1.77 ad */
346 1.77 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
347 1.66 ad membar_sync();
348 1.77 ad #endif
349 1.77 ad next = atomic_cas_ptr(&ptm->ptm_owner, owner,
350 1.77 ad (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT));
351 1.77 ad if (next != owner) {
352 1.77 ad pthread__mutex_wakeup(self, ptm);
353 1.66 ad }
354 1.21 chs
355 1.29 ad /*
356 1.77 ad * We must not proceed until told that we are no longer
357 1.77 ad * waiting (via pt_mutexwait being set to zero). Otherwise
358 1.77 ad * it is unsafe to re-enter the thread onto the waiters
359 1.77 ad * list.
360 1.29 ad */
361 1.77 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
362 1.66 ad membar_sync();
363 1.77 ad #endif
364 1.50 ad while (self->pt_mutexwait) {
365 1.64 kre error = _lwp_park(CLOCK_REALTIME, TIMER_ABSTIME,
366 1.77 ad __UNCONST(ts), self->pt_unpark, NULL, NULL);
367 1.50 ad self->pt_unpark = 0;
368 1.60 christos if (__predict_true(error != -1)) {
369 1.60 christos continue;
370 1.60 christos }
371 1.60 christos if (errno == ETIMEDOUT && self->pt_mutexwait) {
372 1.60 christos /*Remove self from waiters list*/
373 1.60 christos pthread__mutex_wakeup(self, ptm);
374 1.60 christos /*priority protect*/
375 1.60 christos if (MUTEX_PROTECT(owner))
376 1.60 christos (void)_sched_protect(-1);
377 1.77 ad errno = serrno;
378 1.60 christos return ETIMEDOUT;
379 1.60 christos }
380 1.44 ad }
381 1.77 ad owner = ptm->ptm_owner;
382 1.2 thorpej }
383 1.2 thorpej }
384 1.2 thorpej
385 1.2 thorpej int
386 1.44 ad pthread_mutex_trylock(pthread_mutex_t *ptm)
387 1.2 thorpej {
388 1.27 ad pthread_t self;
389 1.46 ad void *val, *new, *next;
390 1.2 thorpej
391 1.56 christos if (__predict_false(__uselibcstub))
392 1.56 christos return __libc_mutex_trylock_stub(ptm);
393 1.56 christos
394 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
395 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
396 1.70 kamil
397 1.27 ad self = pthread__self();
398 1.44 ad val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
399 1.44 ad if (__predict_true(val == NULL)) {
400 1.44 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
401 1.44 ad membar_enter();
402 1.44 ad #endif
403 1.44 ad return 0;
404 1.44 ad }
405 1.27 ad
406 1.46 ad if (MUTEX_RECURSIVE(val)) {
407 1.46 ad if (MUTEX_OWNER(val) == 0) {
408 1.46 ad new = (void *)((uintptr_t)self | (uintptr_t)val);
409 1.46 ad next = atomic_cas_ptr(&ptm->ptm_owner, val, new);
410 1.46 ad if (__predict_true(next == val)) {
411 1.46 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
412 1.46 ad membar_enter();
413 1.46 ad #endif
414 1.46 ad return 0;
415 1.46 ad }
416 1.46 ad }
417 1.46 ad if (MUTEX_OWNER(val) == (uintptr_t)self) {
418 1.46 ad if (ptm->ptm_recursed == INT_MAX)
419 1.46 ad return EAGAIN;
420 1.46 ad ptm->ptm_recursed++;
421 1.46 ad return 0;
422 1.46 ad }
423 1.2 thorpej }
424 1.2 thorpej
425 1.44 ad return EBUSY;
426 1.2 thorpej }
427 1.2 thorpej
428 1.2 thorpej int
429 1.44 ad pthread_mutex_unlock(pthread_mutex_t *ptm)
430 1.2 thorpej {
431 1.27 ad pthread_t self;
432 1.77 ad void *val;
433 1.77 ad int error;
434 1.44 ad
435 1.56 christos if (__predict_false(__uselibcstub))
436 1.56 christos return __libc_mutex_unlock_stub(ptm);
437 1.56 christos
438 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
439 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
440 1.70 kamil
441 1.44 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
442 1.44 ad membar_exit();
443 1.44 ad #endif
444 1.77 ad error = 0;
445 1.44 ad self = pthread__self();
446 1.44 ad
447 1.77 ad val = atomic_cas_ptr(&ptm->ptm_owner, self, NULL);
448 1.77 ad if (__predict_false(val != self)) {
449 1.77 ad bool weown = (MUTEX_OWNER(val) == (uintptr_t)self);
450 1.77 ad void *newval = val;
451 1.77 ad if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) {
452 1.77 ad if (!weown) {
453 1.77 ad error = EPERM;
454 1.77 ad newval = val;
455 1.77 ad } else {
456 1.77 ad newval = NULL;
457 1.77 ad }
458 1.77 ad } else if (MUTEX_RECURSIVE(val)) {
459 1.77 ad if (!weown) {
460 1.77 ad error = EPERM;
461 1.77 ad newval = val;
462 1.77 ad } else if (ptm->ptm_recursed) {
463 1.77 ad ptm->ptm_recursed--;
464 1.77 ad newval = val;
465 1.77 ad } else {
466 1.77 ad newval = (pthread_t)MUTEX_RECURSIVE_BIT;
467 1.77 ad }
468 1.44 ad } else {
469 1.77 ad pthread__error(EPERM,
470 1.77 ad "Unlocking unlocked mutex", (val != NULL));
471 1.77 ad pthread__error(EPERM,
472 1.77 ad "Unlocking mutex owned by another thread", weown);
473 1.77 ad newval = NULL;
474 1.44 ad }
475 1.77 ad
476 1.77 ad /*
477 1.77 ad * Release the mutex. If there appear to be waiters, then
478 1.77 ad * wake them up.
479 1.77 ad */
480 1.77 ad if (newval != val) {
481 1.77 ad val = atomic_swap_ptr(&ptm->ptm_owner, newval);
482 1.77 ad if (__predict_false(MUTEX_PROTECT(val))) {
483 1.77 ad /* restore elevated priority */
484 1.77 ad (void)_sched_protect(-1);
485 1.77 ad }
486 1.44 ad }
487 1.44 ad }
488 1.77 ad pthread__smt_wake();
489 1.2 thorpej
490 1.2 thorpej /*
491 1.77 ad * Finally, wake any waiters and return.
492 1.2 thorpej */
493 1.77 ad #ifndef PTHREAD__ATOMIC_IS_MEMBAR
494 1.77 ad membar_enter();
495 1.77 ad #endif
496 1.77 ad if (MUTEX_HAS_WAITERS(val)) {
497 1.77 ad pthread__mutex_wakeup(self, ptm);
498 1.68 ad } else if (self->pt_nwaiters > 0) {
499 1.77 ad pthread__clear_waiters(self);
500 1.2 thorpej }
501 1.44 ad return error;
502 1.44 ad }
503 1.44 ad
504 1.55 yamt /*
505 1.55 yamt * pthread__mutex_wakeup: unpark threads waiting for us
506 1.55 yamt *
507 1.55 yamt * unpark threads on the ptm->ptm_waiters list and self->pt_waiters.
508 1.55 yamt */
509 1.55 yamt
510 1.44 ad static void
511 1.44 ad pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm)
512 1.44 ad {
513 1.44 ad pthread_t thread, next;
514 1.27 ad
515 1.66 ad /* Take ownership of the current set of waiters. */
516 1.44 ad thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL);
517 1.66 ad membar_datadep_consumer(); /* for alpha */
518 1.54 matt pthread__smt_wake();
519 1.44 ad
520 1.77 ad /*
521 1.77 ad * Pull waiters from the queue and add to our list. Use a memory
522 1.77 ad * barrier to ensure that we safely read the value of pt_mutexnext
523 1.77 ad * before 'thread' sees pt_mutexwait being cleared.
524 1.77 ad */
525 1.77 ad while (thread != NULL) {
526 1.77 ad if (self->pt_nwaiters < pthread__unpark_max) {
527 1.77 ad next = thread->pt_mutexnext;
528 1.77 ad if (thread != self) {
529 1.77 ad self->pt_waiters[self->pt_nwaiters++] =
530 1.77 ad thread->pt_lid;
531 1.44 ad membar_sync();
532 1.44 ad }
533 1.50 ad thread->pt_mutexwait = 0;
534 1.44 ad /* No longer safe to touch 'thread' */
535 1.77 ad thread = next;
536 1.77 ad continue;
537 1.44 ad }
538 1.77 ad pthread__clear_waiters(self);
539 1.77 ad }
540 1.77 ad if (self->pt_nwaiters > 0) {
541 1.77 ad pthread__clear_waiters(self);
542 1.44 ad }
543 1.2 thorpej }
544 1.55 yamt
545 1.2 thorpej int
546 1.2 thorpej pthread_mutexattr_init(pthread_mutexattr_t *attr)
547 1.2 thorpej {
548 1.76 kamil #if 0
549 1.56 christos if (__predict_false(__uselibcstub))
550 1.56 christos return __libc_mutexattr_init_stub(attr);
551 1.76 kamil #endif
552 1.2 thorpej
553 1.2 thorpej attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
554 1.44 ad attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
555 1.2 thorpej return 0;
556 1.2 thorpej }
557 1.2 thorpej
558 1.2 thorpej int
559 1.2 thorpej pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
560 1.2 thorpej {
561 1.56 christos if (__predict_false(__uselibcstub))
562 1.56 christos return __libc_mutexattr_destroy_stub(attr);
563 1.2 thorpej
564 1.14 nathanw pthread__error(EINVAL, "Invalid mutex attribute",
565 1.14 nathanw attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
566 1.2 thorpej
567 1.69 kamil attr->ptma_magic = _PT_MUTEXATTR_DEAD;
568 1.69 kamil
569 1.2 thorpej return 0;
570 1.2 thorpej }
571 1.2 thorpej
572 1.2 thorpej int
573 1.2 thorpej pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
574 1.2 thorpej {
575 1.60 christos
576 1.14 nathanw pthread__error(EINVAL, "Invalid mutex attribute",
577 1.14 nathanw attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
578 1.2 thorpej
579 1.60 christos *typep = MUTEX_GET_TYPE(attr->ptma_private);
580 1.2 thorpej return 0;
581 1.2 thorpej }
582 1.2 thorpej
583 1.2 thorpej int
584 1.2 thorpej pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
585 1.2 thorpej {
586 1.60 christos
587 1.56 christos if (__predict_false(__uselibcstub))
588 1.56 christos return __libc_mutexattr_settype_stub(attr, type);
589 1.2 thorpej
590 1.14 nathanw pthread__error(EINVAL, "Invalid mutex attribute",
591 1.14 nathanw attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
592 1.13 nathanw
593 1.2 thorpej switch (type) {
594 1.2 thorpej case PTHREAD_MUTEX_NORMAL:
595 1.2 thorpej case PTHREAD_MUTEX_ERRORCHECK:
596 1.2 thorpej case PTHREAD_MUTEX_RECURSIVE:
597 1.60 christos MUTEX_SET_TYPE(attr->ptma_private, type);
598 1.60 christos return 0;
599 1.60 christos default:
600 1.60 christos return EINVAL;
601 1.60 christos }
602 1.60 christos }
603 1.60 christos
604 1.60 christos int
605 1.60 christos pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int*proto)
606 1.60 christos {
607 1.60 christos
608 1.60 christos pthread__error(EINVAL, "Invalid mutex attribute",
609 1.60 christos attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
610 1.60 christos
611 1.60 christos *proto = MUTEX_GET_PROTOCOL(attr->ptma_private);
612 1.60 christos return 0;
613 1.60 christos }
614 1.60 christos
615 1.60 christos int
616 1.60 christos pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int proto)
617 1.60 christos {
618 1.60 christos
619 1.60 christos pthread__error(EINVAL, "Invalid mutex attribute",
620 1.60 christos attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
621 1.60 christos
622 1.60 christos switch (proto) {
623 1.60 christos case PTHREAD_PRIO_NONE:
624 1.60 christos case PTHREAD_PRIO_PROTECT:
625 1.60 christos MUTEX_SET_PROTOCOL(attr->ptma_private, proto);
626 1.44 ad return 0;
627 1.60 christos case PTHREAD_PRIO_INHERIT:
628 1.60 christos return ENOTSUP;
629 1.2 thorpej default:
630 1.2 thorpej return EINVAL;
631 1.2 thorpej }
632 1.2 thorpej }
633 1.2 thorpej
634 1.60 christos int
635 1.60 christos pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *ceil)
636 1.60 christos {
637 1.60 christos
638 1.60 christos pthread__error(EINVAL, "Invalid mutex attribute",
639 1.60 christos attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
640 1.60 christos
641 1.60 christos *ceil = MUTEX_GET_CEILING(attr->ptma_private);
642 1.60 christos return 0;
643 1.60 christos }
644 1.60 christos
645 1.60 christos int
646 1.60 christos pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int ceil)
647 1.60 christos {
648 1.60 christos
649 1.60 christos pthread__error(EINVAL, "Invalid mutex attribute",
650 1.60 christos attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
651 1.60 christos
652 1.60 christos if (ceil & ~0xff)
653 1.60 christos return EINVAL;
654 1.60 christos
655 1.60 christos MUTEX_SET_CEILING(attr->ptma_private, ceil);
656 1.60 christos return 0;
657 1.60 christos }
658 1.60 christos
659 1.60 christos #ifdef _PTHREAD_PSHARED
660 1.60 christos int
661 1.60 christos pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,
662 1.60 christos int * __restrict pshared)
663 1.60 christos {
664 1.60 christos
665 1.70 kamil pthread__error(EINVAL, "Invalid mutex attribute",
666 1.70 kamil attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
667 1.70 kamil
668 1.60 christos *pshared = PTHREAD_PROCESS_PRIVATE;
669 1.60 christos return 0;
670 1.60 christos }
671 1.60 christos
672 1.60 christos int
673 1.60 christos pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
674 1.60 christos {
675 1.60 christos
676 1.70 kamil pthread__error(EINVAL, "Invalid mutex attribute",
677 1.70 kamil attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
678 1.70 kamil
679 1.60 christos switch(pshared) {
680 1.60 christos case PTHREAD_PROCESS_PRIVATE:
681 1.60 christos return 0;
682 1.60 christos case PTHREAD_PROCESS_SHARED:
683 1.60 christos return ENOSYS;
684 1.60 christos }
685 1.60 christos return EINVAL;
686 1.60 christos }
687 1.60 christos #endif
688 1.60 christos
689 1.55 yamt /*
690 1.55 yamt * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters
691 1.55 yamt *
692 1.77 ad * In order to avoid unnecessary contention on interlocking mutexes, we try
693 1.77 ad * to defer waking up threads until we unlock the mutex. The threads will
694 1.77 ad * be woken up when the calling thread (self) releases a mutex.
695 1.55 yamt */
696 1.50 ad void
697 1.50 ad pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm)
698 1.33 ad {
699 1.33 ad
700 1.50 ad if (__predict_false(ptm == NULL ||
701 1.50 ad MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) {
702 1.77 ad pthread__clear_waiters(self);
703 1.50 ad }
704 1.33 ad }
705 1.33 ad
706 1.39 ad int
707 1.61 skrll pthread_mutex_getprioceiling(const pthread_mutex_t *ptm, int *ceil)
708 1.60 christos {
709 1.70 kamil
710 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
711 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
712 1.70 kamil
713 1.62 skrll *ceil = ptm->ptm_ceiling;
714 1.60 christos return 0;
715 1.60 christos }
716 1.60 christos
717 1.60 christos int
718 1.60 christos pthread_mutex_setprioceiling(pthread_mutex_t *ptm, int ceil, int *old_ceil)
719 1.60 christos {
720 1.60 christos int error;
721 1.60 christos
722 1.70 kamil pthread__error(EINVAL, "Invalid mutex",
723 1.70 kamil ptm->ptm_magic == _PT_MUTEX_MAGIC);
724 1.70 kamil
725 1.60 christos error = pthread_mutex_lock(ptm);
726 1.60 christos if (error == 0) {
727 1.62 skrll *old_ceil = ptm->ptm_ceiling;
728 1.60 christos /*check range*/
729 1.62 skrll ptm->ptm_ceiling = ceil;
730 1.60 christos pthread_mutex_unlock(ptm);
731 1.60 christos }
732 1.60 christos return error;
733 1.60 christos }
734 1.60 christos
735 1.60 christos int
736 1.44 ad _pthread_mutex_held_np(pthread_mutex_t *ptm)
737 1.39 ad {
738 1.39 ad
739 1.44 ad return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self();
740 1.39 ad }
741 1.39 ad
742 1.39 ad pthread_t
743 1.44 ad _pthread_mutex_owner_np(pthread_mutex_t *ptm)
744 1.39 ad {
745 1.39 ad
746 1.44 ad return (pthread_t)MUTEX_OWNER(ptm->ptm_owner);
747 1.39 ad }
748