Added silc_mutex_trylock
authorPekka Riikonen <priikone@silcnet.org>
Sat, 23 Feb 2008 13:41:33 +0000 (15:41 +0200)
committerPekka Riikonen <priikone@silcnet.org>
Sat, 23 Feb 2008 13:41:33 +0000 (15:41 +0200)
lib/silcutil/silcmutex.h
lib/silcutil/unix/silcunixthread.c

index 878d37deb8fdf4f9365aa7f67f9ce25140028cdf..2ff79f39c15e42cd39c04241c5aa8339c0436bec 100644 (file)
@@ -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
index 286a26db7fba5e4f3da8b7315fc1e304e7b6929b..fd30cf3d03b14c6e8bd75f274707c80fa27bce01 100644 (file)
@@ -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