Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Automatically release resources associated with detached threads.
[simgrid.git] / src / xbt / xbt_os_thread.c
index c2768de..3dc40c5 100644 (file)
@@ -8,6 +8,7 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "gras_config.h"
 #include "xbt/sysdep.h"
 #include "xbt/ex.h"
 #include "xbt/ex_interface.h"   /* We play crude games with exceptions */
@@ -40,10 +41,12 @@ static xbt_os_mutex_t next_sem_ID_lock;
 
 typedef struct xbt_os_thread_ {
   pthread_t t;
+  int detached;
   char *name;
   void *param;
   pvoid_f_pvoid_t start_routine;
-  ex_ctx_t *exception;
+  xbt_running_ctx_t *running_ctx;
+  void *extra_data;
 } s_xbt_os_thread_t;
 static xbt_os_thread_t main_thread = NULL;
 
@@ -52,15 +55,20 @@ static pthread_key_t xbt_self_thread_key;
 static int thread_mod_inited = 0;
 
 /* frees the xbt_os_thread_t corresponding to the current thread */
-static void xbt_os_thread_free_thread_data(void *d)
+static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
 {
-  free(d);
+  if (thread == main_thread)    /* just killed main thread */
+    main_thread = NULL;
+
+  free(thread->running_ctx);
+  free(thread->name);
+  free(thread);
 }
 
 /* callback: context fetching */
-static ex_ctx_t *_os_thread_ex_ctx(void)
+static xbt_running_ctx_t *_os_thread_get_running_ctx(void)
 {
-  return xbt_os_thread_self()->exception;
+  return xbt_os_thread_self()->running_ctx;
 }
 
 /* callback: termination */
@@ -82,45 +90,57 @@ void xbt_os_thread_mod_preinit(void)
   if ((errcode = pthread_key_create(&xbt_self_thread_key, NULL)))
     THROW0(system_error, errcode,
            "pthread_key_create failed for xbt_self_thread_key");
-
+  
   main_thread = xbt_new(s_xbt_os_thread_t, 1);
   main_thread->name = (char *) "main";
   main_thread->start_routine = NULL;
   main_thread->param = NULL;
-  main_thread->exception = xbt_new(ex_ctx_t, 1);
-  XBT_CTX_INITIALIZE(main_thread->exception);
+  main_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
+  XBT_RUNNING_CTX_INITIALIZE(main_thread->running_ctx);
+
+  if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread)))
+    THROW0(system_error, errcode,
+           "pthread_setspecific failed for xbt_self_thread_key");
 
-  __xbt_ex_ctx = _os_thread_ex_ctx;
+  
+  __xbt_running_ctx_fetch = _os_thread_get_running_ctx;
   __xbt_ex_terminate = _os_thread_ex_terminate;
 
   thread_mod_inited = 1;
 
-#ifndef HAVE_SEM_WAIT
+#ifndef HAVE_SEM_INIT
   next_sem_ID_lock = xbt_os_mutex_init();
 #endif
 
 }
 
