Added SILC Thread Queue API
[silc.git] / lib / silcutil / symbian / silcsymbianthread.cpp
index f3aa6f26a0dc29e64c7b91e90f6bea244fa90502..05a2e893b294a22ed8b72e20ad838e0dc35dda49 100644 (file)
@@ -46,9 +46,12 @@ static TInt silc_thread_start(TAny *context)
   void *user_context = tc->context;
   SilcBool waitable = tc->waitable;
   void *ret = NULL;
+  SilcTls tls;
 
   silc_free(tc);
 
+  tls = silc_thread_tls_init();
+
   CTrapCleanup *cs = CTrapCleanup::New();
   if (cs) {
     CActiveScheduler *s = new CActiveScheduler;
@@ -63,6 +66,7 @@ static TInt silc_thread_start(TAny *context)
     delete cs;
   }
 
+  silc_free(tls);
   silc_thread_exit(ret);
 
 #endif
@@ -228,8 +232,8 @@ void silc_mutex_unlock(SilcMutex mutex)
 {
 #ifdef SILC_THREADS
   if (mutex) {
-    mutex->mutex->Signal();
     mutex->locked = FALSE;
+    mutex->mutex->Signal();
   }
 #endif /* SILC_THREADS */
 }
@@ -409,4 +413,28 @@ SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
 #endif /* SILC_THREADS*/
 }
 
+/************************** Thread-local Storage ****************************/
+
+SilcTls silc_thread_tls_init(void)
+{
+  SilcTls tls;
+
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  /* Allocate Tls for the thread */
+  tls = (SilcTls)silc_calloc(1, sizeof(*tls));
+  if (!tls)
+    return NULL;
+
+  Dll::SetTls(tls);
+
+  return tls;
+}
+
+SilcTls silc_thread_get_tls(void)
+{
+  return STATIC_CAST(SilcTls, Dll::Tls());
+}
+
 } /* extern "C" */