2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3 * Copyright (c) 2006 David Xu <yfxu@corp.netease.com>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by John Birrell.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $DragonFly: src/lib/libthread_xu/thread/thr_mutex.c,v 1.15 2008/05/09 16:03:27 dillon Exp $
36 #include "namespace.h"
37 #include <machine/tls.h>
42 #include <sys/queue.h>
44 #include "un-namespace.h"
46 #include "thr_private.h"
48 #if defined(_PTHREADS_INVARIANTS)
49 #define MUTEX_INIT_LINK(m) do { \
50 (m)->m_qe.tqe_prev = NULL; \
51 (m)->m_qe.tqe_next = NULL; \
53 #define MUTEX_ASSERT_IS_OWNED(m) do { \
54 if ((m)->m_qe.tqe_prev == NULL) \
55 PANIC("mutex is not on list"); \
57 #define MUTEX_ASSERT_NOT_OWNED(m) do { \
58 if (((m)->m_qe.tqe_prev != NULL) || \
59 ((m)->m_qe.tqe_next != NULL)) \
60 PANIC("mutex is on list"); \
62 #define THR_ASSERT_NOT_IN_SYNCQ(thr) do { \
63 THR_ASSERT(((thr)->sflags & THR_FLAGS_IN_SYNCQ) == 0, \
64 "thread in syncq when it shouldn't be."); \
67 #define MUTEX_INIT_LINK(m)
68 #define MUTEX_ASSERT_IS_OWNED(m)
69 #define MUTEX_ASSERT_NOT_OWNED(m)
70 #define THR_ASSERT_NOT_IN_SYNCQ(thr)
73 #define THR_IN_MUTEXQ(thr) (((thr)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
74 #define MUTEX_DESTROY(m) do { \
78 umtx_t _mutex_static_lock;
83 static int mutex_self_trylock(pthread_mutex_t);
84 static int mutex_self_lock(pthread_mutex_t,
85 const struct timespec *abstime);
86 static int mutex_unlock_common(pthread_mutex_t *);
88 int __pthread_mutex_init(pthread_mutex_t *mutex,
89 const pthread_mutexattr_t *mutex_attr);
90 int __pthread_mutex_trylock(pthread_mutex_t *mutex);
91 int __pthread_mutex_lock(pthread_mutex_t *mutex);
92 int __pthread_mutex_timedlock(pthread_mutex_t *mutex,
93 const struct timespec *abs_timeout);
96 mutex_init(pthread_mutex_t *mutex,
97 const pthread_mutexattr_t *mutex_attr, int private)
99 const struct pthread_mutex_attr *attr;
100 struct pthread_mutex *pmutex;
102 if (mutex_attr == NULL) {
103 attr = &_pthread_mutexattr_default;
106 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
107 attr->m_type >= MUTEX_TYPE_MAX)
109 if (attr->m_protocol < PTHREAD_PRIO_NONE ||
110 attr->m_protocol > PTHREAD_PRIO_PROTECT)
114 if ((pmutex = (pthread_mutex_t)
115 malloc(sizeof(struct pthread_mutex))) == NULL)
118 _thr_umtx_init(&pmutex->m_lock);
119 pmutex->m_type = attr->m_type;
120 pmutex->m_protocol = attr->m_protocol;
121 TAILQ_INIT(&pmutex->m_queue);
122 pmutex->m_owner = NULL;
123 pmutex->m_flags = attr->m_flags | MUTEX_FLAGS_INITED;
125 pmutex->m_flags |= MUTEX_FLAGS_PRIVATE;
127 pmutex->m_refcount = 0;
128 if (attr->m_protocol == PTHREAD_PRIO_PROTECT)
129 pmutex->m_prio = attr->m_ceiling;
132 pmutex->m_saved_prio = 0;
133 MUTEX_INIT_LINK(pmutex);
139 init_static(struct pthread *thread, pthread_mutex_t *mutex)
143 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
146 ret = mutex_init(mutex, NULL, 0);
150 THR_LOCK_RELEASE(thread, &_mutex_static_lock);
156 init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
160 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
163 ret = mutex_init(mutex, NULL, 1);
167 THR_LOCK_RELEASE(thread, &_mutex_static_lock);
173 _pthread_mutex_init(pthread_mutex_t *mutex,
174 const pthread_mutexattr_t *mutex_attr)
176 return mutex_init(mutex, mutex_attr, 1);
180 __pthread_mutex_init(pthread_mutex_t *mutex,
181 const pthread_mutexattr_t *mutex_attr)
183 return mutex_init(mutex, mutex_attr, 0);
187 _mutex_reinit(pthread_mutex_t *mutex)
189 _thr_umtx_init(&(*mutex)->m_lock);
190 TAILQ_INIT(&(*mutex)->m_queue);
191 MUTEX_INIT_LINK(*mutex);
192 (*mutex)->m_owner = NULL;
193 (*mutex)->m_count = 0;
194 (*mutex)->m_refcount = 0;
195 (*mutex)->m_prio = 0;
196 (*mutex)->m_saved_prio = 0;
201 _mutex_fork(struct pthread *curthread)
203 struct pthread_mutex *m;
205 TAILQ_FOREACH(m, &curthread->mutexq, m_qe)
206 m->m_lock = UMTX_LOCKED;
210 _pthread_mutex_destroy(pthread_mutex_t *mutex)
212 struct pthread *curthread = tls_get_curthread();
218 else if (*mutex == NULL)
222 * Try to lock the mutex structure, we only need to
223 * try once, if failed, the mutex is in used.
225 ret = THR_UMTX_TRYLOCK(curthread, &(*mutex)->m_lock);
230 * Check mutex other fields to see if this mutex is
231 * in use. Mostly for prority mutex types, or there
232 * are condition variables referencing it.
234 if (((*mutex)->m_owner != NULL) ||
235 (TAILQ_FIRST(&(*mutex)->m_queue) != NULL) ||
236 ((*mutex)->m_refcount != 0)) {
237 THR_UMTX_UNLOCK(curthread, &(*mutex)->m_lock);
241 * Save a pointer to the mutex so it can be free'd
242 * and set the caller's pointer to NULL:
247 /* Unlock the mutex structure: */
248 THR_UMTX_UNLOCK(curthread, &m->m_lock);
251 * Free the memory allocated for the mutex
254 MUTEX_ASSERT_NOT_OWNED(m);
259 /* Return the completion status: */
264 mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
266 struct pthread_mutex *m;
270 ret = THR_UMTX_TRYLOCK(curthread, &m->m_lock);
272 m->m_owner = curthread;
273 /* Add to the list of owned mutexes: */
274 MUTEX_ASSERT_NOT_OWNED(m);
275 TAILQ_INSERT_TAIL(&curthread->mutexq,
277 } else if (m->m_owner == curthread) {
278 ret = mutex_self_trylock(m);
285 __pthread_mutex_trylock(pthread_mutex_t *m)
287 struct pthread *curthread = tls_get_curthread();
290 if (__predict_false(m == NULL))
293 * If the mutex is statically initialized, perform the dynamic
296 if (__predict_false(*m == NULL)) {
297 ret = init_static(curthread, m);
298 if (__predict_false(ret != 0))
301 return (mutex_trylock_common(curthread, m));
305 _pthread_mutex_trylock(pthread_mutex_t *m)
307 struct pthread *curthread = tls_get_curthread();
311 * If the mutex is statically initialized, perform the dynamic
312 * initialization marking the mutex private (delete safe):
314 if (__predict_false(*m == NULL)) {
315 ret = init_static_private(curthread, m);
316 if (__predict_false(ret != 0))
319 return (mutex_trylock_common(curthread, m));
323 mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
324 const struct timespec * abstime)
326 struct timespec ts, ts2;
327 struct pthread_mutex *m;
331 ret = THR_UMTX_TRYLOCK(curthread, &m->m_lock);
333 m->m_owner = curthread;
334 /* Add to the list of owned mutexes: */
335 MUTEX_ASSERT_NOT_OWNED(m);
336 TAILQ_INSERT_TAIL(&curthread->mutexq,
338 } else if (m->m_owner == curthread) {
339 ret = mutex_self_lock(m, abstime);
341 if (abstime == NULL) {
342 THR_UMTX_LOCK(curthread, &m->m_lock);
344 } else if (__predict_false(
345 abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
346 abstime->tv_nsec >= 1000000000)) {
349 clock_gettime(CLOCK_REALTIME, &ts);
350 TIMESPEC_SUB(&ts2, abstime, &ts);
351 ret = THR_UMTX_TIMEDLOCK(curthread,
354 * Timed out wait is not restarted if
355 * it was interrupted, not worth to do it.
361 m->m_owner = curthread;
362 /* Add to the list of owned mutexes: */
363 MUTEX_ASSERT_NOT_OWNED(m);
364 TAILQ_INSERT_TAIL(&curthread->mutexq,
372 __pthread_mutex_lock(pthread_mutex_t *m)
374 struct pthread *curthread;
377 if (__predict_false(m == NULL))
381 * If the mutex is statically initialized, perform the dynamic
384 curthread = tls_get_curthread();
385 if (__predict_false(*m == NULL)) {
386 ret = init_static(curthread, m);
387 if (__predict_false(ret))
390 return (mutex_lock_common(curthread, m, NULL));
394 _pthread_mutex_lock(pthread_mutex_t *m)
396 struct pthread *curthread;
399 if (__predict_false(m == NULL))
403 * If the mutex is statically initialized, perform the dynamic
404 * initialization marking it private (delete safe):
406 curthread = tls_get_curthread();
407 if (__predict_false(*m == NULL)) {
408 ret = init_static_private(curthread, m);
409 if (__predict_false(ret))
412 return (mutex_lock_common(curthread, m, NULL));
416 __pthread_mutex_timedlock(pthread_mutex_t *m,
417 const struct timespec *abs_timeout)
419 struct pthread *curthread;
422 if (__predict_false(m == NULL))
426 * If the mutex is statically initialized, perform the dynamic
429 curthread = tls_get_curthread();
430 if (__predict_false(*m == NULL)) {
431 ret = init_static(curthread, m);
432 if (__predict_false(ret))
435 return (mutex_lock_common(curthread, m, abs_timeout));
439 _pthread_mutex_timedlock(pthread_mutex_t *m,
440 const struct timespec *abs_timeout)
442 struct pthread *curthread;
445 if (__predict_false(m == NULL))
448 curthread = tls_get_curthread();
451 * If the mutex is statically initialized, perform the dynamic
452 * initialization marking it private (delete safe):
454 if (__predict_false(*m == NULL)) {
455 ret = init_static_private(curthread, m);
456 if (__predict_false(ret))
459 return (mutex_lock_common(curthread, m, abs_timeout));
463 _pthread_mutex_unlock(pthread_mutex_t *m)
465 if (__predict_false(m == NULL))
467 return (mutex_unlock_common(m));
471 mutex_self_trylock(pthread_mutex_t m)
476 /* case PTHREAD_MUTEX_DEFAULT: */
477 case PTHREAD_MUTEX_ERRORCHECK:
478 case PTHREAD_MUTEX_NORMAL:
482 case PTHREAD_MUTEX_RECURSIVE:
483 /* Increment the lock count: */
484 if (m->m_count + 1 > 0) {
492 /* Trap invalid mutex types; */
500 mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
502 struct timespec ts1, ts2;
506 /* case PTHREAD_MUTEX_DEFAULT: */
507 case PTHREAD_MUTEX_ERRORCHECK:
509 clock_gettime(CLOCK_REALTIME, &ts1);
510 TIMESPEC_SUB(&ts2, abstime, &ts1);
511 __sys_nanosleep(&ts2, NULL);
515 * POSIX specifies that mutexes should return
516 * EDEADLK if a recursive lock is detected.
522 case PTHREAD_MUTEX_NORMAL:
524 * What SS2 define as a 'normal' mutex. Intentionally
525 * deadlock on attempts to get a lock you already own.
529 clock_gettime(CLOCK_REALTIME, &ts1);
530 TIMESPEC_SUB(&ts2, abstime, &ts1);
531 __sys_nanosleep(&ts2, NULL);
537 __sys_nanosleep(&ts1, NULL);
541 case PTHREAD_MUTEX_RECURSIVE:
542 /* Increment the lock count: */
543 if (m->m_count + 1 > 0) {
551 /* Trap invalid mutex types; */
559 mutex_unlock_common(pthread_mutex_t *mutex)
561 struct pthread *curthread = tls_get_curthread();
562 struct pthread_mutex *m;
564 if (__predict_false((m = *mutex)== NULL))
566 if (__predict_false(m->m_owner != curthread))
570 m->m_type == PTHREAD_MUTEX_RECURSIVE &&
575 * Clear the count in case this is a recursive mutex.
579 /* Remove the mutex from the threads queue. */
580 MUTEX_ASSERT_IS_OWNED(m);
581 TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
584 * Hand off the mutex to the next waiting thread.
586 THR_UMTX_UNLOCK(curthread, &m->m_lock);
592 _mutex_cv_lock(pthread_mutex_t *m, int count)
596 if ((ret = _pthread_mutex_lock(m)) == 0) {
598 (*m)->m_count += count;
604 _mutex_cv_unlock(pthread_mutex_t *mutex, int *count)
606 struct pthread *curthread = tls_get_curthread();
607 struct pthread_mutex *m;
609 if (__predict_false(mutex == NULL))
611 if (__predict_false((m = *mutex) == NULL))
613 if (__predict_false(m->m_owner != curthread))
620 /* Remove the mutex from the threads queue. */
621 MUTEX_ASSERT_IS_OWNED(m);
622 TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
624 THR_UMTX_UNLOCK(curthread, &m->m_lock);
629 _mutex_unlock_private(pthread_t pthread)
631 struct pthread_mutex *m, *m_next;
633 for (m = TAILQ_FIRST(&pthread->mutexq); m != NULL; m = m_next) {
634 m_next = TAILQ_NEXT(m, m_qe);
635 if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
636 _pthread_mutex_unlock(&m);
640 __strong_reference(__pthread_mutex_init, pthread_mutex_init);
641 __strong_reference(__pthread_mutex_lock, pthread_mutex_lock);
642 __strong_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
643 __strong_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
645 /* Single underscore versions provided for libc internal usage: */
646 /* No difference between libc and application usage of these: */
647 __strong_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
648 __strong_reference(_pthread_mutex_unlock, pthread_mutex_unlock);