-void xbt_os_thread_mod_postexit(void) {
+void xbt_os_thread_mod_postexit(void)
+{
   /* FIXME: don't try to free our key on shutdown.
      Valgrind detects no leak if we don't, and whine if we try to */
   //   int errcode;
 
   //   if ((errcode=pthread_key_delete(xbt_self_thread_key)))
   //     THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
-  free(main_thread->exception);
+  free(main_thread->running_ctx);
   free(main_thread);
   main_thread = NULL;
   thread_mod_inited = 0;
-#ifndef HAVE_SEM_WAIT
+#ifndef HAVE_SEM_INIT
   xbt_os_mutex_destroy(next_sem_ID_lock);
 #endif
 
   /* Restore the default exception setup */
-  __xbt_ex_ctx = &__xbt_ex_ctx_default;
+  __xbt_running_ctx_fetch = &__xbt_ex_ctx_default;
   __xbt_ex_terminate = &__xbt_ex_terminate_default;
 }
 
+int xbt_os_thread_atfork(void (*prepare)(void),
+                         void (*parent)(void), void (*child)(void))
+{
+  return pthread_atfork(prepare, parent, child);
+}
+
 static void *wrapper_start_routine(void *s)
 {
   xbt_os_thread_t t = s;
@@ -130,22 +150,28 @@ static void *wrapper_start_routine(void *s)
     THROW0(system_error, errcode,
            "pthread_setspecific failed for xbt_self_thread_key");
 
-  return (*(t->start_routine)) (t->param);
+  void *res = (*(t->start_routine)) (t->param);
+  if (t->detached)
+    xbt_os_thread_free_thread_data(t);
+  return res;
 }
 
 xbt_os_thread_t xbt_os_thread_create(const char *name,
                                      pvoid_f_pvoid_t start_routine,
-                                     void *param)
+                                     void *param,
+                                     void *extra_data)
 {
   int errcode;
 
   xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
+  res_thread->detached = 0;
   res_thread->name = xbt_strdup(name);
   res_thread->start_routine = start_routine;
   res_thread->param = param;
-  res_thread->exception = xbt_new(ex_ctx_t, 1);
-  XBT_CTX_INITIALIZE(res_thread->exception);
-
+  res_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
+  XBT_RUNNING_CTX_INITIALIZE(res_thread->running_ctx);
+  res_thread->extra_data = extra_data;
+  
   if ((errcode = pthread_create(&(res_thread->t), NULL,
                                 wrapper_start_routine, res_thread)))
     THROW1(system_error, errcode,
@@ -161,8 +187,18 @@ const char *xbt_os_thread_name(xbt_os_thread_t t)
 
 const char *xbt_os_thread_self_name(void)
 {
-  xbt_os_thread_t self = xbt_os_thread_self();
-  return self ? self->name : "main";
+  xbt_os_thread_t me = xbt_os_thread_self();
+  return me ? me->name : "main";
+}
+
+void xbt_os_thread_set_extra_data(void *data)
+{
+  xbt_os_thread_self()->extra_data = data;
+}
+
+void *xbt_os_thread_get_extra_data(void)
+{
+  return xbt_os_thread_self()->extra_data;
 }
 
 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
@@ -173,16 +209,7 @@ void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
   if ((errcode = pthread_join(thread->t, thread_return)))
     THROW1(system_error, errcode, "pthread_join failed: %s",
            strerror(errcode));
-  if (thread->exception)
-    free(thread->exception);
-
-  if (thread->name)
-    free(thread->name);
-
-  if (thread == main_thread)    /* just killed main thread */
-    main_thread = NULL;
-
-  free(thread);
+  xbt_os_thread_free_thread_data(thread);
 }
 
 void xbt_os_thread_exit(int *retval)
@@ -198,12 +225,16 @@ xbt_os_thread_t xbt_os_thread_self(void)
     return NULL;
 
   res = pthread_getspecific(xbt_self_thread_key);
-  if (!res)
-    res = main_thread;
 
   return res;
 }
 
+void xbt_os_thread_detach(xbt_os_thread_t thread)
+{
+  thread->detached = 1;
+  pthread_detach(thread->t);
+}
+
 #include <sched.h>
 void xbt_os_thread_yield(void)
 {
@@ -262,8 +293,9 @@ void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
     case ETIMEDOUT:
       THROW1(timeout_error, 0, "mutex %p not ready", mutex);
     default:
-      THROW2(system_error, errcode, "xbt_mutex_timedacquire(%p) failed: %s",
-             mutex, strerror(errcode));
+      THROW2(system_error, errcode,
+             "xbt_mutex_timedacquire(%p) failed: %s", mutex,
+             strerror(errcode));
     }
 
 
@@ -275,11 +307,11 @@ void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
 
     ts_end.tv_sec = (time_t) floor(end);
     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
