+static void __context_exit(xbt_context_t context, int value)
+{
+ int i;
+
+ DEBUG1("--------- %p is exiting ---------", context);
+
+ DEBUG0("Calling cleanup functions");
+ if (context->cleanup_func) {
+ DEBUG0("Calling cleanup function");
+ context->cleanup_func(context->cleanup_arg);
+ }
+
+ DEBUG0("Freeing arguments");
+ for (i = 0; i < context->argc; i++)
+ if (context->argv[i])
+ free(context->argv[i]);
+
+ if (context->argv)
+ free(context->argv);
+
+ DEBUG0("Putting context in the to_destroy set");
+ xbt_swag_remove(context, context_living);
+ xbt_swag_insert(context, context_to_destroy);
+ DEBUG0("Context put in the to_destroy set");
+
+ DEBUG0("Yielding");
+
+#ifdef CONTEXT_THREADS
+ DEBUG2("[%p] **** Locking %p ****", context, context->mutex);
+ xbt_os_mutex_lock(context->mutex);
+/* DEBUG1("[%p] **** Updating current_context ****"); */
+/* current_context = context; */
+ DEBUG1("[%p] **** Releasing the prisonner ****", context);
+ xbt_os_cond_signal(context->cond);
+ DEBUG2("[%p] **** Unlocking individual %p ****", context,
+ context->mutex);
+ xbt_os_mutex_unlock(context->mutex);
+ DEBUG1("[%p] **** Exiting ****", context);
+ xbt_os_thread_exit(NULL); // We should provide return value in case other wants it
+#else
+ __xbt_context_yield(context);
+#endif
+ xbt_assert0(0, "You can't be here!");
+}
+
+static void *__context_wrapper(void *c)
+{
+ xbt_context_t context = current_context;
+
+#ifdef CONTEXT_THREADS
+ context = (xbt_context_t) c;
+ context->thread = xbt_os_thread_self();
+
+ DEBUG3("**[ctx:%p;self:%p]** Lock creation_mutex %p ****", context,
+ (void *) xbt_os_thread_self(), creation_mutex);
+ xbt_os_mutex_lock(creation_mutex);
+ xbt_os_mutex_lock(context->mutex);
+
+ DEBUG4
+ ("**[ctx:%p;self:%p]** Releasing the creator (creation_cond %p,%p) ****",
+ context, (void *) xbt_os_thread_self(), creation_cond,
+ creation_mutex);
+ xbt_os_cond_signal(creation_cond);
+ xbt_os_mutex_unlock(creation_mutex);
+
+ DEBUG4("**[ctx:%p;self:%p]** Going to Jail on lock %p and cond %p ****",
+ context, (void *) xbt_os_thread_self(), context->mutex,
+ context->cond);
+ xbt_os_cond_wait(context->cond, context->mutex);
+
+ DEBUG3("**[ctx:%p;self:%p]** Unlocking individual %p ****",
+ context, (void *) xbt_os_thread_self(), context->mutex);
+ xbt_os_mutex_unlock(context->mutex);
+
+#endif
+
+ if (context->startup_func)
+ context->startup_func(context->startup_arg);
+
+ DEBUG0("Calling the main function");
+
+ __context_exit(context, (context->code) (context->argc, context->argv));
+ return NULL;
+}
+
+#ifndef CONTEXT_THREADS
+/* callback: context fetching (used only with ucontext, os_thread deal with it for us otherwise) */
+static ex_ctx_t *_context_ex_ctx(void) {
+ return current_context->exception;
+}
+
+/* callback: termination */
+static void _context_ex_terminate(xbt_ex_t * e) {
+ xbt_ex_display(e);
+
+ abort();
+ /* FIXME: there should be a configuration variable to
+ choose to kill everyone or only this one */
+}
+#endif
+