pthread_mutex.c revision 1.5 1 /* $NetBSD: pthread_mutex.c,v 1.5 2003/01/19 21:42:09 thorpej 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
45 #include "pthread.h"
46 #include "pthread_int.h"
47
48 static int pthread_mutex_lock_slow(pthread_mutex_t *);
49
50 __strong_alias(__libc_mutex_init,pthread_mutex_init)
51 __strong_alias(__libc_mutex_lock,pthread_mutex_lock)
52 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock)
53 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock)
54 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy)
55
56 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init)
57 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy)
58 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype)
59
60 __strong_alias(__libc_thr_once,pthread_once)
61
62 struct mutex_private {
63 int type;
64 int recursecount;
65 };
66
67 static const struct mutex_private mutex_private_default = {
68 PTHREAD_MUTEX_DEFAULT,
69 0,
70 };
71
72 struct mutexattr_private {
73 int type;
74 };
75
76 static const struct mutexattr_private mutexattr_private_default = {
77 PTHREAD_MUTEX_DEFAULT,
78 };
79
80 /*
81 * If the mutex does not already have private data (i.e. was statically
82 * initialized), then give it the default.
83 */
84 #define GET_MUTEX_PRIVATE(mutex, mp) \
85 do { \
86 if (__predict_false((mp = (mutex)->ptm_private) == NULL)) { \
87 /* LINTED cast away const */ \
88 mp = ((mutex)->ptm_private = \
89 (void *)&mutex_private_default); \
90 } \
91 } while (/*CONSTCOND*/0)
92
93 int
94 pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
95 {
96 struct mutexattr_private *map;
97 struct mutex_private *mp;
98
99 #ifdef ERRORCHECK
100 if ((mutex == NULL) ||
101 (attr && (attr->ptma_magic != _PT_MUTEXATTR_MAGIC)))
102 return EINVAL;
103 #endif
104
105 if (attr != NULL && (map = attr->ptma_private) != NULL &&
106 memcmp(map, &mutexattr_private_default, sizeof(*map)) != 0) {
107 mp = malloc(sizeof(*mp));
108 if (mp == NULL)
109 return ENOMEM;
110
111 mp->type = map->type;
112 mp->recursecount = 0;
113 } else {
114 /* LINTED cast away const */
115 mp = (struct mutex_private *) &mutex_private_default;
116 }
117
118 mutex->ptm_magic = _PT_MUTEX_MAGIC;
119 mutex->ptm_owner = NULL;
120 pthread_lockinit(&mutex->ptm_lock);
121 pthread_lockinit(&mutex->ptm_interlock);
122 PTQ_INIT(&mutex->ptm_blocked);
123 mutex->ptm_private = mp;
124
125 return 0;
126 }
127
128
129 int
130 pthread_mutex_destroy(pthread_mutex_t *mutex)
131 {
132
133 #ifdef ERRORCHECK
134 if ((mutex == NULL) ||
135 (mutex->ptm_magic != _PT_MUTEX_MAGIC) ||
136 (mutex->ptm_lock != __SIMPLELOCK_UNLOCKED))
137 return EINVAL;
138 #endif
139
140 mutex->ptm_magic = _PT_MUTEX_DEAD;
141 if (mutex->ptm_private != NULL &&
142 mutex->ptm_private != (const void *)&mutex_private_default)
143 free(mutex->ptm_private);
144
145 return 0;
146 }
147
148
149 /*
150 * Note regarding memory visibility: Pthreads has rules about memory
151 * visibility and mutexes. Very roughly: Memory a thread can see when
152 * it unlocks a mutex can be seen by another thread that locks the
153 * same mutex.
154 *
155 * A memory barrier after a lock and before an unlock will provide
156 * this behavior. This code relies on pthread__simple_lock_try() to issue
157 * a barrier after obtaining a lock, and on pthread__simple_unlock() to
158 * issue a barrier before releasing a lock.
159 */
160
161 int
162 pthread_mutex_lock(pthread_mutex_t *mutex)
163 {
164 int error;
165
166 #ifdef ERRORCHECK
167 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
168 return EINVAL;
169 #endif
170
171 /*
172 * Note that if we get the lock, we don't have to deal with any
173 * non-default lock type handling.
174 */
175 if (__predict_false(pthread__simple_lock_try(&mutex->ptm_lock) == 0)) {
176 error = pthread_mutex_lock_slow(mutex);
177 if (error)
178 return error;
179 }
180
181 /* We have the lock! */
182 mutex->ptm_owner = pthread__self();
183
184 return 0;
185 }
186
187
188 static int
189 pthread_mutex_lock_slow(pthread_mutex_t *mutex)
190 {
191 pthread_t self;
192
193 self = pthread__self();
194
195 while (/*CONSTCOND*/1) {
196 if (pthread__simple_lock_try(&mutex->ptm_lock))
197 break; /* got it! */
198
199 /* Okay, didn't look free. Get the interlock... */
200 pthread_spinlock(self, &mutex->ptm_interlock);
201 /*
202 * The mutex_unlock routine will get the interlock
203 * before looking at the list of sleepers, so if the
204 * lock is held we can safely put ourselves on the
205 * sleep queue. If it's not held, we can try taking it
206 * again.
207 */
208 if (mutex->ptm_lock == __SIMPLELOCK_LOCKED) {
209 struct mutex_private *mp;
210
211 GET_MUTEX_PRIVATE(mutex, mp);
212
213 if (mutex->ptm_owner == self) {
214 switch (mp->type) {
215 case PTHREAD_MUTEX_ERRORCHECK:
216 pthread_spinunlock(self,
217 &mutex->ptm_interlock);
218 return EDEADLK;
219
220 case PTHREAD_MUTEX_RECURSIVE:
221 /*
222 * It's safe to do this without
223 * holding the interlock, because
224 * we only modify it if we know we
225 * own the mutex.
226 */
227 pthread_spinunlock(self,
228 &mutex->ptm_interlock);
229 if (mp->recursecount == INT_MAX)
230 return EAGAIN;
231 mp->recursecount++;
232 return 0;
233 }
234 }
235
236 PTQ_INSERT_TAIL(&mutex->ptm_blocked, self, pt_sleep);
237 /*
238 * Locking a mutex is not a cancellation
239 * point, so we don't need to do the
240 * test-cancellation dance. We may get woken
241 * up spuriously by pthread_cancel, though,
242 * but it's okay since we're just going to
243 * retry.
244 */
245 pthread_spinlock(self, &self->pt_statelock);
246 self->pt_state = PT_STATE_BLOCKED_QUEUE;
247 self->pt_sleepobj = mutex;
248 self->pt_sleepq = &mutex->ptm_blocked;
249 self->pt_sleeplock = &mutex->ptm_interlock;
250 pthread_spinunlock(self, &self->pt_statelock);
251
252 pthread__block(self, &mutex->ptm_interlock);
253 /* interlock is not held when we return */
254 } else {
255 pthread_spinunlock(self, &mutex->ptm_interlock);
256 }
257 /* Go around for another try. */
258 }
259
260 return 0;
261 }
262
263
264 int
265 pthread_mutex_trylock(pthread_mutex_t *mutex)
266 {
267 pthread_t self = pthread__self();
268
269 #ifdef ERRORCHECK
270 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
271 return EINVAL;
272 #endif
273
274 if (pthread__simple_lock_try(&mutex->ptm_lock) == 0) {
275 struct mutex_private *mp;
276
277 GET_MUTEX_PRIVATE(mutex, mp);
278
279 /*
280 * These tests can be performed without holding the
281 * interlock because these fields are only modified
282 * if we know we own the mutex.
283 */
284 if (mutex->ptm_owner == self) {
285 switch (mp->type) {
286 case PTHREAD_MUTEX_ERRORCHECK:
287 return EDEADLK;
288
289 case PTHREAD_MUTEX_RECURSIVE:
290 if (mp->recursecount == INT_MAX)
291 return EAGAIN;
292 mp->recursecount++;
293 return 0;
294 }
295 }
296
297 return EBUSY;
298 }
299
300 mutex->ptm_owner = self;
301
302 return 0;
303 }
304
305
306 int
307 pthread_mutex_unlock(pthread_mutex_t *mutex)
308 {
309 struct mutex_private *mp;
310 pthread_t self, blocked;
311
312 self = pthread__self();
313
314 #ifdef ERRORCHECK
315 if ((mutex == NULL) || (mutex->ptm_magic != _PT_MUTEX_MAGIC))
316 return EINVAL;
317
318 if (mutex->ptm_lock != __SIMPLELOCK_LOCKED)
319 return EPERM; /* Not exactly the right error. */
320 #endif
321
322 GET_MUTEX_PRIVATE(mutex, mp);
323
324 /*
325 * These tests can be performed without holding the
326 * interlock because these fields are only modified
327 * if we know we own the mutex.
328 */
329 switch (mp->type) {
330 case PTHREAD_MUTEX_ERRORCHECK:
331 if (mutex->ptm_owner != self)
332 return EPERM;
333 break;
334
335 case PTHREAD_MUTEX_RECURSIVE:
336 if (mutex->ptm_owner != self)
337 return EPERM;
338 if (mp->recursecount != 0) {
339 mp->recursecount--;
340 return 0;
341 }
342 break;
343 }
344
345 pthread_spinlock(self, &mutex->ptm_interlock);
346 blocked = PTQ_FIRST(&mutex->ptm_blocked);
347 if (blocked)
348 PTQ_REMOVE(&mutex->ptm_blocked, blocked, pt_sleep);
349 mutex->ptm_owner = NULL;
350 pthread__simple_unlock(&mutex->ptm_lock);
351 pthread_spinunlock(self, &mutex->ptm_interlock);
352
353 /* Give the head of the blocked queue another try. */
354 if (blocked)
355 pthread__sched(self, blocked);
356
357 return 0;
358 }
359
360 int
361 pthread_mutexattr_init(pthread_mutexattr_t *attr)
362 {
363 struct mutexattr_private *map;
364
365 #ifdef ERRORCHECK
366 if (attr == NULL)
367 return EINVAL;
368 #endif
369
370 map = malloc(sizeof(*map));
371 if (map == NULL)
372 return ENOMEM;
373
374 *map = mutexattr_private_default;
375
376 attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
377 attr->ptma_private = map;
378
379 return 0;
380 }
381
382
383 int
384 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
385 {
386
387 #ifdef ERRORCHECK
388 if ((attr == NULL) ||
389 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC))
390 return EINVAL;
391 #endif
392
393 attr->ptma_magic = _PT_MUTEXATTR_DEAD;
394 if (attr->ptma_private != NULL)
395 free(attr->ptma_private);
396
397 return 0;
398 }
399
400
401 int
402 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
403 {
404 struct mutexattr_private *map;
405
406 #ifdef ERRORCHECK
407 if ((attr == NULL) ||
408 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC) ||
409 (typep == NULL))
410 return EINVAL;
411 #endif
412
413 map = attr->ptma_private;
414
415 *typep = map->type;
416
417 return 0;
418 }
419
420
421 int
422 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
423 {
424 struct mutexattr_private *map;
425
426 #ifdef ERRORCHECK
427 if ((attr == NULL) ||
428 (attr->ptma_magic != _PT_MUTEXATTR_MAGIC))
429 return EINVAL;
430 #endif
431 map = attr->ptma_private;
432
433 switch (type) {
434 case PTHREAD_MUTEX_NORMAL:
435 case PTHREAD_MUTEX_ERRORCHECK:
436 case PTHREAD_MUTEX_RECURSIVE:
437 map->type = type;
438 break;
439
440 default:
441 return EINVAL;
442 }
443
444 return 0;
445 }
446
447
448 int
449 pthread_once(pthread_once_t *once_control, void (*routine)(void))
450 {
451
452 if (once_control->pto_done == 0) {
453 pthread_mutex_lock(&once_control->pto_mutex);
454 if (once_control->pto_done == 0) {
455 routine();
456 once_control->pto_done = 1;
457 }
458 pthread_mutex_unlock(&once_control->pto_mutex);
459 }
460
461 return 0;
462 }
463