-    DEBUG2("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
+    XBT_DEBUG("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
 
     errcode = pthread_mutex_timedlock(&(mutex->m), &ts_end);
 
-#else /* Well, let's reimplement it since those lazy libc dudes didn't */
+#else                           /* Well, let's reimplement it since those lazy libc dudes didn't */
     double start = xbt_os_time();
     do {
       errcode = pthread_mutex_trylock(&(mutex->m));
@@ -290,7 +322,7 @@ void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
     if (errcode == EBUSY)
       errcode = ETIMEDOUT;
 
-#endif /* HAVE_MUTEX_TIMEDLOCK */
+#endif                          /* HAVE_MUTEX_TIMEDLOCK */
 
     switch (errcode) {
     case 0:
@@ -368,7 +400,7 @@ void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
   } else {
     ts_end.tv_sec = (time_t) floor(end);
     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
-    DEBUG3("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m),
+    XBT_DEBUG("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m),
            &ts_end);
     switch ((errcode =
              pthread_cond_timedwait(&(cond->c), &(mutex->m), &ts_end))) {
@@ -446,10 +478,10 @@ xbt_os_sem_t xbt_os_sem_init(unsigned int value)
     THROW1(system_error, errno, "sem_init() failed: %s", strerror(errno));
   res->ps = &(res->s);
 
-#else /* damn, no sem_init(). Reimplement it */
+#else                           /* damn, no sem_init(). Reimplement it */
 
   xbt_os_mutex_acquire(next_sem_ID_lock);
-  res->name = bprintf("/%d.%d", (*xbt_getpid) (), ++next_sem_ID);
+  res->name = bprintf("/%d", ++next_sem_ID);
   xbt_os_mutex_release(next_sem_ID_lock);
 
   res->ps = sem_open(res->name, O_CREAT, 0644, value);
@@ -463,7 +495,8 @@ xbt_os_sem_t xbt_os_sem_init(unsigned int value)
 
   /* Remove the name from the semaphore namespace: we never join on it */
   if (sem_unlink(res->name) < 0)
-    THROW1(system_error, errno, "sem_unlink() failed: %s", strerror(errno));
+    THROW1(system_error, errno, "sem_unlink() failed: %s",
+           strerror(errno));
 
 #endif
 
@@ -496,8 +529,9 @@ void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
     case ETIMEDOUT:
       THROW1(timeout_error, 0, "semaphore %p not ready", sem);
     default:
-      THROW2(system_error, errcode, "xbt_os_sem_timedacquire(%p) failed: %s",
-             sem, strerror(errcode));
+      THROW2(system_error, errcode,
+             "xbt_os_sem_timedacquire(%p) failed: %s", sem,
+             strerror(errcode));
     }
 
   } else {
@@ -507,10 +541,10 @@ void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
 
     ts_end.tv_sec = (time_t) floor(end);
     ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
-    DEBUG2("sem_timedwait(%p,%p)", sem->ps, &ts_end);
+    XBT_DEBUG("sem_timedwait(%p,%p)", sem->ps, &ts_end);
     errcode = sem_timedwait(sem->s, &ts_end);
 
-#else /* Okay, reimplement this function then */
+#else                           /* Okay, reimplement this function then */
     double start = xbt_os_time();
     do {
       errcode = sem_trywait(sem->ps);
@@ -528,7 +562,8 @@ void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
 
     case ETIMEDOUT:
       THROW2(timeout_error, delay,
-             "semaphore %p wasn't signaled before timeout (%f)", sem, delay);
+             "semaphore %p wasn't signaled before timeout (%f)", sem,
+             delay);
 
     default:
       THROW3(system_error, errcode, "sem_timedwait(%p,%f) failed: %s", sem,
@@ -552,10 +587,9 @@ void xbt_os_sem_destroy(xbt_os_sem_t sem)
     THROW0(arg_error, EINVAL, "Cannot destroy the NULL sempahore");
 
 #ifdef HAVE_SEM_INIT
-  if (sem_destroy(sem->ps))
-    <0)
-      THROW1(system_error, errno, "sem_destroy() failed: %s",
-             strerror(errno));
+  if (sem_destroy(sem->ps) < 0)
+    THROW1(system_error, errno, "sem_destroy() failed: %s",
+           strerror(errno));
 #else
   if (sem_close(sem->ps) < 0)
     THROW1(system_error, errno, "sem_close() failed: %s", strerror(errno));
@@ -568,15 +602,17 @@ void xbt_os_sem_destroy(xbt_os_sem_t sem)
 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
 {
   if (!sem)
-    THROW0(arg_error, EINVAL, "Cannot get the value of the NULL semaphore");
+    THROW0(arg_error, EINVAL,
+           "Cannot get the value of the NULL semaphore");
 
   if (sem_getvalue(&(sem->s), svalue) < 0)
-    THROW1(system_error, errno, "sem_getvalue() failed: %s", strerror(errno));
+    THROW1(system_error, errno, "sem_getvalue() failed: %s",
+           strerror(errno));
 }
 
 /* ********************************* WINDOWS IMPLEMENTATION ************************************ */
 
-#elif defined(WIN32)
+#elif defined(_XBT_WIN32)
 
 #include <math.h>
 
@@ -586,6 +622,7 @@ typedef struct xbt_os_thread_ {
   unsigned long id;             /* the win thread id            */
   pvoid_f_pvoid_t start_routine;
   void *param;
+  void *extra_data;
 } s_xbt_os_thread_t;
 
 /* so we can specify the size of the stack of the threads */
@@ -612,6 +649,12 @@ void xbt_os_thread_mod_postexit(void)
            "TlsFree() failed to cleanup the thread submodule");
 }
 
+int xbt_os_thread_atfork(void (*prepare)(void),
+                         void (*parent)(void), void (*child)(void))
+{
+  return 0;
+}
+
 static DWORD WINAPI wrapper_start_routine(void *s)
 {
   xbt_os_thread_t t = (xbt_os_thread_t) s;
@@ -630,7 +673,8 @@ static DWORD WINAPI wrapper_start_routine(void *s)
 
 xbt_os_thread_t xbt_os_thread_create(const char *name,
                                      pvoid_f_pvoid_t start_routine,
-                                     void *param)
+                                     void *param,
+                                     void *extra_data)
 {
 
   xbt_os_thread_t t = xbt_new(s_xbt_os_thread_t, 1);
@@ -638,7 +682,7 @@ xbt_os_thread_t xbt_os_thread_create(const char *name,
   t->name = xbt_strdup(name);
   t->start_routine = start_routine;
   t->param = param;
-
+  t->extra_data = extra_data;
   t->handle = CreateThread(NULL, XBT_DEFAULT_THREAD_STACK_SIZE,
                            (LPTHREAD_START_ROUTINE) wrapper_start_routine,
                            t, STACK_SIZE_PARAM_IS_A_RESERVATION, &(t->id));
@@ -666,12 +710,14 @@ void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
 {
 
   if (WAIT_OBJECT_0 != WaitForSingleObject(thread->handle, INFINITE))
-    THROW0(system_error, (int) GetLastError(), "WaitForSingleObject failed");
+    THROW0(system_error, (int) GetLastError(),
+           "WaitForSingleObject failed");
 
   if (thread_return) {
 
     if (!GetExitCodeThread(thread->handle, (DWORD *) (*thread_return)))
-      THROW0(system_error, (int) GetLastError(), "GetExitCodeThread failed");
+      THROW0(system_error, (int) GetLastError(),
+             "GetExitCodeThread failed");
   }
 
   CloseHandle(thread->handle);
@@ -690,6 +736,12 @@ void xbt_os_thread_exit(int *retval)
     ExitThread(0);
 }
 
+void xbt_os_thread_detach(xbt_os_thread_t thread)
+{
+  THROW_UNIMPLEMENTED;
+}
+
+
 xbt_os_thread_t xbt_os_thread_self(void)
 {
   return TlsGetValue(xbt_self_thread_key);
@@ -861,8 +913,8 @@ void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex,
   if (delay < 0) {
     xbt_os_cond_wait(cond, mutex);
   } else {
-    DEBUG3("xbt_cond_timedwait(%p,%p,%lu)", &(cond->events), &(mutex->lock),
-           end);
+    XBT_DEBUG("xbt_cond_timedwait(%p,%p,%lu)", &(cond->events),
+           &(mutex->lock), end);
 
     /* lock the threads counter and increment it */
     EnterCriticalSection(&cond->waiters_count_lock);
@@ -998,8 +1050,8 @@ void xbt_os_sem_acquire(xbt_os_sem_t sem)
 
   /* wait failure */
   if (WAIT_OBJECT_0 != WaitForSingleObject(sem->h, INFINITE))
-    THROW1(system_error, GetLastError(), "WaitForSingleObject() failed: %s",
-           strerror(GetLastError()));
+    THROW1(system_error, GetLastError(),
+           "WaitForSingleObject() failed: %s", strerror(GetLastError()));
   EnterCriticalSection(&(sem->value_lock));
   sem->value--;
   LeaveCriticalSection(&(sem->value_lock));
@@ -1075,7 +1127,8 @@ void xbt_os_sem_destroy(xbt_os_sem_t sem)
 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
 {
   if (!sem)
-    THROW0(arg_error, EINVAL, "Cannot get the value of the NULL semaphore");
+    THROW0(arg_error, EINVAL,
+           "Cannot get the value of the NULL semaphore");
 
   EnterCriticalSection(&(sem->value_lock));
   *svalue = sem->value;