git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@9693
48e7efb5-ca39-0410-a469-
dd3cf9ba447f
typedef struct xbt_os_thread_ {
pthread_t t;
typedef struct xbt_os_thread_ {
pthread_t t;
char *name;
void *param;
pvoid_f_pvoid_t start_routine;
char *name;
void *param;
pvoid_f_pvoid_t start_routine;
static int thread_mod_inited = 0;
/* frees the xbt_os_thread_t corresponding to the current thread */
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)
+ if (thread == main_thread) /* just killed main thread */
+ main_thread = NULL;
+
+ free(thread->running_ctx);
+ free(thread->name);
+ free(thread);
}
/* callback: context fetching */
}
/* callback: context fetching */
THROW0(system_error, errcode,
"pthread_setspecific failed for xbt_self_thread_key");
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,
}
xbt_os_thread_t xbt_os_thread_create(const char *name,
int errcode;
xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
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->name = xbt_strdup(name);
res_thread->start_routine = start_routine;
res_thread->param = param;
if ((errcode = pthread_join(thread->t, thread_return)))
THROW1(system_error, errcode, "pthread_join failed: %s",
strerror(errcode));
if ((errcode = pthread_join(thread->t, thread_return)))
THROW1(system_error, errcode, "pthread_join failed: %s",
strerror(errcode));
- if (thread->running_ctx)
- free(thread->running_ctx);
-
- 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)
}
void xbt_os_thread_exit(int *retval)
void xbt_os_thread_detach(xbt_os_thread_t thread)
{
void xbt_os_thread_detach(xbt_os_thread_t thread)
{
pthread_detach(thread->t);
}
pthread_detach(thread->t);
}