Clear locked flag before unlocking mutex, not after.
[crypto.git] / lib / silcutil / unix / silcunixthread.c
index b32f61d62d6d663f4042b7d6b513ae65ea6f0dcc..2415a380e31eb3bb21509ff4be74b9c58ce07d2b 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2006 Pekka Riikonen
+  Copyright (C) 2001 - 2007 Pekka Riikonen
 
   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
@@ -97,6 +97,14 @@ SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
 #endif
 }
 
+void silc_thread_yield(void)
+{
+#ifdef SILC_THREADS
+#ifdef HAVE_SCHED_YIELD
+  sched_yield();
+#endif /* HAVE_SCHED_YIELD */
+#endif /* SILC_THREADS */
+}
 
 /***************************** SILC Mutex API *******************************/
 
@@ -104,9 +112,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 +123,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;
@@ -136,8 +144,8 @@ void silc_mutex_lock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
   if (mutex) {
-    if (pthread_mutex_lock(&mutex->mutex))
-      assert(FALSE);
+    SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0);
+    mutex->locked = TRUE;
   }
 #endif /* SILC_THREADS */
 }
@@ -146,17 +154,82 @@ void silc_mutex_unlock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
   if (mutex) {
-    if (pthread_mutex_unlock(&mutex->mutex))
-      assert(FALSE);
+    mutex->locked = FALSE;
+    SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0);
   }
 #endif /* SILC_THREADS */
 }
 
+void silc_mutex_assert_locked(SilcMutex mutex)
+{
+#ifdef SILC_THREADS
+  if (mutex)
+    SILC_VERIFY(mutex->locked);
+#endif /* SILC_THREADS */
+}
 
-/**************************** SILC CondVar API ******************************/
+/***************************** SILC Rwlock API ******************************/
+
+/* SILC read/write lock structure */
+struct SilcRwLockStruct {
+#ifdef SILC_THREADS
+  pthread_rwlock_t rwlock;
+#else
+  void *tmp;
+#endif /* SILC_THREADS */
+};
+
+SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
+{
+#ifdef SILC_THREADS
+  *rwlock = silc_calloc(1, sizeof(**rwlock));
+  if (*rwlock == NULL)
+    return FALSE;
+  pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
+  return TRUE;
+#else
+  return FALSE;
+#endif /* SILC_THREADS */
+}
+
+void silc_rwlock_free(SilcRwLock rwlock)
+{
+#ifdef SILC_THREADS
+  if (rwlock) {
+    pthread_rwlock_destroy(&rwlock->rwlock);
+    silc_free(rwlock);
+  }
+#endif /* SILC_THREADS */
+}
+
+void silc_rwlock_rdlock(SilcRwLock rwlock)
+{
+#ifdef SILC_THREADS
+  if (rwlock)
+    pthread_rwlock_rdlock(&rwlock->rwlock);
+#endif /* SILC_THREADS */
+}
+
+void silc_rwlock_wrlock(SilcRwLock rwlock)
+{
+#ifdef SILC_THREADS
+  if (rwlock)
+    SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
+#endif /* SILC_THREADS */
+}
+
+void silc_rwlock_unlock(SilcRwLock rwlock)
+{
+#ifdef SILC_THREADS
+  if (rwlock)
+    SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
+#endif /* SILC_THREADS */
+}
+
+/****************************** SILC Cond API *******************************/
 
 /* SILC Conditional Variable context */
-struct SilcCondVarStruct {
+struct SilcCondStruct {
 #ifdef SILC_THREADS
   pthread_cond_t cond;
 #else
@@ -164,7 +237,7 @@ struct SilcCondVarStruct {
 #endif /* SILC_THREADS*/
 };
 
-SilcBool silc_condvar_alloc(SilcCondVar *cond)
+SilcBool silc_cond_alloc(SilcCond *cond)
 {
 #ifdef SILC_THREADS
   *cond = silc_calloc(1, sizeof(**cond));
@@ -177,7 +250,7 @@ SilcBool silc_condvar_alloc(SilcCondVar *cond)
 #endif /* SILC_THREADS*/
 }
 
-void silc_condvar_free(SilcCondVar cond)
+void silc_cond_free(SilcCond cond)
 {
 #ifdef SILC_THREADS
   pthread_cond_destroy(&cond->cond);
@@ -185,29 +258,29 @@ void silc_condvar_free(SilcCondVar cond)
 #endif /* SILC_THREADS*/
 }
 
-void silc_condvar_signal(SilcCondVar cond)
+void silc_cond_signal(SilcCond cond)
 {
 #ifdef SILC_THREADS
   pthread_cond_signal(&cond->cond);
 #endif /* SILC_THREADS*/
 }
 
-void silc_condvar_broadcast(SilcCondVar cond)
+void silc_cond_broadcast(SilcCond cond)
 {
 #ifdef SILC_THREADS
   pthread_cond_broadcast(&cond->cond);
 #endif /* SILC_THREADS*/
 }
 
-void silc_condvar_wait(SilcCondVar cond, SilcMutex mutex)
+void silc_cond_wait(SilcCond cond, SilcMutex mutex)
 {
 #ifdef SILC_THREADS
   pthread_cond_wait(&cond->cond, &mutex->mutex);
 #endif /* SILC_THREADS*/
 }
 
-SilcBool silc_condvar_timedwait(SilcCondVar cond, SilcMutex mutex,
-                               int timeout)
+SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
+                            int timeout)
 {
 #ifdef SILC_THREADS
   struct timespec t;
@@ -218,5 +291,7 @@ SilcBool silc_condvar_timedwait(SilcCondVar cond, SilcMutex mutex,
   }
 
   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
+#else
+  return FALSE;
 #endif /* SILC_THREADS*/
 }