From a894395e0be871e9919cb2f9070e2baa2258cb47 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Mon, 6 Nov 2006 16:28:25 +0000 Subject: [PATCH] Added silc_mutex_assert_locked. --- lib/silcutil/silcmutex.h | 18 ++++++++++++++++-- lib/silcutil/unix/silcunixthread.c | 18 ++++++++++++++---- lib/silcutil/win32/silcwin32thread.c | 16 +++++++++++----- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/silcutil/silcmutex.h b/lib/silcutil/silcmutex.h index 52407855..37f08add 100644 --- a/lib/silcutil/silcmutex.h +++ b/lib/silcutil/silcmutex.h @@ -112,10 +112,24 @@ void silc_mutex_lock(SilcMutex mutex); * NOTES * * The caller must not call the silc_mutex_unlock for an unlocked - * mutex or mutex not locked by the current thread. It is fatal - * error if this occurs. + * mutex or mutex not locked by the current thread. * ***/ void silc_mutex_unlock(SilcMutex mutex); +/****f* silcutil/SilcMutexAPI/silc_mutex_assert_locked + * + * SYNOPSIS + * + * void silc_mutex_assert_locked(SilcMutex mutex); + * + * DESCRIPTION + * + * Asserts that the `mutex' is locked. It is fatal error if the mutex + * is not locked. If debugging is not compiled in this function has + * no effect (SILC_DEBUG define). + * + ***/ +void silc_mutex_assert_locked(SilcMutex mutex); + #endif diff --git a/lib/silcutil/unix/silcunixthread.c b/lib/silcutil/unix/silcunixthread.c index 487faf05..3985a617 100644 --- a/lib/silcutil/unix/silcunixthread.c +++ b/lib/silcutil/unix/silcunixthread.c @@ -104,9 +104,8 @@ SilcBool silc_thread_wait(SilcThread thread, void **exit_value) struct SilcMutexStruct { #ifdef SILC_THREADS pthread_mutex_t mutex; -#else - void *tmp; #endif /* SILC_THREADS */ + unsigned int locked : 1; }; SilcBool silc_mutex_alloc(SilcMutex *mutex) @@ -116,6 +115,7 @@ SilcBool silc_mutex_alloc(SilcMutex *mutex) if (*mutex == NULL) return FALSE; pthread_mutex_init(&(*mutex)->mutex, NULL); + (*mutex)->locked = FALSE; return TRUE; #else return FALSE; @@ -137,7 +137,8 @@ void silc_mutex_lock(SilcMutex mutex) #ifdef SILC_THREADS if (mutex) { if (pthread_mutex_lock(&mutex->mutex)) - assert(FALSE); + SILC_ASSERT(FALSE); + mutex->locked = TRUE; } #endif /* SILC_THREADS */ } @@ -147,11 +148,20 @@ void silc_mutex_unlock(SilcMutex mutex) #ifdef SILC_THREADS if (mutex) { if (pthread_mutex_unlock(&mutex->mutex)) - assert(FALSE); + SILC_ASSERT(FALSE); + mutex->locked = FALSE; } #endif /* SILC_THREADS */ } +void silc_mutex_assert_locked(SilcMutex mutex) +{ +#ifdef SILC_THREADS + if (mutex) + SILC_ASSERT(mutex->locked); +#endif /* SILC_THREADS */ +} + /**************************** SILC Cond API ******************************/ diff --git a/lib/silcutil/win32/silcwin32thread.c b/lib/silcutil/win32/silcwin32thread.c index 0d82f559..97e516bc 100644 --- a/lib/silcutil/win32/silcwin32thread.c +++ b/lib/silcutil/win32/silcwin32thread.c @@ -153,10 +153,8 @@ SilcBool silc_thread_wait(SilcThread thread, void **exit_value) struct SilcMutexStruct { #ifdef SILC_THREADS CRITICAL_SECTION mutex; - BOOL locked; -#else - void *tmp; #endif /* SILC_THREADS */ + unsigned int locked : 1; }; SilcBool silc_mutex_alloc(SilcMutex *mutex) @@ -187,7 +185,7 @@ void silc_mutex_lock(SilcMutex mutex) #ifdef SILC_THREADS if (mutex) { EnterCriticalSection(&mutex->mutex); - assert(mutex->locked == FALSE); + SILC_ASSERT(mutex->locked == FALSE); mutex->locked = TRUE; } #endif /* SILC_THREADS */ @@ -197,13 +195,21 @@ void silc_mutex_unlock(SilcMutex mutex) { #ifdef SILC_THREADS if (mutex) { - assert(mutex->locked == TRUE); + SILC_ASSERT(mutex->locked == TRUE); mutex->locked = FALSE; LeaveCriticalSection(&mutex->mutex); } #endif /* SILC_THREADS */ } +void silc_mutex_assert_locked(SilcMutex mutex) +{ +#ifdef SILC_THREADS + if (mutex) + SILC_ASSERT(mutex->locked); +#endif /* SILC_THREADS */ +} + /**************************** SILC Cond API ******************************/ -- 2.24.0