#include "xbt/log.h"
#include "xbt/dynar.h"
#include "xbt/xbt_thread.h"
-#include <pthread.h> // I need pthread_join that is not yet available in xbt_thread.
+/*#include <pthread.h>*/ /* I need pthread_join that is not yet available in xbt_thread.*/
#ifdef CONTEXT_THREADS
/* This file (context.c) is only loaded in libsimgrid, not libgras.
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)
if (!context) return;
DEBUG1("Freeing %p",context);
#ifdef CONTEXT_THREADS
- DEBUG1("\t joining %p",(void *)context->thread->t);
- pthread_join(context->thread->t,NULL);
+ /*DEBUG1("\t joining %p",(void *)context->thread->t);*/
+ DEBUG1("\t joining %p",(void *)context->thread);
+ /*pthread_join(context->thread->t,NULL);*/
+ xbt_thread_join(context->thread,NULL);
DEBUG1("\t xbt_free %p",(void *)context->thread);
xbt_free(context->thread);
DEBUG1("\t mutex_destroy %p",(void *)context->mutex);
context->thread = xbt_thread_self();
DEBUG2("**[%p:%p]** Lock ****",context,(void*)xbt_thread_self());
- xbt_mutex_lock(context->mutex);
+ xbt_mutex_lock(creation_mutex);
- DEBUG2("**[%p:%p]** Releasing the prisonner ****",context,(void*)xbt_thread_self());
- xbt_thcond_signal(context->cond);
+ DEBUG2("**[%p:%p]** Releasing the creator ****",context,(void*)xbt_thread_self());
+ xbt_thcond_signal(creation_cond);
+ xbt_mutex_unlock(creation_mutex);
DEBUG2("**[%p:%p]** Going to Jail ****",context,(void*)xbt_thread_self());
+ xbt_mutex_lock(context->mutex);
xbt_thcond_wait(context->cond, context->mutex);
DEBUG2("**[%p:%p]** Unlocking ****",context,(void*)xbt_thread_self());
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
}
}
#ifdef CONTEXT_THREADS
/* Launch the thread */
DEBUG1("**[%p]** Locking ****",context);
- xbt_mutex_lock(context->mutex);
+ xbt_mutex_lock(creation_mutex);
DEBUG1("**[%p]** Thread create ****",context);
context->thread = xbt_thread_create(__context_wrapper, context);
DEBUG2("**[%p]** Thread created : %p ****",context,context->thread);
DEBUG1("**[%p]** Going to jail ****",context);
- xbt_thcond_wait(context->cond, context->mutex);
+ xbt_thcond_wait(creation_cond, creation_mutex);
DEBUG1("**[%p]** Unlocking ****",context);
- xbt_mutex_unlock(context->mutex);
+ xbt_mutex_unlock(creation_mutex);
#else
makecontext (&(context->uc), (void (*) (void)) __context_wrapper,1, context);
#endif
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); */
xbt_swag_free(context_living);
init_context = current_context = NULL ;
+#ifdef CONTEXT_THREADS
+ xbt_mutex_destroy(creation_mutex);
+ xbt_thcond_destroy(creation_cond);
+#endif
}
/**