From 631f1669539aaa58b8c49c4d431930f7ab68a633 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sat, 23 Feb 2008 15:41:33 +0200 Subject: [PATCH] Added silc_mutex_trylock --- lib/silcutil/silcmutex.h | 16 ++++++++++++++++ lib/silcutil/unix/silcunixthread.c | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/silcutil/silcmutex.h b/lib/silcutil/silcmutex.h index 878d37de..2ff79f39 100644 --- a/lib/silcutil/silcmutex.h +++ b/lib/silcutil/silcmutex.h @@ -133,6 +133,22 @@ void silc_mutex_lock(SilcMutex mutex); ***/ void silc_mutex_unlock(SilcMutex mutex); +/****f* silcutil/silc_mutex_trylock + * + * SYNOPSIS + * + * SilcBool silc_mutex_trylock(SilcMutex mutex); + * + * DESCRIPTION + * + * Attempts to lock the `mutex'. Returns TRUE if the caller was able + * to acquire the lock and FALSE if the mutex is already locked. If the + * mutex is already locked the caller cannot acquire the lock at this + * time. + * + ***/ +SilcBool silc_mutex_trylock(SilcMutex mutex); + /****f* silcutil/silc_mutex_assert_locked * * SYNOPSIS diff --git a/lib/silcutil/unix/silcunixthread.c b/lib/silcutil/unix/silcunixthread.c index 286a26db..fd30cf3d 100644 --- a/lib/silcutil/unix/silcunixthread.c +++ b/lib/silcutil/unix/silcunixthread.c @@ -200,6 +200,19 @@ void silc_mutex_unlock(SilcMutex mutex) #endif /* SILC_THREADS */ } +SilcBool silc_mutex_trylock(SilcMutex mutex) +{ +#ifdef SILC_THREADS + if (mutex) { + if (pthread_mutex_trylock(&mutex->mutex) == 0) { + mutex->locked = TRUE; + return TRUE; + } + } +#endif /* SILC_THREADS */ + return FALSE; +} + void silc_mutex_assert_locked(SilcMutex mutex) { #ifdef SILC_THREADS -- 2.24.0