Added silc_mutex_trylock
[runtime.git] / lib / silcutil / unix / silcunixthread.c
index 3c5cb65904393e49d1e2089d47d03aa77a797060..fd30cf3d03b14c6e8bd75f274707c80fa27bce01 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2006 Pekka Riikonen
+  Copyright (C) 2001 - 2008 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
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 
-#include "silc.h"
+#include "silcruntime.h"
+
+/************************ Static utility functions **************************/
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other);
 
 /**************************** SILC Thread API *******************************/
 
+typedef struct {
+  SilcTls tls;
+  SilcThreadStart start_func;
+  void *context;
+} *SilcThreadStartContext;
+
+static void *silc_thread_start(void *context)
+{
+  SilcThreadStartContext c = context;
+  SilcThreadStart start_func = c->start_func;
+  void *start_context = c->context;
+  SilcTls other = c->tls;
+
+  silc_free(c);
+
+  silc_thread_tls_init_shared(other);
+
+  return start_func(start_context);
+}
+
 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
                              SilcBool waitable)
 {
 #ifdef SILC_THREADS
+  SilcThreadStartContext c;
   pthread_attr_t attr;
   pthread_t thread;
   int ret;
@@ -35,24 +59,36 @@ SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
   if (!start_func)
     return NULL;
 
+  c = silc_calloc(1, sizeof(*c));
+  if (!c)
+    return NULL;
+  c->start_func = start_func;
+  c->context = context;
+  c->tls = silc_thread_get_tls();
+
   if (pthread_attr_init(&attr)) {
-    SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+    silc_set_errno_posix(errno);
+    SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
+    silc_free(c);
     return NULL;
   }
 
   if (pthread_attr_setdetachstate(&attr,
                                  waitable ? PTHREAD_CREATE_JOINABLE :
                                  PTHREAD_CREATE_DETACHED)) {
-    SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+    silc_set_errno_posix(errno);
+    SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
     pthread_attr_destroy(&attr);
+    silc_free(c);
     return NULL;
   }
 
-  ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
-                      context);
+  ret = pthread_create(&thread, &attr, silc_thread_start, c);
   if (ret) {
-    SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
+    silc_set_errno_posix(errno);
+    SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
     pthread_attr_destroy(&attr);
+    silc_free(c);
     return NULL;
   }
 
@@ -97,6 +133,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 +148,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)
@@ -115,7 +158,12 @@ SilcBool silc_mutex_alloc(SilcMutex *mutex)
   *mutex = silc_calloc(1, sizeof(**mutex));
   if (*mutex == NULL)
     return FALSE;
-  pthread_mutex_init(&(*mutex)->mutex, NULL);
+  if (pthread_mutex_init(&(*mutex)->mutex, NULL)) {
+    silc_set_errno_posix(errno);
+    silc_free(*mutex);
+    return FALSE;
+  }
+  (*mutex)->locked = FALSE;
   return TRUE;
 #else
   return FALSE;
@@ -136,8 +184,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,14 +194,96 @@ 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 */
+}
+
+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
+  if (mutex)
+    SILC_VERIFY(mutex->locked);
+#endif /* SILC_THREADS */
+}
+
+/***************************** 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;
+  if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
+    silc_set_errno_posix(errno);
+    silc_free(*rwlock);
+    return FALSE;
   }
+  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 Cond API *******************************/
 
 /* SILC Conditional Variable context */
 struct SilcCondStruct {
@@ -170,7 +300,11 @@ SilcBool silc_cond_alloc(SilcCond *cond)
   *cond = silc_calloc(1, sizeof(**cond));
   if (*cond == NULL)
     return FALSE;
-  pthread_cond_init(&(*cond)->cond, NULL);
+  if (pthread_cond_init(&(*cond)->cond, NULL)) {
+    silc_set_errno_posix(errno);
+    silc_free(*cond);
+    return FALSE;
+  }
   return TRUE;
 #else
   return FALSE;
@@ -218,5 +352,149 @@ SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
   }
 
   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
+#else
+  return FALSE;
 #endif /* SILC_THREADS*/
 }
+
+/************************** Thread-local Storage ****************************/
+
+#if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
+     defined(HAVE_PTHREAD_ONCE))
+
+static SilcBool key_set = FALSE;
+static pthread_key_t key;
+static pthread_once_t key_once = PTHREAD_ONCE_INIT;
+
+static void silc_thread_tls_destructor(void *context)
+{
+  SilcTls tls = context;
+
+  if (tls->tls_variables)
+    silc_hash_table_free(tls->tls_variables);
+  tls->tls_variables = NULL;
+
+  silc_free(tls);
+}
+
+static void silc_thread_tls_alloc(void)
+{
+  if (pthread_key_create(&key, silc_thread_tls_destructor))
+    SILC_LOG_ERROR(("Error creating Thread-local storage"));
+  key_set = TRUE;
+}
+
+SilcTls silc_thread_tls_init(void)
+{
+  SilcTls tls;
+
+  pthread_once(&key_once, silc_thread_tls_alloc);
+
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  /* Allocate Tls for the thread */
+  tls = silc_calloc(1, sizeof(*tls));
+  if (!tls) {
+    SILC_LOG_ERROR(("Error allocating Thread-local storage"));
+    return NULL;
+  }
+  pthread_setspecific(key, tls);
+
+  /* Allocate global lock */
+  silc_mutex_alloc(&tls->lock);
+
+  return tls;
+}
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other)
+{
+  SilcTls tls;
+
+  pthread_once(&key_once, silc_thread_tls_alloc);
+
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  /* Allocate Tls for the thread */
+  tls = silc_calloc(1, sizeof(*tls));
+  if (!tls) {
+    SILC_LOG_ERROR(("Error allocating Thread-local storage"));
+    return NULL;
+  }
+  pthread_setspecific(key, tls);
+
+  /* Take shared data */
+  tls->shared_data = 1;
+  tls->lock = other->lock;
+  tls->variables = other->variables;
+
+  return tls;
+}
+
+SilcTls silc_thread_get_tls(void)
+{
+  if (!key_set)
+    return NULL;
+  return pthread_getspecific(key);
+}
+
+void silc_thread_tls_uninit(void)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls || tls->shared_data)
+    return;
+
+  /* Main thread cleanup */
+  if (tls->tls_variables)
+    silc_hash_table_free(tls->tls_variables);
+  if (tls->variables)
+    silc_hash_table_free(tls->variables);
+  if (tls->lock)
+    silc_mutex_free(tls->lock);
+  tls->variables = NULL;
+  tls->lock = NULL;
+  silc_free(tls);
+  pthread_setspecific(key, NULL);
+}
+
+#else
+
+SilcTlsStruct tls;
+SilcTls tls_ptr = NULL;
+
+SilcTls silc_thread_tls_init(void)
+{
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  tls_ptr = &tls;
+  memset(tls_ptr, 0, sizeof(*tls_ptr));
+
+  atexit(silc_thread_tls_uninit);
+
+  return tls_ptr;
+}
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other)
+{
+  return silc_thread_tls_init();
+}
+
+SilcTls silc_thread_get_tls(void)
+{
+  return tls_ptr;
+}
+
+void silc_thread_tls_uninit(void)
+{
+  if (tls.tls_variables)
+    silc_hash_table_free(tls.tls_variables);
+  if (tls.variables)
+    silc_hash_table_free(tls.variables);
+  if (tls.lock)
+    silc_mutex_free(tls.lock);
+}
+
+#endif