diff --git a/include/upipe-pthread/umutex_pthread.h b/include/upipe-pthread/umutex_pthread.h index 6a504a14f..fd92b571c 100644 --- a/include/upipe-pthread/umutex_pthread.h +++ b/include/upipe-pthread/umutex_pthread.h @@ -28,6 +28,13 @@ extern "C" { */ struct umutex *umutex_pthread_alloc(const pthread_mutexattr_t *mutexattr); +/** @This tries to lock a mutex. + * + * @param umutex pointer to a umutex structure + * @return an error code, UBASE_ERR_BUSY for EBUSY + */ +int umutex_pthread_trylock(struct umutex *umutex); + #ifdef __cplusplus } #endif diff --git a/lib/upipe-pthread/umutex_pthread.c b/lib/upipe-pthread/umutex_pthread.c index e60f82370..0e418bfe0 100644 --- a/lib/upipe-pthread/umutex_pthread.c +++ b/lib/upipe-pthread/umutex_pthread.c @@ -51,6 +51,23 @@ static inline int umutex_pthread_lock(struct umutex *umutex) } } +/** @This tries to lock a mutex. + * + * @param umutex pointer to a umutex structure + * @return an error code, UBASE_ERR_BUSY for EBUSY + */ +int umutex_pthread_trylock(struct umutex *umutex) +{ + struct umutex_pthread *umutex_pthread = umutex_pthread_from_umutex(umutex); + int err = pthread_mutex_trylock(&umutex_pthread->mutex); + switch (err) { + case 0: return UBASE_ERR_NONE; + case EBUSY: return UBASE_ERR_BUSY; + default: + case EINVAL: return UBASE_ERR_INVALID; + } +} + /** @This unlocks a mutex. * * @param umutex pointer to a umutex structure