Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Also avoid to leak the stub under windows
[simgrid.git] / src / xbt / xbt_thread.c
index 4152995..7936071 100644 (file)
@@ -22,6 +22,8 @@
 
 typedef struct xbt_thread_ {
    pthread_t t;
+   void *param;
+   pvoid_f_pvoid_t *start_routine;
 } s_xbt_thread_t ;
 
 /* thread-specific data containing the xbt_thread_t structure */
@@ -46,35 +48,28 @@ void xbt_thread_mod_exit(void) {
 //     THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
 }
 
-typedef struct s_xbt_thread_wrapper_for_restart__ {
-  pvoid_f_pvoid_t *start_routine;
-  void* param;
-  xbt_thread_t res;
-} s_xbt_thread_wrapper_for_restart_t, *xbt_thread_wrapper_for_restart_t;
-
 static void * wrapper_start_routine(void *s) {
-  xbt_thread_wrapper_for_restart_t stub = s;
+  xbt_thread_t t = s;   
   int errcode;
 
-   if ((errcode=pthread_setspecific(xbt_self_thread_key,stub->res)))
-     THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");
-   return stub->start_routine(stub->param);
+  if ((errcode=pthread_setspecific(xbt_self_thread_key,t)))
+    THROW0(system_error,errcode,"pthread_setspecific failed for xbt_self_thread_key");   
+  return t->start_routine(t->param);
 }
 
 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
                               void* param)  {
    int errcode;
-   xbt_thread_wrapper_for_restart_t stub = xbt_new0(s_xbt_thread_wrapper_for_restart_t,1);
 
-   stub->start_routine = start_routine ;
-   stub->param = param;
-   stub->res = xbt_new(s_xbt_thread_t,1);
+   xbt_thread_t res_thread=xbt_new(s_xbt_thread_t,1);
+   res_thread->start_routine = start_routine;
+   res_thread->param = param;
 
    
-   if ((errcode = pthread_create(&(stub->res->t), NULL, wrapper_start_routine, stub)))
+   if ((errcode = pthread_create(&(res_thread->t), NULL, wrapper_start_routine, res_thread)))
      THROW0(system_error,errcode, "pthread_create failed");
-   
-   return stub->res;
+
+   return res_thread;
 }
 
 void 
@@ -183,15 +178,11 @@ void xbt_thcond_destroy(xbt_thcond_t cond){
 #elif defined(WIN32)
 
 typedef struct xbt_thread_ {
-   HANDLE handle;                  /* the win thread handle        */
-   unsigned long id;               /* the win thread id            */
-} s_xbt_thread_t ;
-
-typedef struct s_xbt_thread_wrapper_for_restart__ {
+  HANDLE handle;                  /* the win thread handle        */
+  unsigned long id;               /* the win thread id            */
   pvoid_f_pvoid_t *start_routine;
   void* param;
-  xbt_thread_t res;
-} s_xbt_thread_wrapper_for_restart_t, *xbt_thread_wrapper_for_restart_t;
+} s_xbt_thread_t ;
 
 /* key to the TLS containing the xbt_thread_t structure */
 static unsigned long xbt_self_thread_key;
@@ -205,35 +196,34 @@ void xbt_thread_mod_exit(void) {
      THROW0(system_error,(int)GetLastError(),"TlsFree() failed to cleanup the thread submodule");
 }
 
-DWORD WINAPI  wrapper_start_routine(void *s) {
-  xbt_thread_wrapper_for_restart_t stub = (xbt_thread_wrapper_for_restart_t)s;
+static DWORD WINAPI  wrapper_start_routine(void *s) {
+  xbt_thread_t t = (xbt_thread_t)s;
  
-    if(!TlsSetValue(xbt_self_thread_key,stub->res))
+    if(!TlsSetValue(xbt_self_thread_key,t))
      THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
    
-   return (DWORD)stub->start_routine(stub->param);
+   return (DWORD)t->start_routine(t->param);
 }
 
 
 xbt_thread_t xbt_thread_create(pvoid_f_pvoid_t start_routine,
                               void* param)  {
    
-   xbt_thread_wrapper_for_restart_t stub = xbt_new0(s_xbt_thread_wrapper_for_restart_t,1);
+   xbt_thread_t t = xbt_new(s_xbt_thread_t,1);
 
-   stub->start_routine = start_routine ;
-   stub->param = param;
-   stub->res = xbt_new(s_xbt_thread_t,1);
+   t->start_routine = start_routine ;
+   t->param = param;
    
-   stub->res->handle = CreateThread(NULL,0,
-                             (LPTHREAD_START_ROUTINE)wrapper_start_routine,
-                             stub,0,&(stub->res->id));
+   t->handle = CreateThread(NULL,0,
+                           (LPTHREAD_START_ROUTINE)wrapper_start_routine,
+                           t,0,&(t->id));
        
-   if(!stub->res->handle) {
-     xbt_free(stub->res);
+   if(!t->handle) {
+     xbt_free(t);
      THROW0(system_error,(int)GetLastError(),"CreateThread failed");
    }
    
-   return stub->res;
+   return t;
 }
 
 void 
@@ -252,9 +242,6 @@ xbt_thread_join(xbt_thread_t thread,void ** thread_return) {
 }
 
 void xbt_thread_exit(int *retval) {
-   
-   xbt_thread_t self = xbt_thread_self();
-   
    if(retval)
        ExitThread(*retval);
    else