From: Matthew Dillon Date: Wed, 15 Jul 2009 19:13:13 +0000 (-0700) Subject: MPSAFE - mutexes X-Git-Tag: v2.4.0~432 X-Git-Url: http://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/17386740d7e37fd918f769304323d08cfde748e2 MPSAFE - mutexes * Add additional inline functions to test the lock state. --- diff --git a/sys/sys/mutex2.h b/sys/sys/mutex2.h index af63c25..e7196eb 100644 --- a/sys/sys/mutex2.h +++ b/sys/sys/mutex2.h @@ -229,6 +229,81 @@ mtx_unlock_sh(mtx_t mtx) } /* + * Return TRUE (non-zero) if the mutex is locked shared or exclusive by + * anyone, including the owner. + */ +static __inline int +mtx_islocked(mtx_t mtx) +{ + return(mtx->mtx_lock != 0); +} + +/* + * Return TRUE (non-zero) if the mutex is locked exclusively by anyone, + * including the owner. + * + * The mutex may in an unlocked or shared lock state. + */ +static __inline int +mtx_islocked_ex(mtx_t mtx) +{ + return((mtx->mtx_lock & MTX_EXCLUSIVE) != 0); +} + +/* + * Return TRUE (non-zero) if the mutex is not locked. + */ +static __inline int +mtx_notlocked(mtx_t mtx) +{ + return(mtx->mtx_lock == 0); +} + +/* + * Return TRUE (non-zero) if the mutex is not locked exclusively. + * The mutex may in an unlocked or shared lock state. + */ +static __inline int +mtx_notlocked_ex(mtx_t mtx) +{ + return((mtx->mtx_lock & MTX_EXCLUSIVE) != 0); +} + +/* + * Return TRUE (non-zero) if the mutex is exclusively locked by + * the caller. + */ +static __inline int +mtx_owned(mtx_t mtx) +{ + return((mtx->mtx_lock & MTX_EXCLUSIVE) && mtx->mtx_owner == curthread); +} + +/* + * Return TRUE (non-zero) if the mutex is not exclusively locked by + * the caller. + */ +static __inline int +mtx_notowned(mtx_t mtx) +{ + return((mtx->mtx_lock & MTX_EXCLUSIVE) == 0 || + mtx->mtx_owner != curthread); +} + +/* + * Return the shared or exclusive lock count. A return value of 0 + * indicate that the mutex is not locked. + * + * NOTE: If the mutex is held exclusively by someone other then the + * caller the lock count for the other owner is still returned. + */ +static __inline int +mtx_lockrefs(mtx_t mtx) +{ + return(mtx->mtx_lock & MTX_MASK); +} + +/* * Bump the lock's ref count. This field is independent of the lock. */ static __inline void