+
+/*******************************/
+/* Object creation/destruction */
+/*******************************/
+/**
+ * \param code a main function
+ * \param startup_func a function to call when running the context for
+ * the first time and just before the main function \a code
+ * \param startup_arg the argument passed to the previous function (\a startup_func)
+ * \param cleanup_func a function to call when running the context, just after
+ the termination of the main function \a code
+ * \param cleanup_arg the argument passed to the previous function (\a cleanup_func)
+ * \param argc first argument of function \a code
+ * \param argv seconde argument of function \a code
+ */
+xbt_context_t
+xbt_context_new(const char *name,
+ xbt_main_func_t code,
+ void_f_pvoid_t startup_func,
+ void *startup_arg,
+ void_f_pvoid_t cleanup_func,
+ void *cleanup_arg, int argc, char *argv[]
+ )
+{
+ xbt_context_t res = NULL;
+
+ res = xbt_new0(s_xbt_context_t, 1);
+
+ res->code = code;
+ res->name = xbt_strdup(name);
+
+#ifdef CONTEXT_THREADS
+ /*
+ * initialize the semaphores used to schedule/yield
+ * the process associated to the newly created context
+ */
+ res->begin = xbt_os_sem_init(0);
+ res->end = xbt_os_sem_init(0);
+#else
+
+ 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); */
+ /* WARNING : when this context is over, the current_context (i.e. the
+ father), is awaken... Theorically, the wrapper should prevent using
+ this feature. */
+ res->uc.uc_stack.ss_sp = pth_skaddr_makecontext(res->stack, STACK_SIZE);
+ res->uc.uc_stack.ss_size = pth_sksize_makecontext(res->stack, STACK_SIZE);
+
+ res->exception = xbt_new(ex_ctx_t, 1);
+ XBT_CTX_INITIALIZE(res->exception);
+#endif /* CONTEXT_THREADS or not */
+
+ res->iwannadie = 0; /* useless but makes valgrind happy */
+
+ res->argc = argc;
+ res->argv = argv;
+ res->startup_func = startup_func;
+ res->startup_arg = startup_arg;
+ res->cleanup_func = cleanup_func;
+ res->cleanup_arg = cleanup_arg;
+
+ xbt_swag_insert(res, context_living);
+
+ return res;
+}
+
+/* Scenario for the end of a context:
+ *
+ * CASE 1: death after end of function
+ * __context_wrapper, called by os thread, calls xbt_context_stop after user code stops
+ * xbt_context_stop calls user cleanup_func if any (in context settings),
+ * add current to trashbin
+ * yields back to maestro (destroy os thread on need)
+ * From time to time, maestro calls xbt_context_empty_trash,
+ * which maps xbt_context_free on the content
+ * xbt_context_free frees some more memory,
+ * joins os thread
+ *
+ * CASE 2: brutal death
+ * xbt_context_kill (from any context)
+ * set context->wannadie to 1
+ * yields to the context
+ * the context is awaken in the middle of __yield.
+ * At the end of it, it checks that wannadie == 1, and call xbt_context_stop
+ * (same than first case afterward)
+ */
+
+
+/* Argument must be stopped first -- runs in maestro context */
+static void xbt_context_free(xbt_context_t context)