2003-04-01 03:46:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
|
2006-03-27 23:50:21 +00:00
|
|
|
* Copyright (c) 2006 David Xu <davidxu@freebsd.org>.
|
2003-04-01 03:46:29 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by John Birrell.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include "thr_private.h"
|
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
#if defined(_PTHREADS_INVARIANTS)
|
2005-04-02 01:20:00 +00:00
|
|
|
#define MUTEX_INIT_LINK(m) do { \
|
2003-04-01 22:39:31 +00:00
|
|
|
(m)->m_qe.tqe_prev = NULL; \
|
|
|
|
(m)->m_qe.tqe_next = NULL; \
|
|
|
|
} while (0)
|
2005-04-02 01:20:00 +00:00
|
|
|
#define MUTEX_ASSERT_IS_OWNED(m) do { \
|
2003-04-01 22:39:31 +00:00
|
|
|
if ((m)->m_qe.tqe_prev == NULL) \
|
|
|
|
PANIC("mutex is not on list"); \
|
|
|
|
} while (0)
|
2005-04-02 01:20:00 +00:00
|
|
|
#define MUTEX_ASSERT_NOT_OWNED(m) do { \
|
2003-04-01 22:39:31 +00:00
|
|
|
if (((m)->m_qe.tqe_prev != NULL) || \
|
|
|
|
((m)->m_qe.tqe_next != NULL)) \
|
|
|
|
PANIC("mutex is on list"); \
|
|
|
|
} while (0)
|
|
|
|
#else
|
2005-04-02 01:20:00 +00:00
|
|
|
#define MUTEX_INIT_LINK(m)
|
|
|
|
#define MUTEX_ASSERT_IS_OWNED(m)
|
|
|
|
#define MUTEX_ASSERT_NOT_OWNED(m)
|
2003-04-01 22:39:31 +00:00
|
|
|
#endif
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
2006-03-27 23:50:21 +00:00
|
|
|
static int mutex_self_trylock(struct pthread *, pthread_mutex_t);
|
|
|
|
static int mutex_self_lock(struct pthread *, pthread_mutex_t,
|
2005-04-02 01:20:00 +00:00
|
|
|
const struct timespec *abstime);
|
2006-03-27 23:50:21 +00:00
|
|
|
static int mutex_unlock_common(pthread_mutex_t *, int);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
__weak_reference(__pthread_mutex_init, pthread_mutex_init);
|
2003-04-01 03:46:29 +00:00
|
|
|
__weak_reference(__pthread_mutex_lock, pthread_mutex_lock);
|
2005-04-02 01:20:00 +00:00
|
|
|
__weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
|
|
|
|
__weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Single underscore versions provided for libc internal usage: */
|
2003-04-01 03:46:29 +00:00
|
|
|
/* No difference between libc and application usage of these: */
|
|
|
|
__weak_reference(_pthread_mutex_destroy, pthread_mutex_destroy);
|
2005-04-02 01:20:00 +00:00
|
|
|
__weak_reference(_pthread_mutex_unlock, pthread_mutex_unlock);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
__weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling);
|
|
|
|
__weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling);
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
mutex_init(pthread_mutex_t *mutex,
|
|
|
|
const pthread_mutexattr_t *mutex_attr, int private)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2006-02-28 06:06:19 +00:00
|
|
|
const struct pthread_mutex_attr *attr;
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread_mutex *pmutex;
|
|
|
|
|
2006-02-28 06:06:19 +00:00
|
|
|
if (mutex_attr == NULL) {
|
2006-03-27 23:50:21 +00:00
|
|
|
attr = &_pthread_mutexattr_default;
|
2006-02-28 06:06:19 +00:00
|
|
|
} else {
|
|
|
|
attr = *mutex_attr;
|
|
|
|
if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
|
|
|
|
attr->m_type >= PTHREAD_MUTEX_TYPE_MAX)
|
|
|
|
return (EINVAL);
|
|
|
|
if (attr->m_protocol < PTHREAD_PRIO_NONE ||
|
|
|
|
attr->m_protocol > PTHREAD_PRIO_PROTECT)
|
|
|
|
return (EINVAL);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
2006-02-28 06:06:19 +00:00
|
|
|
if ((pmutex = (pthread_mutex_t)
|
|
|
|
malloc(sizeof(struct pthread_mutex))) == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
_thr_umtx_init(&pmutex->m_lock);
|
|
|
|
pmutex->m_type = attr->m_type;
|
|
|
|
pmutex->m_protocol = attr->m_protocol;
|
|
|
|
TAILQ_INIT(&pmutex->m_queue);
|
|
|
|
pmutex->m_owner = NULL;
|
|
|
|
pmutex->m_flags = attr->m_flags | MUTEX_FLAGS_INITED;
|
|
|
|
if (private)
|
|
|
|
pmutex->m_flags |= MUTEX_FLAGS_PRIVATE;
|
|
|
|
pmutex->m_count = 0;
|
|
|
|
pmutex->m_refcount = 0;
|
|
|
|
if (attr->m_protocol == PTHREAD_PRIO_PROTECT)
|
|
|
|
pmutex->m_prio = attr->m_ceiling;
|
|
|
|
else
|
|
|
|
pmutex->m_prio = -1;
|
|
|
|
pmutex->m_saved_prio = 0;
|
|
|
|
MUTEX_INIT_LINK(pmutex);
|
|
|
|
*mutex = pmutex;
|
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
init_static(struct pthread *thread, pthread_mutex_t *mutex)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int ret;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
|
2004-01-19 15:00:57 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (*mutex == NULL)
|
|
|
|
ret = mutex_init(mutex, NULL, 0);
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
THR_LOCK_RELEASE(thread, &_mutex_static_lock);
|
|
|
|
|
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int ret;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
if (*mutex == NULL)
|
|
|
|
ret = mutex_init(mutex, NULL, 1);
|
|
|
|
else
|
|
|
|
ret = 0;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
THR_LOCK_RELEASE(thread, &_mutex_static_lock);
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_mutex_init(pthread_mutex_t *mutex,
|
|
|
|
const pthread_mutexattr_t *mutex_attr)
|
|
|
|
{
|
|
|
|
return mutex_init(mutex, mutex_attr, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
__pthread_mutex_init(pthread_mutex_t *mutex,
|
|
|
|
const pthread_mutexattr_t *mutex_attr)
|
|
|
|
{
|
|
|
|
return mutex_init(mutex, mutex_attr, 0);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
|
|
|
_mutex_reinit(pthread_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
_thr_umtx_init(&(*mutex)->m_lock);
|
|
|
|
TAILQ_INIT(&(*mutex)->m_queue);
|
|
|
|
MUTEX_INIT_LINK(*mutex);
|
|
|
|
(*mutex)->m_owner = NULL;
|
|
|
|
(*mutex)->m_count = 0;
|
|
|
|
(*mutex)->m_refcount = 0;
|
|
|
|
(*mutex)->m_prio = 0;
|
|
|
|
(*mutex)->m_saved_prio = 0;
|
2004-01-19 15:00:57 +00:00
|
|
|
return (0);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
void
|
|
|
|
_mutex_fork(struct pthread *curthread)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread_mutex *m;
|
|
|
|
|
2006-01-14 11:33:43 +00:00
|
|
|
/*
|
|
|
|
* Fix mutex ownership for child process.
|
|
|
|
* note that process shared mutex should not
|
|
|
|
* be inherited because owner is forking thread
|
|
|
|
* which is in parent process, they should be
|
2006-01-16 05:33:48 +00:00
|
|
|
* removed from the owned mutex list, current,
|
2006-01-14 11:33:43 +00:00
|
|
|
* process shared mutex is not supported, so I
|
|
|
|
* am not worried.
|
|
|
|
*/
|
2006-03-27 23:50:21 +00:00
|
|
|
TAILQ_FOREACH(m, &curthread->mutexq, m_qe)
|
2005-04-02 01:20:00 +00:00
|
|
|
m->m_lock = (umtx_t)curthread->tid;
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
|
|
|
_pthread_mutex_destroy(pthread_mutex_t *mutex)
|
2004-02-18 15:16:31 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
pthread_mutex_t m;
|
|
|
|
int ret = 0;
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (__predict_false(*mutex == NULL))
|
2005-04-02 01:20:00 +00:00
|
|
|
ret = EINVAL;
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* Try to lock the mutex structure, we only need to
|
|
|
|
* try once, if failed, the mutex is in used.
|
|
|
|
*/
|
|
|
|
ret = THR_UMTX_TRYLOCK(curthread, &(*mutex)->m_lock);
|
|
|
|
if (ret)
|
|
|
|
return (ret);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check mutex other fields to see if this mutex is
|
|
|
|
* in use. Mostly for prority mutex types, or there
|
|
|
|
* are condition variables referencing it.
|
|
|
|
*/
|
|
|
|
if (((*mutex)->m_owner != NULL) ||
|
|
|
|
(TAILQ_FIRST(&(*mutex)->m_queue) != NULL) ||
|
|
|
|
((*mutex)->m_refcount != 0)) {
|
|
|
|
THR_UMTX_UNLOCK(curthread, &(*mutex)->m_lock);
|
|
|
|
ret = EBUSY;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Save a pointer to the mutex so it can be free'd
|
2006-03-27 23:50:21 +00:00
|
|
|
* and set the caller's pointer to NULL.
|
2005-04-02 01:20:00 +00:00
|
|
|
*/
|
|
|
|
m = *mutex;
|
|
|
|
*mutex = NULL;
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
THR_UMTX_UNLOCK(curthread, &m->m_lock);
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
MUTEX_ASSERT_NOT_OWNED(m);
|
2006-03-27 23:50:21 +00:00
|
|
|
free(m);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
2004-02-18 15:16:31 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex)
|
2004-02-18 15:16:31 +00:00
|
|
|
{
|
2006-03-27 23:50:21 +00:00
|
|
|
struct pthread_mutex *m;
|
|
|
|
int ret;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
m = *mutex;
|
|
|
|
ret = THR_UMTX_TRYLOCK(curthread, &m->m_lock);
|
|
|
|
if (ret == 0) {
|
|
|
|
m->m_owner = curthread;
|
|
|
|
/* Add to the list of owned mutexes. */
|
|
|
|
MUTEX_ASSERT_NOT_OWNED(m);
|
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
|
|
|
|
} else if (m->m_owner == curthread) {
|
|
|
|
ret = mutex_self_trylock(curthread, m);
|
|
|
|
} /* else {} */
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
return (ret);
|
2004-02-18 15:16:31 +00:00
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
|
|
|
__pthread_mutex_trylock(pthread_mutex_t *mutex)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
int ret = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization:
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*mutex != NULL) ||
|
|
|
|
((ret = init_static(curthread, mutex)) == 0))
|
|
|
|
ret = mutex_trylock_common(curthread, mutex);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_pthread_mutex_trylock(pthread_mutex_t *mutex)
|
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
2003-04-01 22:39:31 +00:00
|
|
|
int ret = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization marking the mutex private (delete safe):
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*mutex != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, mutex)) == 0))
|
|
|
|
ret = mutex_trylock_common(curthread, mutex);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-03-27 23:50:21 +00:00
|
|
|
mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
|
2005-04-02 01:20:00 +00:00
|
|
|
const struct timespec * abstime)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct timespec ts, ts2;
|
2006-03-27 23:50:21 +00:00
|
|
|
struct pthread_mutex *m;
|
2005-04-02 01:20:00 +00:00
|
|
|
int ret = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
m = *mutex;
|
|
|
|
ret = THR_UMTX_TRYLOCK(curthread, &m->m_lock);
|
|
|
|
if (ret == 0) {
|
|
|
|
m->m_owner = curthread;
|
|
|
|
/* Add to the list of owned mutexes: */
|
|
|
|
MUTEX_ASSERT_NOT_OWNED(m);
|
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
|
|
|
|
} else if (m->m_owner == curthread) {
|
|
|
|
ret = mutex_self_lock(curthread, m, abstime);
|
|
|
|
} else {
|
|
|
|
if (abstime == NULL) {
|
|
|
|
THR_UMTX_LOCK(curthread, &m->m_lock);
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
TIMESPEC_SUB(&ts2, abstime, &ts);
|
|
|
|
ret = THR_UMTX_TIMEDLOCK(curthread, &m->m_lock, &ts2);
|
|
|
|
/*
|
|
|
|
* Timed out wait is not restarted if
|
|
|
|
* it was interrupted, not worth to do it.
|
|
|
|
*/
|
|
|
|
if (ret == EINTR)
|
|
|
|
ret = ETIMEDOUT;
|
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
if (ret == 0) {
|
2006-03-27 23:50:21 +00:00
|
|
|
m->m_owner = curthread;
|
2005-04-02 01:20:00 +00:00
|
|
|
/* Add to the list of owned mutexes: */
|
2006-03-27 23:50:21 +00:00
|
|
|
MUTEX_ASSERT_NOT_OWNED(m);
|
|
|
|
TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe);
|
2005-04-02 01:20:00 +00:00
|
|
|
}
|
2004-02-18 15:16:31 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
2004-02-18 15:16:31 +00:00
|
|
|
}
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
int
|
|
|
|
__pthread_mutex_lock(pthread_mutex_t *m)
|
2004-02-18 15:16:31 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread;
|
|
|
|
int ret = 0;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_check_init();
|
|
|
|
|
|
|
|
curthread = _get_curthread();
|
2004-02-18 15:16:31 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization:
|
2004-02-18 15:16:31 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
|
|
|
|
ret = mutex_lock_common(curthread, m, NULL);
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_pthread_mutex_lock(pthread_mutex_t *m)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread;
|
2003-04-01 22:39:31 +00:00
|
|
|
int ret = 0;
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_check_init();
|
|
|
|
|
|
|
|
curthread = _get_curthread();
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
2005-04-02 01:20:00 +00:00
|
|
|
* initialization marking it private (delete safe):
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*m != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, m)) == 0))
|
|
|
|
ret = mutex_lock_common(curthread, m, NULL);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-03-27 23:50:21 +00:00
|
|
|
__pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread;
|
2003-04-01 03:46:29 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
_thr_check_init();
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (abstime != NULL && (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
|
|
|
|
abstime->tv_nsec >= 1000000000))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
curthread = _get_curthread();
|
2003-04-01 03:46:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
2005-04-02 01:20:00 +00:00
|
|
|
* initialization:
|
2003-04-01 03:46:29 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*m != NULL) || ((ret = init_static(curthread, m)) == 0))
|
2006-03-27 23:50:21 +00:00
|
|
|
ret = mutex_lock_common(curthread, m, abstime);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2003-12-30 08:44:55 +00:00
|
|
|
int
|
2006-03-27 23:50:21 +00:00
|
|
|
_pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
|
2003-12-30 08:44:55 +00:00
|
|
|
{
|
2006-03-27 23:50:21 +00:00
|
|
|
struct pthread *curthread;
|
2005-04-02 01:20:00 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
_thr_check_init();
|
2003-12-30 08:44:55 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (abstime != NULL && (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
|
|
|
|
abstime->tv_nsec >= 1000000000))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
curthread = _get_curthread();
|
2003-12-30 08:44:55 +00:00
|
|
|
|
|
|
|
/*
|
2005-04-02 01:20:00 +00:00
|
|
|
* If the mutex is statically initialized, perform the dynamic
|
|
|
|
* initialization marking it private (delete safe):
|
2003-12-30 08:44:55 +00:00
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
if ((*m != NULL) ||
|
|
|
|
((ret = init_static_private(curthread, m)) == 0))
|
2006-03-27 23:50:21 +00:00
|
|
|
ret = mutex_lock_common(curthread, m, abstime);
|
2003-12-30 08:44:55 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
return (ret);
|
2003-12-30 08:44:55 +00:00
|
|
|
}
|
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_pthread_mutex_unlock(pthread_mutex_t *m)
|
2003-04-01 22:39:31 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
return (mutex_unlock_common(m, /* add reference */ 0));
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-07-02 02:05:23 +00:00
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_mutex_cv_unlock(pthread_mutex_t *m)
|
2003-07-02 02:05:23 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
return (mutex_unlock_common(m, /* add reference */ 1));
|
2003-07-02 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
2003-04-01 03:46:29 +00:00
|
|
|
int
|
2005-04-02 01:20:00 +00:00
|
|
|
_mutex_cv_lock(pthread_mutex_t *m)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
int ret;
|
|
|
|
|
2005-12-21 05:14:07 +00:00
|
|
|
ret = mutex_lock_common(_get_curthread(), m, NULL);
|
|
|
|
if (ret == 0)
|
2005-04-02 01:20:00 +00:00
|
|
|
(*m)->m_refcount--;
|
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
mutex_self_trylock(struct pthread *curthread, pthread_mutex_t m)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
switch (m->m_type) {
|
|
|
|
case PTHREAD_MUTEX_ERRORCHECK:
|
|
|
|
case PTHREAD_MUTEX_NORMAL:
|
|
|
|
ret = EBUSY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_MUTEX_RECURSIVE:
|
|
|
|
/* Increment the lock count: */
|
|
|
|
if (m->m_count + 1 > 0) {
|
|
|
|
m->m_count++;
|
|
|
|
ret = 0;
|
|
|
|
} else
|
|
|
|
ret = EAGAIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trap invalid mutex types; */
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
|
|
|
mutex_self_lock(struct pthread *curthread, pthread_mutex_t m,
|
|
|
|
const struct timespec *abstime)
|
2003-04-01 22:39:31 +00:00
|
|
|
{
|
2006-03-27 23:50:21 +00:00
|
|
|
struct timespec ts1, ts2;
|
|
|
|
int ret;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
switch (m->m_type) {
|
2003-04-01 22:39:31 +00:00
|
|
|
case PTHREAD_MUTEX_ERRORCHECK:
|
2005-04-02 01:20:00 +00:00
|
|
|
if (abstime) {
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts1);
|
|
|
|
TIMESPEC_SUB(&ts2, abstime, &ts1);
|
|
|
|
__sys_nanosleep(&ts2, NULL);
|
|
|
|
ret = ETIMEDOUT;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* POSIX specifies that mutexes should return
|
|
|
|
* EDEADLK if a recursive lock is detected.
|
|
|
|
*/
|
|
|
|
ret = EDEADLK;
|
|
|
|
}
|
2003-04-01 22:39:31 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_MUTEX_NORMAL:
|
|
|
|
/*
|
|
|
|
* What SS2 define as a 'normal' mutex. Intentionally
|
|
|
|
* deadlock on attempts to get a lock you already own.
|
|
|
|
*/
|
2005-04-02 01:20:00 +00:00
|
|
|
ret = 0;
|
|
|
|
if (abstime) {
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts1);
|
|
|
|
TIMESPEC_SUB(&ts2, abstime, &ts1);
|
|
|
|
__sys_nanosleep(&ts2, NULL);
|
|
|
|
ret = ETIMEDOUT;
|
|
|
|
} else {
|
|
|
|
ts1.tv_sec = 30;
|
|
|
|
ts1.tv_nsec = 0;
|
|
|
|
for (;;)
|
|
|
|
__sys_nanosleep(&ts1, NULL);
|
|
|
|
}
|
2003-04-01 22:39:31 +00:00
|
|
|
break;
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
case PTHREAD_MUTEX_RECURSIVE:
|
|
|
|
/* Increment the lock count: */
|
|
|
|
if (m->m_count + 1 > 0) {
|
|
|
|
m->m_count++;
|
|
|
|
ret = 0;
|
|
|
|
} else
|
|
|
|
ret = EAGAIN;
|
2004-05-20 11:55:04 +00:00
|
|
|
break;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Trap invalid mutex types; */
|
|
|
|
ret = EINVAL;
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
2005-04-02 01:20:00 +00:00
|
|
|
|
|
|
|
return (ret);
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 01:20:00 +00:00
|
|
|
static int
|
2006-03-27 23:50:21 +00:00
|
|
|
mutex_unlock_common(pthread_mutex_t *mutex, int add_reference)
|
2003-04-01 03:46:29 +00:00
|
|
|
{
|
2005-04-02 01:20:00 +00:00
|
|
|
struct pthread *curthread = _get_curthread();
|
|
|
|
struct pthread_mutex *m;
|
2006-03-27 23:50:21 +00:00
|
|
|
int ret = 0;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (__predict_false((m = *mutex) == NULL))
|
|
|
|
return (EINVAL);
|
2003-04-01 03:46:29 +00:00
|
|
|
|
2003-04-01 22:39:31 +00:00
|
|
|
/*
|
2006-03-27 23:50:21 +00:00
|
|
|
* Check if the running thread is not the owner of the mutex.
|
2003-04-01 22:39:31 +00:00
|
|
|
*/
|
2006-03-27 23:50:21 +00:00
|
|
|
if (__predict_false(m->m_owner != curthread)) {
|
|
|
|
ret = EPERM;
|
|
|
|
} else if (__predict_false(
|
|
|
|
m->m_type == PTHREAD_MUTEX_RECURSIVE &&
|
|
|
|
m->m_count > 0)) {
|
|
|
|
m->m_count--;
|
|
|
|
if (add_reference)
|
|
|
|
m->m_refcount++;
|
2005-04-02 01:20:00 +00:00
|
|
|
} else {
|
|
|
|
/*
|
2006-03-27 23:50:21 +00:00
|
|
|
* Clear the count in case this is a recursive mutex.
|
2005-04-02 01:20:00 +00:00
|
|
|
*/
|
2006-03-27 23:50:21 +00:00
|
|
|
m->m_count = 0;
|
|
|
|
m->m_owner = NULL;
|
|
|
|
/* Remove the mutex from the threads queue. */
|
|
|
|
MUTEX_ASSERT_IS_OWNED(m);
|
|
|
|
TAILQ_REMOVE(&curthread->mutexq, m, m_qe);
|
|
|
|
MUTEX_INIT_LINK(m);
|
|
|
|
if (add_reference)
|
|
|
|
m->m_refcount++;
|
|
|
|
THR_UMTX_UNLOCK(curthread, &m->m_lock);
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
2006-03-27 23:50:21 +00:00
|
|
|
return (ret);
|
2003-04-01 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_mutex_unlock_private(pthread_t pthread)
|
|
|
|
{
|
|
|
|
struct pthread_mutex *m, *m_next;
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
for (m = TAILQ_FIRST(&pthread->mutexq); m != NULL; m = m_next) {
|
2003-04-01 03:46:29 +00:00
|
|
|
m_next = TAILQ_NEXT(m, m_qe);
|
2003-04-01 22:39:31 +00:00
|
|
|
if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
|
2006-03-27 23:50:21 +00:00
|
|
|
_pthread_mutex_unlock(&m);
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
int
|
|
|
|
_pthread_mutex_getprioceiling(pthread_mutex_t *mutex,
|
|
|
|
int *prioceiling)
|
2003-04-01 22:39:31 +00:00
|
|
|
{
|
2006-03-27 23:50:21 +00:00
|
|
|
int ret;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (*mutex == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
else if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT)
|
|
|
|
ret = EINVAL;
|
|
|
|
else
|
|
|
|
ret = (*mutex)->m_prio;
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
return(ret);
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
int
|
|
|
|
_pthread_mutex_setprioceiling(pthread_mutex_t *mutex,
|
|
|
|
int prioceiling, int *old_ceiling)
|
2003-04-01 22:39:31 +00:00
|
|
|
{
|
2006-03-27 23:50:21 +00:00
|
|
|
int ret = 0;
|
|
|
|
int tmp;
|
2003-04-01 22:39:31 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
if (*mutex == NULL)
|
|
|
|
ret = EINVAL;
|
|
|
|
else if ((*mutex)->m_protocol != PTHREAD_PRIO_PROTECT)
|
|
|
|
ret = EINVAL;
|
|
|
|
else if ((ret = pthread_mutex_lock(mutex)) == 0) {
|
|
|
|
tmp = (*mutex)->m_prio;
|
|
|
|
(*mutex)->m_prio = prioceiling;
|
|
|
|
ret = pthread_mutex_unlock(mutex);
|
2005-04-02 01:20:00 +00:00
|
|
|
|
2006-03-27 23:50:21 +00:00
|
|
|
/* Return the old ceiling. */
|
|
|
|
*old_ceiling = tmp;
|
2003-04-01 22:39:31 +00:00
|
|
|
}
|
2006-03-27 23:50:21 +00:00
|
|
|
return(ret);
|
2003-05-12 10:34:01 +00:00
|
|
|
}
|