2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3 * Copyright (c) 2006 David Xu <davidxu@freebsd.org>.
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
35 #include "namespace.h"
36 #include <machine/tls.h>
40 #include <sys/queue.h>
42 #include "un-namespace.h"
44 #include "thr_private.h"
46 #ifdef _PTHREADS_DEBUGGING
54 #if defined(_PTHREADS_INVARIANTS)
55 #define MUTEX_INIT_LINK(m) do { \
56 (m)->m_qe.tqe_prev = NULL; \
57 (m)->m_qe.tqe_next = NULL; \
59 #define MUTEX_ASSERT_IS_OWNED(m) do { \
60 if ((m)->m_qe.tqe_prev == NULL) \
61 PANIC("mutex is not on list"); \
63 #define MUTEX_ASSERT_NOT_OWNED(m) do { \
64 if (((m)->m_qe.tqe_prev != NULL) || \
65 ((m)->m_qe.tqe_next != NULL)) \
66 PANIC("mutex is on list"); \
68 #define THR_ASSERT_NOT_IN_SYNCQ(thr) do { \
69 THR_ASSERT(((thr)->sflags & THR_FLAGS_IN_SYNCQ) == 0, \
70 "thread in syncq when it shouldn't be."); \
73 #define MUTEX_INIT_LINK(m)
74 #define MUTEX_ASSERT_IS_OWNED(m)
75 #define MUTEX_ASSERT_NOT_OWNED(m)
76 #define THR_ASSERT_NOT_IN_SYNCQ(thr)
79 #define THR_IN_MUTEXQ(thr) (((thr)->sflags & THR_FLAGS_IN_SYNCQ) != 0)
80 #define MUTEX_DESTROY(m) do { \
84 umtx_t _mutex_static_lock;
86 #ifdef _PTHREADS_DEBUGGING
90 mutex_log(const char *ctl, ...)
97 len = vsnprintf(buf, sizeof(buf), ctl, va);
106 mutex_log(const char *ctl __unused, ...)
112 #ifdef _PTHREADS_DEBUGGING2
115 mutex_log2(struct pthread *curthread, struct pthread_mutex *m, int op)
118 if (curthread->tid < 32)
119 m->m_lastop[curthread->tid] =
120 (__sys_getpid() << 16) | op;
123 (__sys_getpid() << 16) | op;
131 mutex_log2(struct pthread *curthread __unused,
132 struct pthread_mutex *m __unused, int op __unused)
141 static int mutex_self_trylock(pthread_mutex_t);
142 static int mutex_self_lock(pthread_mutex_t,
143 const struct timespec *abstime);
144 static int mutex_unlock_common(pthread_mutex_t *);
146 int __pthread_mutex_init(pthread_mutex_t *mutex,
147 const pthread_mutexattr_t *mutex_attr);
148 int __pthread_mutex_trylock(pthread_mutex_t *mutex);
149 int __pthread_mutex_lock(pthread_mutex_t *mutex);
150 int __pthread_mutex_timedlock(pthread_mutex_t *mutex,
151 const struct timespec *abs_timeout);
154 mutex_check_attr(const struct pthread_mutex_attr *attr)
156 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
157 attr->m_type >= PTHREAD_MUTEX_TYPE_MAX)
159 if (attr->m_protocol < PTHREAD_PRIO_NONE ||
160 attr->m_protocol > PTHREAD_PRIO_PROTECT)
166 mutex_init_body(struct pthread_mutex *pmutex,
167 const struct pthread_mutex_attr *attr, int private)
169 _thr_umtx_init(&pmutex->m_lock);
170 pmutex->m_type = attr->m_type;
171 pmutex->m_protocol = attr->m_protocol;
172 TAILQ_INIT(&pmutex->m_queue);
173 mutex_log2(tls_get_curthread(), pmutex, 32);
174 pmutex->m_owner = NULL;
175 pmutex->m_flags = attr->m_flags | MUTEX_FLAGS_INITED;
177 pmutex->m_flags |= MUTEX_FLAGS_PRIVATE;
179 pmutex->m_refcount = 0;
180 if (attr->m_protocol == PTHREAD_PRIO_PROTECT)
181 pmutex->m_prio = attr->m_ceiling;
184 pmutex->m_saved_prio = 0;
185 MUTEX_INIT_LINK(pmutex);
189 mutex_init(pthread_mutex_t *mutex,
190 const pthread_mutexattr_t *mutex_attr, int private)
192 const struct pthread_mutex_attr *attr;
193 struct pthread_mutex *pmutex;
196 if (mutex_attr == NULL) {
197 attr = &_pthread_mutexattr_default;
200 error = mutex_check_attr(attr);
205 pmutex = __malloc(sizeof(struct pthread_mutex));
208 mutex_init_body(pmutex, attr, private);
214 init_static(struct pthread *thread, pthread_mutex_t *mutex)
218 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
221 ret = mutex_init(mutex, NULL, 0);
224 THR_LOCK_RELEASE(thread, &_mutex_static_lock);
230 init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
234 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
237 ret = mutex_init(mutex, NULL, 1);
241 THR_LOCK_RELEASE(thread, &_mutex_static_lock);
247 _pthread_mutex_init(pthread_mutex_t * __restrict mutex,
248 const pthread_mutexattr_t * __restrict mutex_attr)
250 return mutex_init(mutex, mutex_attr, 1);
254 __pthread_mutex_init(pthread_mutex_t *mutex,
255 const pthread_mutexattr_t *mutex_attr)
257 return mutex_init(mutex, mutex_attr, 0);
262 _mutex_reinit(pthread_mutex_t *mutexp)
264 pthread_mutex_t mutex = *mutexp;
266 _thr_umtx_init(&mutex->m_lock);
267 TAILQ_INIT(&mutex->m_queue);
268 MUTEX_INIT_LINK(mutex);
269 mutex_log2(tls_get_curthread(), mutex, 33);
270 mutex->m_owner = NULL;
272 mutex->m_refcount = 0;
274 mutex->m_saved_prio = 0;
281 _mutex_fork(struct pthread *curthread)
283 struct pthread_mutex *m;
285 TAILQ_FOREACH(m, &curthread->mutexq, m_qe)
286 m->m_lock = UMTX_LOCKED;
290 _pthread_mutex_destroy(pthread_mutex_t *mutex)
292 struct pthread *curthread = tls_get_curthread();
298 } else if (*mutex == NULL) {
302 * Try to lock the mutex structure, we only need to
303 * try once, if failed, the mutex is in use.
305 ret = THR_UMTX_TRYLOCK_PERSIST(curthread, &(*mutex)->m_lock);
310 * Check mutex other fields to see if this mutex is
311 * in use. Mostly for prority mutex types, or there
312 * are condition variables referencing it.
314 if (((*mutex)->m_owner != NULL) ||
315 (TAILQ_FIRST(&(*mutex)->m_queue) != NULL) ||
316 ((*mutex)->m_refcount != 0)) {
317 THR_UMTX_UNLOCK_PERSIST(curthread, &(*mutex)->m_lock);
321 * Save a pointer to the mutex so it can be free'd
322 * and set the caller's pointer to NULL:
327 /* Unlock the mutex structure: */
328 THR_UMTX_UNLOCK_PERSIST(curthread, &m->m_lock);
331 * Free the memory allocated for the mutex
334 MUTEX_ASSERT_NOT_OWNED(m);
339 /* Return the completion status: */
344 mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
346 struct pthread_mutex *m;
350 mutex_log("mutex_lock_trylock_common %p\n", m);
351 ret = THR_UMTX_TRYLOCK_PERSIST(curthread, &m->m_lock);
353 mutex_log2(curthread, m, 1);
354 m->m_owner = curthread;
355 /* Add to the list of owned mutexes: */
356 MUTEX_ASSERT_NOT_OWNED(m);
357 TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
358 } else if (m->m_owner == curthread) {
359 mutex_log2(curthread, m, 2);
360 ret = mutex_self_trylock(m);
362 mutex_log("mutex_lock_trylock_common %p (returns %d)\n", m, ret);
368 __pthread_mutex_trylock(pthread_mutex_t *m)
370 struct pthread *curthread = tls_get_curthread();
373 if (__predict_false(m == NULL))
376 * If the mutex is statically initialized, perform the dynamic
379 if (__predict_false(*m == NULL)) {
380 ret = init_static(curthread, m);
381 if (__predict_false(ret != 0))
384 return (mutex_trylock_common(curthread, m));
388 _pthread_mutex_trylock(pthread_mutex_t *m)
390 struct pthread *curthread = tls_get_curthread();
394 * If the mutex is statically initialized, perform the dynamic
395 * initialization marking the mutex private (delete safe):
397 if (__predict_false(*m == NULL)) {
398 ret = init_static_private(curthread, m);
399 if (__predict_false(ret != 0))
402 return (mutex_trylock_common(curthread, m));
406 mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
407 const struct timespec * abstime)
409 struct timespec ts, ts2;
410 struct pthread_mutex *m;
414 mutex_log("mutex_lock_common %p\n", m);
415 ret = THR_UMTX_TRYLOCK_PERSIST(curthread, &m->m_lock);
417 mutex_log2(curthread, m, 3);
418 m->m_owner = curthread;
419 /* Add to the list of owned mutexes: */
420 MUTEX_ASSERT_NOT_OWNED(m);
421 TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
422 } else if (m->m_owner == curthread) {
423 ret = mutex_self_lock(m, abstime);
425 if (abstime == NULL) {
426 THR_UMTX_LOCK_PERSIST(curthread, &m->m_lock);
428 } else if (__predict_false(
429 abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
430 abstime->tv_nsec >= 1000000000)) {
433 clock_gettime(CLOCK_REALTIME, &ts);
434 timespecsub(abstime, &ts, &ts2);
435 ret = THR_UMTX_TIMEDLOCK_PERSIST(curthread,
439 mutex_log2(curthread, m, 4);
440 m->m_owner = curthread;
441 /* Add to the list of owned mutexes: */
442 MUTEX_ASSERT_NOT_OWNED(m);
443 TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
446 mutex_log("mutex_lock_common %p (returns %d) lock %d,%d\n",
447 m, ret, m->m_lock, m->m_count);
452 __pthread_mutex_lock(pthread_mutex_t *m)
454 struct pthread *curthread;
457 if (__predict_false(m == NULL))
461 * If the mutex is statically initialized, perform the dynamic
464 curthread = tls_get_curthread();
465 if (__predict_false(*m == NULL)) {
466 ret = init_static(curthread, m);
467 if (__predict_false(ret))
470 return (mutex_lock_common(curthread, m, NULL));
474 _pthread_mutex_lock(pthread_mutex_t *m)
476 struct pthread *curthread;
481 if (__predict_false(m == NULL))
485 * If the mutex is statically initialized, perform the dynamic
486 * initialization marking it private (delete safe):
488 curthread = tls_get_curthread();
489 if (__predict_false(*m == NULL)) {
490 ret = init_static_private(curthread, m);
491 if (__predict_false(ret))
494 return (mutex_lock_common(curthread, m, NULL));
498 __pthread_mutex_timedlock(pthread_mutex_t * __restrict m,
499 const struct timespec * __restrict abs_timeout)
501 struct pthread *curthread;
506 if (__predict_false(m == NULL))
510 * If the mutex is statically initialized, perform the dynamic
513 curthread = tls_get_curthread();
514 if (__predict_false(*m == NULL)) {
515 ret = init_static(curthread, m);
516 if (__predict_false(ret))
519 return (mutex_lock_common(curthread, m, abs_timeout));
523 _pthread_mutex_timedlock(pthread_mutex_t *m,
524 const struct timespec *abs_timeout)
526 struct pthread *curthread;
529 if (__predict_false(m == NULL))
532 curthread = tls_get_curthread();
535 * If the mutex is statically initialized, perform the dynamic
536 * initialization marking it private (delete safe):
538 if (__predict_false(*m == NULL)) {
539 ret = init_static_private(curthread, m);
540 if (__predict_false(ret))
543 return (mutex_lock_common(curthread, m, abs_timeout));
547 _pthread_mutex_unlock(pthread_mutex_t *m)
549 if (__predict_false(m == NULL))
551 return (mutex_unlock_common(m));
555 mutex_self_trylock(pthread_mutex_t m)
560 /* case PTHREAD_MUTEX_DEFAULT: */
561 case PTHREAD_MUTEX_ERRORCHECK:
562 case PTHREAD_MUTEX_NORMAL:
566 case PTHREAD_MUTEX_RECURSIVE:
567 /* Increment the lock count: */
568 if (m->m_count + 1 > 0) {
576 /* Trap invalid mutex types; */
584 mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
586 struct timespec ts1, ts2;
590 /* case PTHREAD_MUTEX_DEFAULT: */
591 case PTHREAD_MUTEX_ERRORCHECK:
593 clock_gettime(CLOCK_REALTIME, &ts1);
594 timespecsub(abstime, &ts1, &ts2);
595 __sys_nanosleep(&ts2, NULL);
599 * POSIX specifies that mutexes should return
600 * EDEADLK if a recursive lock is detected.
606 case PTHREAD_MUTEX_NORMAL:
608 * What SS2 define as a 'normal' mutex. Intentionally
609 * deadlock on attempts to get a lock you already own.
613 clock_gettime(CLOCK_REALTIME, &ts1);
614 timespecsub(abstime, &ts1, &ts2);
615 __sys_nanosleep(&ts2, NULL);
621 __sys_nanosleep(&ts1, NULL);
625 case PTHREAD_MUTEX_RECURSIVE:
626 /* Increment the lock count: */
627 if (m->m_count + 1 > 0) {
635 /* Trap invalid mutex types; */
643 mutex_unlock_common(pthread_mutex_t *mutex)
645 struct pthread *curthread = tls_get_curthread();
646 struct pthread_mutex *m;
648 if (__predict_false((m = *mutex) == NULL)) {
649 mutex_log2(curthread, m, 252);
652 mutex_log("mutex_unlock_common %p\n", m);
653 if (__predict_false(m->m_owner != curthread)) {
654 mutex_log("mutex_unlock_common %p (failedA)\n", m);
655 mutex_log2(curthread, m, 253);
659 if (__predict_false(m->m_type == PTHREAD_MUTEX_RECURSIVE &&
662 mutex_log("mutex_unlock_common %p (returns 0, partial)\n", m);
663 mutex_log2(curthread, m, 254);
666 * Clear the count in case this is a recursive mutex.
670 /* Remove the mutex from the threads queue. */
671 MUTEX_ASSERT_IS_OWNED(m);
672 TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
673 mutex_log2(tls_get_curthread(), m, 35);
675 mutex_log2(tls_get_curthread(), m, 36);
677 * Hand off the mutex to the next waiting thread.
679 mutex_log("mutex_unlock_common %p (returns 0) lock %d\n",
681 THR_UMTX_UNLOCK_PERSIST(curthread, &m->m_lock);
682 mutex_log2(tls_get_curthread(), m, 37);
683 mutex_log2(curthread, m, 255);
689 _pthread_mutex_getprioceiling(const pthread_mutex_t * __restrict mutex,
690 int * __restrict prioceiling)
692 if ((mutex == NULL) || (*mutex == NULL))
694 if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT)
696 *prioceiling = (*mutex)->m_prio;
701 _pthread_mutex_setprioceiling(pthread_mutex_t * __restrict mutex,
702 int prioceiling, int * __restrict old_ceiling)
707 if ((mutex == NULL) || (*mutex == NULL))
709 else if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT)
711 else if ((ret = _pthread_mutex_lock(mutex)) == 0) {
712 tmp = (*mutex)->m_prio;
713 (*mutex)->m_prio = prioceiling;
714 ret = _pthread_mutex_unlock(mutex);
721 _mutex_cv_lock(pthread_mutex_t *m, int count)
725 if ((ret = _pthread_mutex_lock(m)) == 0) {
727 (*m)->m_count += count;
733 _mutex_cv_unlock(pthread_mutex_t *mutex, int *count)
735 struct pthread *curthread = tls_get_curthread();
736 struct pthread_mutex *m;
738 if (__predict_false(mutex == NULL))
740 if (__predict_false((m = *mutex) == NULL))
742 if (__predict_false(m->m_owner != curthread))
748 mutex_log2(tls_get_curthread(), m, 45);
750 /* Remove the mutex from the threads queue. */
751 MUTEX_ASSERT_IS_OWNED(m);
752 TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
754 THR_UMTX_UNLOCK_PERSIST(curthread, &m->m_lock);
755 mutex_log2(curthread, m, 250);
760 _mutex_unlock_private(pthread_t pthread)
762 struct pthread_mutex *m, *m_next;
764 for (m = TAILQ_FIRST(&pthread->mutexq); m != NULL; m = m_next) {
765 m_next = TAILQ_NEXT(m, m_qe);
766 if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
767 _pthread_mutex_unlock(&m);
771 __strong_reference(__pthread_mutex_init, pthread_mutex_init);
772 __strong_reference(__pthread_mutex_lock, pthread_mutex_lock);
773 __strong_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
774 __strong_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
776 /* Single underscore versions provided for libc internal usage: */
777 /* No difference between libc and application usage of these: */
778 __strong_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
779 __strong_reference(_pthread_mutex_unlock, pthread_mutex_unlock);
780 __strong_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling);
781 __strong_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling);