1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-13 10:02:38 +00:00

- Reduce function call overhead for uncontended case.

- Remove unused flags MUTEX_FLAGS_* and their code.
- Check validity of the timeout parameter in mutex_self_lock().
This commit is contained in:
David Xu 2008-05-29 07:57:33 +00:00
parent 9dea35a10a
commit 850f4d66cb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=179411
3 changed files with 106 additions and 219 deletions

View File

@ -89,8 +89,7 @@ struct pthread_attr _pthread_attr_default = {
struct pthread_mutex_attr _pthread_mutexattr_default = { struct pthread_mutex_attr _pthread_mutexattr_default = {
.m_type = PTHREAD_MUTEX_DEFAULT, .m_type = PTHREAD_MUTEX_DEFAULT,
.m_protocol = PTHREAD_PRIO_NONE, .m_protocol = PTHREAD_PRIO_NONE,
.m_ceiling = 0, .m_ceiling = 0
.m_flags = 0
}; };
/* Default condition variable attributes: */ /* Default condition variable attributes: */

View File

@ -51,12 +51,12 @@
(m)->m_qe.tqe_next = NULL; \ (m)->m_qe.tqe_next = NULL; \
} while (0) } while (0)
#define MUTEX_ASSERT_IS_OWNED(m) do { \ #define MUTEX_ASSERT_IS_OWNED(m) do { \
if ((m)->m_qe.tqe_prev == NULL) \ if (__predict_false((m)->m_qe.tqe_prev == NULL))\
PANIC("mutex is not on list"); \ PANIC("mutex is not on list"); \
} while (0) } while (0)
#define MUTEX_ASSERT_NOT_OWNED(m) do { \ #define MUTEX_ASSERT_NOT_OWNED(m) do { \
if (((m)->m_qe.tqe_prev != NULL) || \ if (__predict_false((m)->m_qe.tqe_prev != NULL || \
((m)->m_qe.tqe_next != NULL)) \ (m)->m_qe.tqe_next != NULL)) \
PANIC("mutex is on list"); \ PANIC("mutex is on list"); \
} while (0) } while (0)
#else #else
@ -95,9 +95,13 @@ static int mutex_self_lock(pthread_mutex_t,
static int mutex_unlock_common(pthread_mutex_t *); static int mutex_unlock_common(pthread_mutex_t *);
__weak_reference(__pthread_mutex_init, pthread_mutex_init); __weak_reference(__pthread_mutex_init, pthread_mutex_init);
__strong_reference(__pthread_mutex_init, _pthread_mutex_init);
__weak_reference(__pthread_mutex_lock, pthread_mutex_lock); __weak_reference(__pthread_mutex_lock, pthread_mutex_lock);
__strong_reference(__pthread_mutex_lock, _pthread_mutex_lock);
__weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock); __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
__strong_reference(__pthread_mutex_timedlock, _pthread_mutex_timedlock);
__weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock); __weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock);
__strong_reference(__pthread_mutex_trylock, _pthread_mutex_trylock);
/* Single underscore versions provided for libc internal usage: */ /* Single underscore versions provided for libc internal usage: */
/* No difference between libc and application usage of these: */ /* No difference between libc and application usage of these: */
@ -108,15 +112,17 @@ __weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling);
__weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling); __weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling);
__weak_reference(__pthread_mutex_setspinloops_np, pthread_mutex_setspinloops_np); __weak_reference(__pthread_mutex_setspinloops_np, pthread_mutex_setspinloops_np);
__strong_reference(__pthread_mutex_setspinloops_np, _pthread_mutex_setspinloops_np);
__weak_reference(_pthread_mutex_getspinloops_np, pthread_mutex_getspinloops_np); __weak_reference(_pthread_mutex_getspinloops_np, pthread_mutex_getspinloops_np);
__weak_reference(__pthread_mutex_setyieldloops_np, pthread_mutex_setyieldloops_np); __weak_reference(__pthread_mutex_setyieldloops_np, pthread_mutex_setyieldloops_np);
__strong_reference(__pthread_mutex_setyieldloops_np, _pthread_mutex_setyieldloops_np);
__weak_reference(_pthread_mutex_getyieldloops_np, pthread_mutex_getyieldloops_np); __weak_reference(_pthread_mutex_getyieldloops_np, pthread_mutex_getyieldloops_np);
__weak_reference(_pthread_mutex_isowned_np, pthread_mutex_isowned_np); __weak_reference(_pthread_mutex_isowned_np, pthread_mutex_isowned_np);
static int static int
mutex_init(pthread_mutex_t *mutex, mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutex_attr, int private, const pthread_mutexattr_t *mutex_attr,
void *(calloc_cb)(size_t, size_t)) void *(calloc_cb)(size_t, size_t))
{ {
const struct pthread_mutex_attr *attr; const struct pthread_mutex_attr *attr;
@ -139,9 +145,6 @@ mutex_init(pthread_mutex_t *mutex,
pmutex->m_type = attr->m_type; pmutex->m_type = attr->m_type;
pmutex->m_owner = NULL; 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_count = 0;
pmutex->m_refcount = 0; pmutex->m_refcount = 0;
pmutex->m_spinloops = 0; pmutex->m_spinloops = 0;
@ -180,24 +183,7 @@ init_static(struct pthread *thread, pthread_mutex_t *mutex)
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
if (*mutex == NULL) if (*mutex == NULL)
ret = mutex_init(mutex, NULL, 0, calloc); ret = mutex_init(mutex, NULL, calloc);
else
ret = 0;
THR_LOCK_RELEASE(thread, &_mutex_static_lock);
return (ret);
}
static int
init_static_private(struct pthread *thread, pthread_mutex_t *mutex)
{
int ret;
THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
if (*mutex == NULL)
ret = mutex_init(mutex, NULL, 1, calloc);
else else
ret = 0; ret = 0;
@ -218,18 +204,11 @@ set_inherited_priority(struct pthread *curthread, struct pthread_mutex *m)
m->m_lock.m_ceilings[1] = -1; m->m_lock.m_ceilings[1] = -1;
} }
int
_pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutex_attr)
{
return mutex_init(mutex, mutex_attr, 1, calloc);
}
int int
__pthread_mutex_init(pthread_mutex_t *mutex, __pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutex_attr) const pthread_mutexattr_t *mutex_attr)
{ {
return mutex_init(mutex, mutex_attr, 0, calloc); return mutex_init(mutex, mutex_attr, calloc);
} }
/* This function is used internally by malloc. */ /* This function is used internally by malloc. */
@ -240,12 +219,11 @@ _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
static const struct pthread_mutex_attr attr = { static const struct pthread_mutex_attr attr = {
.m_type = PTHREAD_MUTEX_NORMAL, .m_type = PTHREAD_MUTEX_NORMAL,
.m_protocol = PTHREAD_PRIO_NONE, .m_protocol = PTHREAD_PRIO_NONE,
.m_ceiling = 0, .m_ceiling = 0
.m_flags = 0
}; };
static const struct pthread_mutex_attr *pattr = &attr; static const struct pthread_mutex_attr *pattr = &attr;
return mutex_init(mutex, (pthread_mutexattr_t *)&pattr, 0, calloc_cb); return mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb);
} }
void void
@ -319,7 +297,6 @@ _pthread_mutex_destroy(pthread_mutex_t *mutex)
return (ret); return (ret);
} }
#define ENQUEUE_MUTEX(curthread, m) \ #define ENQUEUE_MUTEX(curthread, m) \
do { \ do { \
(m)->m_owner = curthread; \ (m)->m_owner = curthread; \
@ -368,42 +345,19 @@ __pthread_mutex_trylock(pthread_mutex_t *mutex)
return (mutex_trylock_common(curthread, mutex)); return (mutex_trylock_common(curthread, mutex));
} }
int
_pthread_mutex_trylock(pthread_mutex_t *mutex)
{
struct pthread *curthread = _get_curthread();
int ret;
/*
* If the mutex is statically initialized, perform the dynamic
* initialization marking the mutex private (delete safe):
*/
if (__predict_false(*mutex == NULL)) {
ret = init_static_private(curthread, mutex);
if (__predict_false(ret))
return (ret);
}
return (mutex_trylock_common(curthread, mutex));
}
static int static int
mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex, mutex_lock_sleep(struct pthread *curthread, pthread_mutex_t m,
const struct timespec * abstime) const struct timespec * abstime)
{ {
struct timespec ts, ts2; struct timespec ts, ts2;
struct pthread_mutex *m;
uint32_t id; uint32_t id;
int ret; int ret;
int count; int count;
id = TID(curthread); id = TID(curthread);
m = *mutex; if (__predict_false(m->m_owner == curthread))
ret = _thr_umutex_trylock2(&m->m_lock, id); return mutex_self_lock(m, abstime);
if (ret == 0) {
ENQUEUE_MUTEX(curthread, m);
} else if (m->m_owner == curthread) {
ret = mutex_self_lock(m, abstime);
} else {
/* /*
* For adaptive mutexes, spin for a bit in the expectation * For adaptive mutexes, spin for a bit in the expectation
* that if the application requests this mutex type then * that if the application requests this mutex type then
@ -456,14 +410,30 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
done: done:
if (ret == 0) if (ret == 0)
ENQUEUE_MUTEX(curthread, m); ENQUEUE_MUTEX(curthread, m);
return (ret);
} }
static inline int
mutex_lock_common(struct pthread *curthread, struct pthread_mutex *m,
const struct timespec * abstime)
{
uint32_t id;
int ret;
id = TID(curthread);
ret = _thr_umutex_trylock2(&m->m_lock, id);
if (ret == 0)
ENQUEUE_MUTEX(curthread, m);
else
ret = mutex_lock_sleep(curthread, m, abstime);
return (ret); return (ret);
} }
int int
__pthread_mutex_lock(pthread_mutex_t *m) __pthread_mutex_lock(pthread_mutex_t *mutex)
{ {
struct pthread *curthread; struct pthread *curthread;
struct pthread_mutex *m;
int ret; int ret;
_thr_check_init(); _thr_check_init();
@ -474,40 +444,20 @@ __pthread_mutex_lock(pthread_mutex_t *m)
* If the mutex is statically initialized, perform the dynamic * If the mutex is statically initialized, perform the dynamic
* initialization: * initialization:
*/ */
if (__predict_false(*m == NULL)) { if (__predict_false((m = *mutex) == NULL)) {
ret = init_static(curthread, m); ret = init_static(curthread, mutex);
if (__predict_false(ret)) if (__predict_false(ret))
return (ret); return (ret);
m = *mutex;
} }
return (mutex_lock_common(curthread, m, NULL)); return (mutex_lock_common(curthread, m, NULL));
} }
int int
_pthread_mutex_lock(pthread_mutex_t *m) __pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
{
struct pthread *curthread;
int ret;
_thr_check_init();
curthread = _get_curthread();
/*
* If the mutex is statically initialized, perform the dynamic
* initialization marking it private (delete safe):
*/
if (__predict_false(*m == NULL)) {
ret = init_static_private(curthread, m);
if (__predict_false(ret))
return (ret);
}
return (mutex_lock_common(curthread, m, NULL));
}
int
__pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
{ {
struct pthread *curthread; struct pthread *curthread;
struct pthread_mutex *m;
int ret; int ret;
_thr_check_init(); _thr_check_init();
@ -518,32 +468,11 @@ __pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
* If the mutex is statically initialized, perform the dynamic * If the mutex is statically initialized, perform the dynamic
* initialization: * initialization:
*/ */
if (__predict_false(*m == NULL)) { if (__predict_false((m = *mutex) == NULL)) {
ret = init_static(curthread, m); ret = init_static(curthread, mutex);
if (__predict_false(ret))
return (ret);
}
return (mutex_lock_common(curthread, m, abstime));
}
int
_pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime)
{
struct pthread *curthread;
int ret;
_thr_check_init();
curthread = _get_curthread();
/*
* If the mutex is statically initialized, perform the dynamic
* initialization marking it private (delete safe):
*/
if (__predict_false(*m == NULL)) {
ret = init_static_private(curthread, m);
if (__predict_false(ret)) if (__predict_false(ret))
return (ret); return (ret);
m = *mutex;
} }
return (mutex_lock_common(curthread, m, abstime)); return (mutex_lock_common(curthread, m, abstime));
} }
@ -559,7 +488,7 @@ _mutex_cv_lock(pthread_mutex_t *m, int count)
{ {
int ret; int ret;
ret = mutex_lock_common(_get_curthread(), m, NULL); ret = mutex_lock_common(_get_curthread(), *m, NULL);
if (ret == 0) { if (ret == 0) {
(*m)->m_refcount--; (*m)->m_refcount--;
(*m)->m_count += count; (*m)->m_count += count;
@ -605,10 +534,15 @@ mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
case PTHREAD_MUTEX_ERRORCHECK: case PTHREAD_MUTEX_ERRORCHECK:
case PTHREAD_MUTEX_ADAPTIVE_NP: case PTHREAD_MUTEX_ADAPTIVE_NP:
if (abstime) { if (abstime) {
if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
abstime->tv_nsec >= 1000000000) {
ret = EINVAL;
} else {
clock_gettime(CLOCK_REALTIME, &ts1); clock_gettime(CLOCK_REALTIME, &ts1);
TIMESPEC_SUB(&ts2, abstime, &ts1); TIMESPEC_SUB(&ts2, abstime, &ts1);
__sys_nanosleep(&ts2, NULL); __sys_nanosleep(&ts2, NULL);
ret = ETIMEDOUT; ret = ETIMEDOUT;
}
} else { } else {
/* /*
* POSIX specifies that mutexes should return * POSIX specifies that mutexes should return
@ -625,10 +559,15 @@ mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
*/ */
ret = 0; ret = 0;
if (abstime) { if (abstime) {
if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
abstime->tv_nsec >= 1000000000) {
ret = EINVAL;
} else {
clock_gettime(CLOCK_REALTIME, &ts1); clock_gettime(CLOCK_REALTIME, &ts1);
TIMESPEC_SUB(&ts2, abstime, &ts1); TIMESPEC_SUB(&ts2, abstime, &ts1);
__sys_nanosleep(&ts2, NULL); __sys_nanosleep(&ts2, NULL);
ret = ETIMEDOUT; ret = ETIMEDOUT;
}
} else { } else {
ts1.tv_sec = 30; ts1.tv_sec = 30;
ts1.tv_nsec = 0; ts1.tv_nsec = 0;
@ -726,17 +665,6 @@ _mutex_cv_unlock(pthread_mutex_t *mutex, int *count)
return (0); return (0);
} }
void
_mutex_unlock_private(pthread_t pthread)
{
struct pthread_mutex *m, *m_next;
TAILQ_FOREACH_SAFE(m, &pthread->mutexq, m_qe, m_next) {
if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0)
_pthread_mutex_unlock(&m);
}
}
int int
_pthread_mutex_getprioceiling(pthread_mutex_t *mutex, _pthread_mutex_getprioceiling(pthread_mutex_t *mutex,
int *prioceiling) int *prioceiling)
@ -799,21 +727,6 @@ _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count)
return (0); return (0);
} }
int
_pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count)
{
struct pthread *curthread = _get_curthread();
int ret;
if (__predict_false(*mutex == NULL)) {
ret = init_static_private(curthread, mutex);
if (__predict_false(ret))
return (ret);
}
(*mutex)->m_spinloops = count;
return (0);
}
int int
__pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count) __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count)
{ {
@ -838,21 +751,6 @@ _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count)
return (0); return (0);
} }
int
_pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count)
{
struct pthread *curthread = _get_curthread();
int ret;
if (__predict_false(*mutex == NULL)) {
ret = init_static_private(curthread, mutex);
if (__predict_false(ret))
return (ret);
}
(*mutex)->m_yieldloops = count;
return (0);
}
int int
__pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count) __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count)
{ {

View File

@ -118,7 +118,6 @@ struct pthread_mutex {
struct umutex m_lock; struct umutex m_lock;
enum pthread_mutextype m_type; enum pthread_mutextype m_type;
struct pthread *m_owner; struct pthread *m_owner;
int m_flags;
int m_count; int m_count;
int m_refcount; int m_refcount;
int m_spinloops; int m_spinloops;
@ -129,18 +128,10 @@ struct pthread_mutex {
TAILQ_ENTRY(pthread_mutex) m_qe; TAILQ_ENTRY(pthread_mutex) m_qe;
}; };
/*
* Flags for mutexes.
*/
#define MUTEX_FLAGS_PRIVATE 0x01
#define MUTEX_FLAGS_INITED 0x02
#define MUTEX_FLAGS_BUSY 0x04
struct pthread_mutex_attr { struct pthread_mutex_attr {
enum pthread_mutextype m_type; enum pthread_mutextype m_type;
int m_protocol; int m_protocol;
int m_ceiling; int m_ceiling;
int m_flags;
}; };
#define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \ #define PTHREAD_MUTEXATTR_STATIC_INITIALIZER \
@ -619,7 +610,6 @@ int _mutex_cv_lock(pthread_mutex_t *, int count) __hidden;
int _mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden; int _mutex_cv_unlock(pthread_mutex_t *, int *count) __hidden;
int _mutex_reinit(pthread_mutex_t *) __hidden; int _mutex_reinit(pthread_mutex_t *) __hidden;
void _mutex_fork(struct pthread *curthread) __hidden; void _mutex_fork(struct pthread *curthread) __hidden;
void _mutex_unlock_private(struct pthread *) __hidden;
void _libpthread_init(struct pthread *) __hidden; void _libpthread_init(struct pthread *) __hidden;
struct pthread *_thr_alloc(struct pthread *) __hidden; struct pthread *_thr_alloc(struct pthread *) __hidden;
void _thread_exit(const char *, int, const char *) __hidden __dead2; void _thread_exit(const char *, int, const char *) __hidden __dead2;