From 7de9cc2f04ea944de8a0f02b0c8f69856a085694 Mon Sep 17 00:00:00 2001 From: mquinson Date: Sun, 5 Aug 2007 15:41:05 +0000 Subject: [PATCH] harden xbt_os_thread_self to work when module not inited, and xbt_os_thread_getparam when called from master thread (need because we use it in the logs now) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3969 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/xbt/xbt_os_thread.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/xbt/xbt_os_thread.c b/src/xbt/xbt_os_thread.c index 2dc4aa24d3..122446c3d7 100644 --- a/src/xbt/xbt_os_thread.c +++ b/src/xbt/xbt_os_thread.c @@ -31,6 +31,7 @@ typedef struct xbt_os_thread_ { /* thread-specific data containing the xbt_os_thread_t structure */ 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){ @@ -40,8 +41,13 @@ static void xbt_os_thread_free_thread_data(void*d){ void xbt_os_thread_mod_init(void) { int errcode; + if (thread_mod_inited) + return; + if ((errcode=pthread_key_create(&xbt_self_thread_key, NULL))) THROW0(system_error,errcode,"pthread_key_create failed for xbt_self_thread_key"); + + thread_mod_inited = 1; } void xbt_os_thread_mod_exit(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 */ @@ -90,7 +96,7 @@ void xbt_os_thread_exit(int *retval) { } xbt_os_thread_t xbt_os_thread_self(void) { - return pthread_getspecific(xbt_self_thread_key); + return thread_mod_inited ? pthread_getspecific(xbt_self_thread_key):NULL; } #include @@ -211,7 +217,7 @@ void xbt_os_cond_destroy(xbt_os_cond_t cond){ void *xbt_os_thread_getparam(void) { xbt_os_thread_t t = xbt_os_thread_self(); - return t->param; + return t?t->param:NULL; } /* ********************************* WINDOWS IMPLEMENTATION ************************************ */ -- 2.20.1