Create inline function _thr_umutex_trylock2 to only try one atomic

operation, if it is failed, we call syscall directly, this saves
one atomic operation per lock contention.
This commit is contained in:
David Xu 2006-12-14 13:22:02 +00:00
parent 95e8481bac
commit 8a8178c010
2 changed files with 11 additions and 3 deletions

View File

@ -343,7 +343,7 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
id = TID(curthread);
m = *mutex;
ret = _thr_umutex_trylock(&m->m_lock, id);
ret = _thr_umutex_trylock2(&m->m_lock, id);
if (ret == 0) {
m->m_owner = curthread;
/* Add to the list of owned mutexes: */
@ -356,7 +356,7 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
ret = mutex_self_lock(m, abstime);
} else {
if (abstime == NULL) {
ret = _thr_umutex_lock(&m->m_lock, id);
ret = __thr_umutex_lock(&m->m_lock);
} else if (__predict_false(
abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
abstime->tv_nsec >= 1000000000)) {
@ -364,7 +364,7 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
} else {
clock_gettime(CLOCK_REALTIME, &ts);
TIMESPEC_SUB(&ts2, abstime, &ts);
ret = _thr_umutex_timedlock(&m->m_lock, id, &ts2);
ret = __thr_umutex_timedlock(&m->m_lock, &ts2);
/*
* Timed out wait is not restarted if
* it was interrupted, not worth to do it.

View File

@ -64,6 +64,14 @@ _thr_umutex_trylock(struct umutex *mtx, uint32_t id)
return (__thr_umutex_trylock(mtx));
}
static inline int
_thr_umutex_trylock2(struct umutex *mtx, uint32_t id)
{
if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id))
return (0);
return (EBUSY);
}
static inline int
_thr_umutex_lock(struct umutex *mtx, uint32_t id)
{