pthread_mutex.c revision 1.8 1 /* $NetBSD: pthread_mutex.c,v 1.8 2003/01/31 02:55:00 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #include <assert.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "pthread.h"
47 #include "pthread_int.h"
48
49 static int pthread_mutex_lock_slow(pthread_mutex_t *);
50
51 __strong_alias(__libc_mutex_init,pthread_mutex_init)
52 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
53 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
54 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
55 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
56
57 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
58 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
59 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
60
61 __strong_alias(__libc_thr_once,pthread_once)
62
63 struct mutex_private {
64 int type;
65 int recursecount;
66 };
67
68 static const struct mutex_private mutex_private_default = {
69 PTHREAD_MUTEX_DEFAULT,
70 0,
71 };
72
73 struct mutexattr_private {
74 int type;
75 };
76
77 static const struct mutexattr_private mutexattr_private_default = {
78 PTHREAD_MUTEX_DEFAULT,
79 };
80
81 /*
82 * If the mutex does not already have private data (i.e. was statically
83 * initialized), then give it the default.
84 */
85 #define GET_MUTEX_PRIVATE(mutex, mp) \
86 do { \
87 if (__predict_false((mp = (mutex)->ptm_private) == NULL)) { \
88 /* LINTED cast away const */ \
89 mp = ((mutex)->ptm_private = \
90 (void *)&mutex_private_default); \
91 } \
92 } while (/*CONSTCOND*/0)
93
94 int
95 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
96 {
97 struct mutexattr_private *map;
98 struct mutex_private *mp;
99
100 #ifdef ERRORCHECK
101 if ((mutex == NULL) ||
102 (attr && (attr->ptma_magic != _PT_MUTEXATTR_MAGIC)))
103 return EINVAL;
104 #endif
105
106 if (attr != NULL && (map = attr->ptma_private) != NULL &&
107 memcmp(map, &mutexattr_private_default, sizeof(*map)) != 0) {
108 mp = malloc(sizeof(*mp));
109 if (mp == NULL)
110 return ENOMEM;
111
112 mp->type = map->type;
113 mp->recursecount = 0;
114 } else {
115 /* LINTED cast away const */
116 mp = (struct mutex_private *) &mutex_private_default;
117 }
118
119 mutex->ptm_magic = _PT_MUTEX_MAGIC;
120 mutex->ptm_owner = NULL;
121 pthread_lockinit(&mutex->ptm_lock);
122 pthread_lockinit(&mutex->ptm_interlock);
123 PTQ_INIT(&mutex->ptm_blocked);
124 mutex->ptm_private = mp;
125
126 return 0;
127 }
128
129
130 int
131 pthread_mutex_destroy(pthread_mutex_t *mutex)
132 {
133
134 #ifdef ERRORCHECK
135 if ((mutex == NULL) ||
136 (mutex->ptm_magic != _PT_MUTEX_MAGIC) ||
137 (mutex->ptm_lock != __SIMPLELOCK_UNLOCKED))
138 return EINVAL;
139 #endif
140
141 mutex->ptm_magic = _PT_MUTEX_DEAD;
142 if (mutex->ptm_private != NULL &&
143 mutex->ptm_private != (const void *)&mutex_private_default)
144 free(mutex->ptm_private);
145
146 return 0;
147 }
148
149
150 /*
151 * Note regarding memory visibility: Pthreads has rules about memory
152 * visibility and mutexes. Very roughly: Memory a thread can see when
153 * it unlocks a mutex can be seen by another thread that locks the
154 * same mutex.
155 *
156 * A memory barrier after a lock and before an unlock will provide
157 * this behavior. This code relies on pthread__simple_lock_try() to issue
158 * a barrier after obtaining a lock, and on pthread__simple_unlock() to
159 * issue a barrier before releasing a lock.
160 */
161
162 int
163 pthread_mutex_lock(pthread_mutex_t *mutex)
164 {
165 int error;
166
167 #ifdef ERRORCHECK
168 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
169 return EINVAL;
170 #endif
171
172 PTHREADD_ADD(PTHREADD_MUTEX_LOCK);
173 /*
174 * Note that if we get the lock, we don't have to deal with any
175 * non-default lock type handling.
176 */
177 if (__predict_false(pthread__simple_lock_try(&mutex->ptm_lock) == 0)) {
178 error = pthread_mutex_lock_slow(mutex);
179 if (error)
180 return error;
181 }
182
183 /* We have the lock! */
184 /*
185 * Identifying ourselves may be slow, and this assignment is
186 * only needed for (a) debugging identity of the owning thread
187 * and (b) handling errorcheck and recursive mutexes. It's
188 * better to just stash our stack pointer here and let those
189 * slow exception cases compute the stack->thread mapping.
190 */
191 mutex->ptm_owner = (pthread_t)pthread__sp();
192
193 return 0;
194 }
195
196
197 static int
198 pthread_mutex_lock_slow(pthread_mutex_t *mutex)
199 {
200 pthread_t self;
201
202 self = pthread__self();
203
204 PTHREADD_ADD(PTHREADD_MUTEX_LOCK_SLOW);
205 while (/*CONSTCOND*/1) {
206 if (pthread__simple_lock_try(&mutex->ptm_lock))
207 break; /* got it! */
208
209 /* Okay, didn't look free. Get the interlock... */
210 pthread_spinlock(self, &mutex->ptm_interlock);
211 /*
212 * The mutex_unlock routine will get the interlock
213 * before looking at the list of sleepers, so if the
214 * lock is held we can safely put ourselves on the
215 * sleep queue. If it's not held, we can try taking it
216 * again.
217 */
218 if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
219 struct mutex_private *mp;
220
221 GET_MUTEX_PRIVATE(mutex, mp);
222
223 if (pthread__id(mutex->ptm_owner) == self) {
224 switch (mp->type) {
225 case PTHREAD_MUTEX_ERRORCHECK:
226 pthread_spinunlock(self,
227 &mutex->ptm_interlock);
228 return EDEADLK;
229
230 case PTHREAD_MUTEX_RECURSIVE:
231 /*
232 * It's safe to do this without
233 * holding the interlock, because
234 * we only modify it if we know we
235 * own the mutex.
236 */
237 pthread_spinunlock(self,
238 &mutex->ptm_interlock);
239 if (mp->recursecount == INT_MAX)
240 return EAGAIN;
241 mp->recursecount++;
242 return 0;
243 }
244 }
245
246 PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
247 /*
248 * Locking a mutex is not a cancellation
249 * point, so we don't need to do the
250 * test-cancellation dance. We may get woken
251 * up spuriously by pthread_cancel or signals,
252 * but it's okay since we're just going to
253 * retry.
254 */
255 pthread_spinlock(self, &self->pt_statelock);
256 self->pt_state = PT_STATE_BLOCKED_QUEUE;
257 self->pt_sleepobj = mutex;
258 self->pt_sleepq = &mutex->ptm_blocked;
259 self->pt_sleeplock = &mutex->ptm_interlock;
260 pthread_spinunlock(self, &self->pt_statelock);
261
262 pthread__block(self, &mutex->ptm_interlock);
263 /* interlock is not held when we return */
264 } else {
265 pthread_spinunlock(self, &mutex->ptm_interlock);
266 }
267 /* Go around for another try. */
268 }
269
270 return 0;
271 }
272
273
274 int
275 pthread_mutex_trylock(pthread_mutex_t *mutex)
276 {
277
278 #ifdef ERRORCHECK
279 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
280 return EINVAL;
281 #endif
282
283 PTHREADD_ADD(PTHREADD_MUTEX_TRYLOCK);
284 if (pthread__simple_lock_try(&mutex->ptm_lock) == 0) {
285 pthread_t self;
286 struct mutex_private *mp;
287
288 GET_MUTEX_PRIVATE(mutex, mp);
289
290 /*
291 * These tests can be performed without holding the
292 * interlock because these fields are only modified
293 * if we know we own the mutex.
294 */
295 self = pthread__self();
296 if (pthread__id(mutex->ptm_owner) == self) {
297 switch (mp->type) {
298 case PTHREAD_MUTEX_ERRORCHECK:
299 return EDEADLK;
300
301 case PTHREAD_MUTEX_RECURSIVE:
302 if (mp->recursecount == INT_MAX)
303 return EAGAIN;
304 mp->recursecount++;
305 return 0;
306 }
307 }
308
309 return EBUSY;
310 }
311
312 /* see comment at the end of pthread_mutex_lock() */
313 mutex->ptm_owner = (pthread_t)pthread__sp();
314
315 return 0;
316 }
317
318
319 int
320 pthread_mutex_unlock(pthread_mutex_t *mutex)
321 {
322 struct mutex_private *mp;
323 pthread_t self, blocked;
324
325 #ifdef ERRORCHECK
326 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
327 return EINVAL;
328
329 if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
330 return EPERM; /* Not exactly the right error. */
331 #endif
332 PTHREADD_ADD(PTHREADD_MUTEX_UNLOCK);
333
334 GET_MUTEX_PRIVATE(mutex, mp);
335
336 /*
337 * These tests can be performed without holding the
338 * interlock because these fields are only modified
339 * if we know we own the mutex.
340 */
341 switch (mp->type) {
342 case PTHREAD_MUTEX_ERRORCHECK:
343 if (pthread__id(mutex->ptm_owner) != pthread__self())
344 return EPERM;
345 break;
346
347 case PTHREAD_MUTEX_RECURSIVE:
348 if (pthread__id(mutex->ptm_owner) != pthread__self())
349 return EPERM;
350 if (mp->recursecount != 0) {
351 mp->recursecount--;
352 return 0;
353 }
354 break;
355 }
356
357 mutex->ptm_owner = NULL;
358 pthread__simple_unlock(&mutex->ptm_lock);
359 /*
360 * Do a double-checked locking dance to see if there are any
361 * waiters. If we don't see any waiters, we can exit, because
362 * we've already released the lock. If we do see waiters, they
363 * were probably waiting on us... there's a slight chance that
364 * they are waiting on a different thread's ownership of the
365 * lock that happened between the unlock above and this
366 * examination of the queue; if so, no harm is done, as the
367 * waiter will loop and see that the mutex is still locked.
368 */
369 if (!PTQ_EMPTY(&mutex->ptm_blocked)) {
370 self = pthread__self();
371 pthread_spinlock(self, &mutex->ptm_interlock);
372 blocked = PTQ_FIRST(&mutex->ptm_blocked);
373 if (blocked)
374 PTQ_REMOVE(&mutex->ptm_blocked, blocked, pt_sleep);
375 pthread_spinunlock(self, &mutex->ptm_interlock);
376
377 /* Give the head of the blocked queue another try. */
378 if (blocked) {
379 PTHREADD_ADD(PTHREADD_MUTEX_UNLOCK_UNBLOCK);
380 pthread__sched(self, blocked);
381 }
382 }
383 return 0;
384 }
385
386 int
387 pthread_mutexattr_init(pthread_mutexattr_t *attr)
388 {
389 struct mutexattr_private *map;
390
391 #ifdef ERRORCHECK
392 if (attr == NULL)
393 return EINVAL;
394 #endif
395
396 map = malloc(sizeof(*map));
397 if (map == NULL)
398 return ENOMEM;
399
400 *map = mutexattr_private_default;
401
402 attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
403 attr->ptma_private = map;
404
405 return 0;
406 }
407
408
409 int
410 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
411 {
412
413 #ifdef ERRORCHECK
414 if ((attr == NULL) ||
415 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC))
416 return EINVAL;
417 #endif
418
419 attr->ptma_magic = _PT_MUTEXATTR_DEAD;
420 if (attr->ptma_private != NULL)
421 free(attr->ptma_private);
422
423 return 0;
424 }
425
426
427 int
428 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
429 {
430 struct mutexattr_private *map;
431
432 #ifdef ERRORCHECK
433 if ((attr == NULL) ||
434 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC) ||
435 (typep == NULL))
436 return EINVAL;
437 #endif
438
439 map = attr->ptma_private;
440
441 *typep = map->type;
442
443 return 0;
444 }
445
446
447 int
448 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
449 {
450 struct mutexattr_private *map;
451
452 #ifdef ERRORCHECK
453 if ((attr == NULL) ||
454 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC))
455 return EINVAL;
456 #endif
457 map = attr->ptma_private;
458
459 switch (type) {
460 case PTHREAD_MUTEX_NORMAL:
461 case PTHREAD_MUTEX_ERRORCHECK:
462 case PTHREAD_MUTEX_RECURSIVE:
463 map->type = type;
464 break;
465
466 default:
467 return EINVAL;
468 }
469
470 return 0;
471 }
472
473
474 int
475 pthread_once(pthread_once_t *once_control, void (*routine)(void))
476 {
477
478 if (once_control->pto_done == 0) {
479 pthread_mutex_lock(&once_control->pto_mutex);
480 if (once_control->pto_done == 0) {
481 routine();
482 once_control->pto_done = 1;
483 }
484 pthread_mutex_unlock(&once_control->pto_mutex);
485 }
486
487 return 0;
488 }
489