Allow NULL to silc_mutex_* functions.
authorPekka Riikonen <priikone@silcnet.org>
Thu, 5 May 2005 18:37:21 +0000 (18:37 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Thu, 5 May 2005 18:37:21 +0000 (18:37 +0000)
lib/silcutil/silcmutex.h
lib/silcutil/unix/silcunixmutex.c
lib/silcutil/win32/silcwin32mutex.c

index abd6e043909038fd41fcb069a874e6058c2463ad..19b4863f61790e8be92b5f40b67cd27b880cc0e9 100644 (file)
@@ -86,7 +86,8 @@ bool silc_mutex_alloc(SilcMutex *mutex);
  *
  * DESCRIPTION
  *
- *    Free SILC Mutex object and frees all allocated memory.
+ *    Free SILC Mutex object and frees all allocated memory.  If `mutex'
+ *    is NULL this function has no effect.
  *
  ***/
 void silc_mutex_free(SilcMutex mutex);
@@ -101,7 +102,8 @@ void silc_mutex_free(SilcMutex mutex);
  *
  *    Locks the mutex. If the mutex is locked by another thread the
  *    current thread will block until the other thread has issued
- *    silc_mutex_unlock for the mutex.
+ *    silc_mutex_unlock for the mutex.  If `mutex' is NULL this function
+ *    has no effect.
  *
  * NOTES
  *
@@ -121,7 +123,8 @@ void silc_mutex_lock(SilcMutex mutex);
  * DESCRIPTION
  *
  *    Unlocks the mutex and thus releases it for another thread that
- *    may be waiting for the lock.
+ *    may be waiting for the lock.  If `mutex' is NULL this function
+ *    has no effect.
  *
  * NOTES
  *
index 27a1655a468379ab4c96949f0a78d16c89d4e465..1336eaeea7d57592db62d2983d3f4dda6efca471 100644 (file)
@@ -9,7 +9,7 @@
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.
-  
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -44,27 +44,33 @@ bool silc_mutex_alloc(SilcMutex *mutex)
 void silc_mutex_free(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  pthread_mutex_destroy(&mutex->mutex);
-  silc_free(mutex);
+  if (mutex) {
+    pthread_mutex_destroy(&mutex->mutex);
+    silc_free(mutex);
+  }
 #endif /* SILC_THREADS */
 }
 
 void silc_mutex_lock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  if (pthread_mutex_lock(&mutex->mutex))
-    assert(FALSE);
-  assert(mutex->locked == 0);
-  mutex->locked = 1;
+  if (mutex) {
+    if (pthread_mutex_lock(&mutex->mutex))
+      assert(FALSE);
+    assert(mutex->locked == 0);
+    mutex->locked = 1;
+  }
 #endif /* SILC_THREADS */
 }
 
 void silc_mutex_unlock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  assert(mutex->locked == 1);
-  mutex->locked = 0;
-  if (pthread_mutex_unlock(&mutex->mutex))
-    assert(FALSE);
+  if (mutex) {
+    assert(mutex->locked == 1);
+    mutex->locked = 0;
+    if (pthread_mutex_unlock(&mutex->mutex))
+      assert(FALSE);
+  }
 #endif /* SILC_THREADS */
 }
index 64c3e9381d24c22f95874bc4d938f858a20a47c3..09e8e90ab01774d1edd5db1f1d13d80cb14c85eb 100644 (file)
@@ -9,7 +9,7 @@
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.
-  
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -21,9 +21,9 @@
 #include "silcincludes.h"
 
 /* SILC Mutex structure */
-struct SilcMutexStruct {       
+struct SilcMutexStruct {
 #ifdef SILC_THREADS
-  CRITICAL_SECTION mutex;  
+  CRITICAL_SECTION mutex;
   BOOL locked;
 #else
   void *tmp;
@@ -33,8 +33,8 @@ struct SilcMutexStruct {
 bool silc_mutex_alloc(SilcMutex *mutex)
 {
 #ifdef SILC_THREADS
-  *mutex = silc_calloc(1, sizeof(**mutex));  
-  InitializeCriticalSection(&((*mutex)->mutex));  
+  *mutex = silc_calloc(1, sizeof(**mutex));
+  InitializeCriticalSection(&((*mutex)->mutex));
 #endif /* SILC_THREADS */
   return TRUE;
 }
@@ -42,25 +42,31 @@ bool silc_mutex_alloc(SilcMutex *mutex)
 void silc_mutex_free(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  DeleteCriticalSection(&mutex->mutex);
-  silc_free(mutex);
+  if (mutex) {
+    DeleteCriticalSection(&mutex->mutex);
+    silc_free(mutex);
+  }
 #endif /* SILC_THREADS */
 }
 
 void silc_mutex_lock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  EnterCriticalSection(&mutex->mutex);    
-  assert(mutex->locked == FALSE);
-  mutex->locked = TRUE;
+  if (mutex) {
+    EnterCriticalSection(&mutex->mutex);
+    assert(mutex->locked == FALSE);
+    mutex->locked = TRUE;
+  }
 #endif /* SILC_THREADS */
 }
 
 void silc_mutex_unlock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  assert(mutex->locked == TRUE);
-  mutex->locked = FALSE;
-  LeaveCriticalSection(&mutex->mutex);       
+  if (mutex) {
+    assert(mutex->locked == TRUE);
+    mutex->locked = FALSE;
+    LeaveCriticalSection(&mutex->mutex);
+  }
 #endif /* SILC_THREADS */
 }