X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b4d4da7d43fb917b14ab7b7d1b9538ad65569844..c3fa98ca7ce6d3cebc47f87e1e783a214b04b35f:/src/xbt/context.c diff --git a/src/xbt/context.c b/src/xbt/context.c index 9b3dd1111a..56936cdcf8 100644 --- a/src/xbt/context.c +++ b/src/xbt/context.c @@ -33,6 +33,10 @@ static xbt_context_t current_context = NULL; static xbt_context_t init_context = NULL; static xbt_swag_t context_to_destroy = NULL; static xbt_swag_t context_living = NULL; +#ifdef CONTEXT_THREADS +static xbt_mutex_t creation_mutex; +static xbt_thcond_t creation_cond; +#endif static void __context_exit(xbt_context_t context ,int value); static void __xbt_context_yield(xbt_context_t context) @@ -44,15 +48,15 @@ static void __xbt_context_yield(xbt_context_t context) #ifdef CONTEXT_THREADS if (context){ xbt_context_t self = current_context; - DEBUG1("[%p] **** Locking ****", self); + DEBUG2("[%p] **** Locking ctx %p ****", self, context); xbt_mutex_lock(context->mutex); DEBUG1("[%p] **** Updating current_context ****", self); current_context = context; DEBUG1("[%p] **** Releasing the prisonner ****", self); xbt_thcond_signal(context->cond); - DEBUG1("[%p] **** Going to jail ****", self); + DEBUG3("[%p] **** Going to jail on individual %p/%p ****", self,context->cond, context->mutex); xbt_thcond_wait(context->cond, context->mutex); - DEBUG1("[%p] **** Unlocking ****", self); + DEBUG2("[%p] **** Unlocking individual %p ****", self,context->mutex); xbt_mutex_unlock(context->mutex); DEBUG1("[%p] **** Updating current_context ****", self); current_context = self; @@ -158,13 +162,13 @@ static void __context_exit(xbt_context_t context ,int value) DEBUG0("Yielding"); #ifdef CONTEXT_THREADS - DEBUG1("[%p] **** Locking ****", context); + DEBUG2("[%p] **** Locking %p ****", context, context->mutex); xbt_mutex_lock(context->mutex); /* DEBUG1("[%p] **** Updating current_context ****"); */ /* current_context = context; */ DEBUG1("[%p] **** Releasing the prisonner ****", context); xbt_thcond_signal(context->cond); - DEBUG1("[%p] **** Unlocking ****", context); + DEBUG2("[%p] **** Unlocking individual %p ****", context, context->mutex); xbt_mutex_unlock(context->mutex); DEBUG1("[%p] **** Exiting ****", context); xbt_thread_exit(NULL); // We should provide return value in case other wants it @@ -176,21 +180,26 @@ static void __context_exit(xbt_context_t context ,int value) static void * __context_wrapper(void* c) { - xbt_context_t context = (xbt_context_t)c; + xbt_context_t context = (xbt_context_t) current_context; #ifdef CONTEXT_THREADS context->thread = xbt_thread_self(); - DEBUG2("**[%p:%p]** Lock ****",context,(void*)xbt_thread_self()); + DEBUG3("**[ctx:%p;self:%p]** Lock creation_mutex %p ****",context,(void*)xbt_thread_self(), creation_mutex); + xbt_mutex_lock(creation_mutex); xbt_mutex_lock(context->mutex); - DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)xbt_thread_self()); - xbt_thcond_signal(context->cond); + DEBUG4("**[ctx:%p;self:%p]** Releasing the creator (creation_cond %p,%p) ****", + context,(void*)xbt_thread_self(),creation_cond,creation_mutex); + xbt_thcond_signal(creation_cond); + xbt_mutex_unlock(creation_mutex); - DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)xbt_thread_self()); + DEBUG4("**[ctx:%p;self:%p]** Going to Jail on lock %p and cond %p ****", + context,(void*)xbt_thread_self(),context->mutex,context->cond); xbt_thcond_wait(context->cond, context->mutex); - DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)xbt_thread_self()); + DEBUG3("**[ctx:%p;self:%p]** Unlocking individual %p ****", + context,(void*)xbt_thread_self(),context->mutex); xbt_mutex_unlock(context->mutex); #endif @@ -244,6 +253,10 @@ void xbt_context_init(void) context_to_destroy = xbt_swag_new(xbt_swag_offset(*current_context,hookup)); context_living = xbt_swag_new(xbt_swag_offset(*current_context,hookup)); xbt_swag_insert(init_context, context_living); +#ifdef CONTEXT_THREADS + creation_mutex = xbt_mutex_init(); + creation_cond = xbt_thcond_init(); +#endif } } @@ -271,17 +284,18 @@ void xbt_context_start(xbt_context_t context) { #ifdef CONTEXT_THREADS /* Launch the thread */ - DEBUG1("**[%p]** Locking ****",context); - xbt_mutex_lock(context->mutex); + DEBUG3("**[ctx:%p;self:%p]** Locking creation_mutex %p ****",context,xbt_thread_self(),creation_mutex); + xbt_mutex_lock(creation_mutex); - DEBUG1("**[%p]** Thread create ****",context); + DEBUG2("**[ctx:%p;self:%p]** Thread create ****",context,xbt_thread_self()); context->thread = xbt_thread_create(__context_wrapper, context); - DEBUG2("**[%p]** Thread created : %p ****",context,context->thread); + DEBUG3("**[ctx:%p;self:%p]** Thread created : %p ****",context,xbt_thread_self(),context->thread); - DEBUG1("**[%p]** Going to jail ****",context); - xbt_thcond_wait(context->cond, context->mutex); - DEBUG1("**[%p]** Unlocking ****",context); - xbt_mutex_unlock(context->mutex); + DEBUG4("**[ctx:%p;self:%p]** Going to jail on creation_cond/mutex (%p,%p) ****", + context,xbt_thread_self(),creation_cond, creation_mutex); + xbt_thcond_wait(creation_cond, creation_mutex); + DEBUG3("**[ctx:%p;self:%p]** Unlocking creation %p ****",context, xbt_thread_self(),creation_mutex); + xbt_mutex_unlock(creation_mutex); #else makecontext (&(context->uc), (void (*) (void)) __context_wrapper,1, context); #endif @@ -313,7 +327,7 @@ xbt_context_t xbt_context_new(xbt_context_function_t code, res->mutex = xbt_mutex_init(); res->cond = xbt_thcond_init(); #else - /* FIXME: strerror is not thread safe */ + xbt_assert2(getcontext(&(res->uc))==0,"Error in context saving: %d (%s)", errno, strerror(errno)); res->uc.uc_link = NULL; /* res->uc.uc_link = &(current_context->uc); */ @@ -375,13 +389,18 @@ void xbt_context_exit(void) { xbt_context_kill(context); } } - // xbt_context_kill(init_context); + free(init_context->exception); + free(init_context); + init_context = current_context = NULL ; xbt_context_empty_trash(); xbt_swag_free(context_to_destroy); xbt_swag_free(context_living); - init_context = current_context = NULL ; +#ifdef CONTEXT_THREADS + xbt_mutex_destroy(creation_mutex); + xbt_thcond_destroy(creation_cond); +#endif } /**