thread: implicitly create thread_t if an external thread calls thread_current()
authorMartin Willi <martin@revosec.ch>
Fri, 26 Apr 2013 14:59:34 +0000 (16:59 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 18 Jul 2013 10:17:53 +0000 (12:17 +0200)
src/libstrongswan/threading/thread.c

index d6d98d1..eb167d6 100644 (file)
@@ -341,7 +341,20 @@ thread_t *thread_create(thread_main_t main, void *arg)
  */
 thread_t *thread_current()
 {
-       return current_thread->get(current_thread);
+       private_thread_t *this;
+
+       this = (private_thread_t*)current_thread->get(current_thread);
+       if (!this)
+       {
+               this = thread_create_internal();
+
+               id_mutex->lock(id_mutex);
+               this->id = next_id++;
+               id_mutex->unlock(id_mutex);
+
+               current_thread->set(current_thread, (void*)this);
+       }
+       return &this->public;
 }
 
 /**