Added SILC Thread Queue API
[silc.git] / lib / silcutil / silcthread.c
index 794259c78dc11eab00bd71d39679053c0dabc988..8e51a15d13ef4c182382cf029f3b0f41512c3deb 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "silc.h"
 
+/***************************** Thread Pool API *****************************/
+
 /* Explanation of the thread pool execution.
 
    When new call is added to thread pool by calling silc_thread_pool_run
@@ -317,10 +319,16 @@ SilcThreadPool silc_thread_pool_alloc(SilcStack stack,
   SilcThreadPool tp;
   int i;
 
-  if (max_threads < min_threads)
+  if (max_threads < min_threads) {
+    silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT,
+                         "Max threads is smaller than min threads (%d < %d)",
+                         max_threads, min_threads);
     return NULL;
-  if (!max_threads)
+  }
+  if (!max_threads) {
+    silc_set_errno_reason(SILC_ERR_INVALID_ARGUMENT, "Max threads is 0");
     return NULL;
+  }
 
   if (stack)
     stack = silc_stack_alloc(0, stack);
@@ -409,6 +417,7 @@ SilcBool silc_thread_pool_run(SilcThreadPool tp,
 
   if (tp->destroy) {
     silc_mutex_unlock(tp->lock);
+    silc_set_errno(SILC_ERR_NOT_VALID);
     return FALSE;
   }
 
@@ -420,6 +429,7 @@ SilcBool silc_thread_pool_run(SilcThreadPool tp,
       /* Maximum threads reached */
       if (!queuable) {
        silc_mutex_unlock(tp->lock);
+       silc_set_errno(SILC_ERR_LIMIT);
        return FALSE;
       }
 
@@ -572,3 +582,27 @@ void silc_thread_pool_purge(SilcThreadPool tp)
   silc_list_start(tp->threads);
   silc_mutex_unlock(tp->lock);
 }
+
+/*************************** Thread-local Storage ***************************/
+
+void silc_thread_tls_set(void *context)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls) {
+    /* Initialize Tls for this thread */
+    tls = silc_thread_tls_init();
+    if (!tls)
+      return;
+  }
+
+  tls->thread_context = context;
+}
+
+void *silc_thread_tls_get(void)
+{
+  SilcTls tls = silc_thread_get_tls();
+  if (!tls)
+    return NULL;
+  return tls->thread_context;
+}