Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New simplified API for the context factory [Cristian]
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 17 Aug 2009 09:23:53 +0000 (09:23 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 17 Aug 2009 09:23:53 +0000 (09:23 +0000)
It provides functionality for context creation/destruction, start,
stop, resume and suspend. The rest of the logic was factorized and
moved to the SIMIX layer, resulting in a simpler and cleaner
implementation.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@6561 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/simix/private.h
src/simix/smx_context.c
src/simix/smx_context_sysv.c
src/simix/smx_context_thread.c
src/simix/smx_global.c
src/simix/smx_process.c

index 48fd010..5a635c3 100644 (file)
@@ -189,43 +189,40 @@ typedef struct s_smx_context {
  */
 
 /* function used to create a new context */
-typedef int (*smx_pfn_context_factory_create_context_t) (smx_process_t *, xbt_main_func_t);
+typedef smx_context_t (*smx_pfn_context_factory_create_context_t) (xbt_main_func_t);
 
 /* function used to create the context for the maestro process */
-typedef int (*smx_pfn_context_factory_create_maestro_context_t) (smx_process_t*);
+typedef smx_context_t (*smx_pfn_context_factory_create_maestro_context_t) (void);
 
 /* this function finalize the specified context factory */
 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
 
 /* function used to destroy the specified context */
-typedef void (*smx_pfn_context_free_t) (smx_process_t);
-
-/* function used to kill the specified context */
-typedef void (*smx_pfn_context_kill_t) (smx_process_t);
-
-/* function used to resume the specified context */
-typedef void (*smx_pfn_context_schedule_t) (smx_process_t);
-
-/* function used to yield the specified context */
-typedef void (*smx_pfn_context_yield_t) (void);
+typedef void (*smx_pfn_context_free_t) (smx_context_t);
 
 /* function used to start the specified context */
-typedef void (*smx_pfn_context_start_t) (smx_process_t);
+typedef void (*smx_pfn_context_start_t) (smx_context_t);
 
 /* function used to stop the current context */
 typedef void (*smx_pfn_context_stop_t) (int);
 
+/* function used to suspend the current context */
+typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
+
+/* function used to resume the current context */
+typedef void (*smx_pfn_context_resume_t) (smx_context_t old_context, 
+                                          smx_context_t new_context);
+
 /* interface of the context factories */
 typedef struct s_smx_context_factory {
   smx_pfn_context_factory_create_maestro_context_t create_maestro_context;
   smx_pfn_context_factory_create_context_t create_context;
   smx_pfn_context_factory_finalize_t finalize;
   smx_pfn_context_free_t free;
-  smx_pfn_context_kill_t kill;
-  smx_pfn_context_schedule_t schedule;
-  smx_pfn_context_yield_t yield;
   smx_pfn_context_start_t start;
   smx_pfn_context_stop_t stop;
+  smx_pfn_context_suspend_t suspend;
+  smx_pfn_context_resume_t resume;
   const char *name;
 } s_smx_context_factory_t;
 
@@ -253,18 +250,18 @@ void SIMIX_ctx_java_factory_init(smx_context_factory_t * factory);
 /**
  * \param smx_process the simix maestro process that contains this context
  */
-static inline int SIMIX_context_create_maestro(smx_process_t *process)
+static inline smx_context_t SIMIX_context_create_maestro()
 {
-  return (*(simix_global->context_factory->create_maestro_context)) (process);
+  return (*(simix_global->context_factory->create_maestro_context)) ();
 }
 
 /**
  * \param smx_process the simix process that contains this context
  * \param code a main function
  */
-static inline int SIMIX_context_new(smx_process_t *process, xbt_main_func_t code)
+static inline smx_context_t SIMIX_context_new(xbt_main_func_t code)
 {
-    return (*(simix_global->context_factory->create_context)) (process, code);
+    return (*(simix_global->context_factory->create_context)) (code);
 }
 
 /* Scenario for the end of a context:
@@ -287,15 +284,11 @@ static inline int SIMIX_context_new(smx_process_t *process, xbt_main_func_t code
  *   At the end of it, it checks that wannadie == 1, and call smx_context_stop
  *   (same than first case afterward)
  */
-static inline void SIMIX_context_kill(smx_process_t process)
-{
-  (*(simix_global->context_factory->kill)) (process);
-}
 
 /* Argument must be stopped first -- runs in maestro context */
-static inline void SIMIX_context_free(smx_process_t process)
+static inline void SIMIX_context_free(smx_context_t context)
 {
-  (*(simix_global->context_factory->free)) (process);
+  (*(simix_global->context_factory->free)) (context);
 }
 
 /**
@@ -304,41 +297,25 @@ static inline void SIMIX_context_free(smx_process_t process)
  * Calling this function prepares \a process to be run. It will
    however run effectively only when calling #SIMIX_context_schedule
  */
-static inline void SIMIX_context_start(smx_process_t process)
+static inline void SIMIX_context_start(smx_context_t context)
 {
-  (*(simix_global->context_factory->start)) (process);
+  (*(simix_global->context_factory->start)) (context);
 }
 
-/**
- * Calling this function makes the current process yield. The process
- * that scheduled it returns from SIMIX_context_schedule as if nothing
- * had happened.
- *
- * Only the processes can call this function, giving back the control
- * to the maestro
- */
-static inline void SIMIX_context_yield(void)
+static inline void SIMIX_context_stop(int exit_code)
 {
-  (*(simix_global->context_factory->yield)) ();
+  (*(simix_global->context_factory->stop)) (exit_code);
 }
 
-/**
- * \param process to be scheduled
- *
- * Calling this function blocks the current process and schedule \a process.
- * When \a process would call SIMIX_context_yield, it will return
- * to this function as if nothing had happened.
- *
- * Only the maestro can call this function to run a given process.
- */
-static inline void SIMIX_context_schedule(smx_process_t process)
+static inline void SIMIX_context_resume(smx_context_t old_context,
+                                        smx_context_t new_context)
 {
-  (*(simix_global->context_factory->schedule)) (process);
+  (*(simix_global->context_factory->resume)) (old_context, new_context);
 }
 
-static inline void SIMIX_context_stop(int exit_code)
+static inline void SIMIX_context_suspend(smx_context_t context)
 {
-  (*(simix_global->context_factory->stop)) (exit_code);
+  (*(simix_global->context_factory->suspend)) (context);
 }
 
 #endif
index 49758bf..3b83787 100644 (file)
@@ -73,7 +73,7 @@ int SIMIX_context_select_factory(const char *name)
   SIMIX_context_init_factory_by_name(&simix_global->context_factory, name);
 
   /* maestro process specialisation */
-  (*(simix_global->context_factory->create_maestro_context)) (&simix_global->maestro_process);
+  simix_global->maestro_process->context = SIMIX_context_create_maestro ();
 
   /* the current process is the process of the maestro */
   simix_global->current_process = simix_global->maestro_process;
index 0638d97..50b5824 100644 (file)
@@ -24,7 +24,7 @@ typedef struct s_smx_ctx_sysv {
   SMX_CTX_BASE_T;
   ucontext_t uc;                /* the thread that execute the code */
   char stack[STACK_SIZE];       /* the thread stack size */
-  smx_process_t prev;           /* the previous process */
+  struct s_smx_ctx_sysv *prev;           /* the previous process */
 #ifdef HAVE_VALGRIND_VALGRIND_H
   unsigned int valgrind_stack_id;       /* the valgrind stack id */
 #endif                          
@@ -35,36 +35,24 @@ typedef struct s_smx_ctx_sysv {
 static ex_ctx_t *xbt_jcontext_ex_ctx(void);
 
 /* callback: termination */
-static void xbt_jcontext_ex_terminate(xbt_ex_t * e);
+static void xbt_jcontext_ex_terminate(xbt_ex_t *e);
 
-static int
-smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
+static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code);
 
-static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory);
+static int smx_ctx_sysv_factory_finalize(smx_context_factory_t *factory);
 
-static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t * maestro);
+static smx_context_t smx_ctx_sysv_factory_create_maestro_context(void);
 
-static void smx_ctx_sysv_free(smx_process_t process);
+static void smx_ctx_sysv_free(smx_context_t context);
 
-static void smx_ctx_sysv_kill(smx_process_t process);
-
-static void smx_ctx_sysv_schedule(smx_process_t process);
-
-static void smx_ctx_sysv_yield(void);
-
-static void smx_ctx_sysv_start(smx_process_t process);
+static void smx_ctx_sysv_start(smx_context_t context);
 
 static void smx_ctx_sysv_stop(int exit_code);
 
-static void smx_ctx_sysv_swap(smx_process_t process);
-
-static void smx_ctx_sysv_schedule(smx_process_t process);
+static void smx_ctx_sysv_suspend(smx_context_t context);
 
-static void smx_ctx_sysv_yield(void);
-
-static void smx_ctx_sysv_suspend(smx_process_t process);
-
-static void smx_ctx_sysv_resume(smx_process_t process);
+static void smx_ctx_sysv_resume(smx_context_t old_context,
+                                smx_context_t new_context);
 
 static void smx_ctx_sysv_wrapper(void);
 
@@ -81,7 +69,7 @@ static void xbt_ctx_sysv_ex_terminate(xbt_ex_t * e)
   abort();
 }
 
-void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory)
+void SIMIX_ctx_sysv_factory_init(smx_context_factory_t *factory)
 {
   *factory = xbt_new0(s_smx_context_factory_t, 1);
 
@@ -89,11 +77,10 @@ void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory)
   (*factory)->finalize = smx_ctx_sysv_factory_finalize;
   (*factory)->create_maestro_context = smx_ctx_sysv_factory_create_maestro_context;
   (*factory)->free = smx_ctx_sysv_free;
-  (*factory)->kill = smx_ctx_sysv_kill;
-  (*factory)->schedule = smx_ctx_sysv_schedule;
-  (*factory)->yield = smx_ctx_sysv_yield;
   (*factory)->start = smx_ctx_sysv_start;
   (*factory)->stop = smx_ctx_sysv_stop;
+  (*factory)->suspend = smx_ctx_sysv_suspend;
+  (*factory)->resume = smx_ctx_sysv_resume;
   (*factory)->name = "smx_sysv_context_factory";
 
   /* context exception handlers */
@@ -101,17 +88,14 @@ void SIMIX_ctx_sysv_factory_init(smx_context_factory_t * factory)
   __xbt_ex_terminate = xbt_ctx_sysv_ex_terminate;
 }
 
-static int smx_ctx_sysv_factory_create_maestro_context(smx_process_t *maestro)
+static smx_context_t smx_ctx_sysv_factory_create_maestro_context()
 {
   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
 
-  context->exception = xbt_new(ex_ctx_t, 1);
+  context->exception = xbt_new(ex_ctx_t, 1);  
   XBT_CTX_INITIALIZE(context->exception);
 
-  (*maestro)->context = (smx_context_t) context;
-
-  return 0;
-
+  return (smx_context_t)context;
 }
 
 static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
@@ -122,10 +106,8 @@ static int smx_ctx_sysv_factory_finalize(smx_context_factory_t * factory)
   return 0;
 }
 
-static int
-smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
+static smx_context_t smx_ctx_sysv_factory_create_context(xbt_main_func_t code)
 {
-  VERB1("Create context %s", (*smx_process)->name);
   smx_ctx_sysv_t context = xbt_new0(s_smx_ctx_sysv_t, 1);
 
   context->code = code;
@@ -146,16 +128,13 @@ smx_ctx_sysv_factory_create_context(smx_process_t *smx_process, xbt_main_func_t
 
   context->exception = xbt_new(ex_ctx_t, 1);
   XBT_CTX_INITIALIZE(context->exception);
-  (*smx_process)->context = (smx_context_t)context;
-  (*smx_process)->iwannadie = 0;
-
-  /* FIXME: Check what should return */
-  return 1;
+  
+  return (smx_context_t)context;
 }
 
-static void smx_ctx_sysv_free(smx_process_t process)
+static void smx_ctx_sysv_free(smx_context_t pcontext)
 {
-  smx_ctx_sysv_t context = (smx_ctx_sysv_t)process->context;   
+  smx_ctx_sysv_t context = (smx_ctx_sysv_t)pcontext;   
   if (context){
 
     if (context->exception)
@@ -170,113 +149,49 @@ static void smx_ctx_sysv_free(smx_process_t process)
   }
 }
 
-static void smx_ctx_sysv_kill(smx_process_t process)
-{
-  DEBUG2("Kill process '%s' (from '%s')", process->name,
-         simix_global->current_process->name);
-  process->iwannadie = 1;
-  smx_ctx_sysv_swap(process);
-}
-
-/** 
- * \param context the winner
- *
- * Calling this function blocks the current context and schedule \a context.  
- * When \a context will call xbt_context_yield, it will return
- * to this function as if nothing had happened.
- * 
- * Only the maestro can call this function to run a given process.
- */
-static void smx_ctx_sysv_schedule(smx_process_t process)
+static void smx_ctx_sysv_start(smx_context_t context)
 {
-  DEBUG1("Schedule process '%s'", process->name);
-  xbt_assert0((simix_global->current_process == simix_global->maestro_process),
-              "You are not supposed to run this function here!");
-  smx_ctx_sysv_swap(process);
-}
-
-/** 
- * Calling this function makes the current context yield. The context
- * that scheduled it returns from xbt_context_schedule as if nothing
- * had happened.
- * 
- * Only the processes can call this function, giving back the control
- * to the maestro
- */
-static void smx_ctx_sysv_yield(void)
-{
-  DEBUG1("Yielding process '%s'", simix_global->current_process->name);
-  xbt_assert0((simix_global->current_process != simix_global->maestro_process),
-              "You are not supposed to run this function here!");
-  smx_ctx_sysv_swap(simix_global->current_process);
-}
-
-static void smx_ctx_sysv_start(smx_process_t process)
-{
-  /*DEBUG1("Start context '%s'", context->name);*/
-  makecontext(&(((smx_ctx_sysv_t) process->context)->uc), smx_ctx_sysv_wrapper, 0);
+  makecontext(&((smx_ctx_sysv_t)context)->uc, smx_ctx_sysv_wrapper, 0);
 }
 
 static void smx_ctx_sysv_stop(int exit_code)
 {
-  /* please no debug here: our procdata was already free'd */
   if (simix_global->current_process->cleanup_func)
-    ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
-
-  smx_ctx_sysv_swap(simix_global->current_process);
-}
-
-static void smx_ctx_sysv_swap(smx_process_t process)
-{
-  DEBUG2("Swap context: '%s' -> '%s'", simix_global->current_process->name, process->name);
-  xbt_assert0(simix_global->current_process, "You have to call context_init() first.");
-  xbt_assert0(process, "Invalid argument");
-
-  if (((smx_ctx_sysv_t) process->context)->prev == NULL)
-    smx_ctx_sysv_resume(process);
-  else
-    smx_ctx_sysv_suspend(process);
+    ((*simix_global->current_process->cleanup_func))
+      (simix_global->current_process->cleanup_arg);
 
-  if (simix_global->current_process->iwannadie)
-    smx_ctx_sysv_stop(1);
+  smx_ctx_sysv_suspend(simix_global->current_process->context);
 }
 
 static void smx_ctx_sysv_wrapper(void)
 {
   smx_ctx_sysv_stop((*(simix_global->current_process->context->code))
-                    (simix_global->current_process->argc, simix_global->current_process->argv));
+                        (simix_global->current_process->argc, 
+                         simix_global->current_process->argv));
 }
 
-static void smx_ctx_sysv_suspend(smx_process_t process)
+static void smx_ctx_sysv_suspend(smx_context_t context)
 {
   int rv;
 
-  DEBUG1("Suspend context: '%s'", simix_global->current_process->name);
-  smx_process_t prev_process = ((smx_ctx_sysv_t) process->context)->prev;
+  smx_ctx_sysv_t prev_context = ((smx_ctx_sysv_t) context)->prev;
 
-  simix_global->current_process = prev_process;
+  ((smx_ctx_sysv_t) context)->prev = NULL;
 
-  ((smx_ctx_sysv_t) process->context)->prev = NULL;
-
-  rv = swapcontext(&(((smx_ctx_sysv_t) process->context)->uc), &(((smx_ctx_sysv_t)prev_process->context)->uc));
+  rv = swapcontext(&((smx_ctx_sysv_t) context)->uc, &prev_context->uc);
 
   xbt_assert0((rv == 0), "Context swapping failure");
 }
 
-static void smx_ctx_sysv_resume(smx_process_t process)
+static void smx_ctx_sysv_resume(smx_context_t old_context,
+                                smx_context_t new_context)
 {
   int rv;
-  smx_ctx_sysv_t new_context = (smx_ctx_sysv_t)process->context;
-  smx_ctx_sysv_t prev_context = (smx_ctx_sysv_t)simix_global->current_process->context;
-
-  DEBUG2("Resume context: '%s' (from '%s')", process->name,
-         simix_global->current_process->name);
-
-  ((smx_ctx_sysv_t) process->context)->prev = simix_global->current_process;
 
-  simix_global->current_process = process;
+  ((smx_ctx_sysv_t) new_context)->prev = (smx_ctx_sysv_t)old_context;
 
-  rv = swapcontext(&prev_context->uc, &new_context->uc);
+  rv = swapcontext(&((smx_ctx_sysv_t)old_context)->uc,
+                   &((smx_ctx_sysv_t)new_context)->uc);
 
   xbt_assert0((rv == 0), "Context swapping failure");
 }
index b3b5ffa..5647ffd 100644 (file)
@@ -23,35 +23,22 @@ typedef struct s_smx_ctx_thread {
   xbt_os_sem_t end;             /* this semaphore is used to schedule/unschedule the process   */
 } s_smx_ctx_thread_t, *smx_ctx_thread_t;
 
-static int
-smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code);
+static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code);
 
-static int
-smx_ctx_thread_factory_create_master_context(smx_process_t * maestro);
+static smx_context_t smx_ctx_thread_factory_create_master_context(void);
 
 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory);
 
-static void smx_ctx_thread_free(smx_process_t process);
+static void smx_ctx_thread_free(smx_context_t context);
 
-static void smx_ctx_thread_kill(smx_process_t process);
-
-static void smx_ctx_thread_schedule(smx_process_t process);
-
-static void smx_ctx_thread_yield(void);
-
-static void smx_ctx_thread_start(smx_process_t process);
+static void smx_ctx_thread_start(smx_context_t context);
 
 static void smx_ctx_thread_stop(int exit_code);
 
-static void smx_ctx_thread_swap(smx_process_t process);
-
-static void smx_ctx_thread_schedule(smx_process_t process);
-
-static void smx_ctx_thread_yield(void);
+static void smx_ctx_thread_suspend(smx_context_t context);
 
-static void smx_ctx_thread_suspend(smx_process_t process);
-
-static void smx_ctx_thread_resume(smx_process_t process);
+static void smx_ctx_thread_resume(smx_context_t old_context,
+                                  smx_context_t new_context);
 
 static void *smx_ctx_thread_wrapper(void *param);
 
@@ -63,19 +50,16 @@ void SIMIX_ctx_thread_factory_init(smx_context_factory_t * factory)
   (*factory)->finalize = smx_ctx_thread_factory_finalize;
   (*factory)->create_maestro_context = smx_ctx_thread_factory_create_master_context;
   (*factory)->free = smx_ctx_thread_free;
-  (*factory)->kill = smx_ctx_thread_kill;
-  (*factory)->schedule = smx_ctx_thread_schedule;
-  (*factory)->yield = smx_ctx_thread_yield;
   (*factory)->start = smx_ctx_thread_start;
   (*factory)->stop = smx_ctx_thread_stop;
+  (*factory)->suspend = smx_ctx_thread_suspend;
+  (*factory)->resume = smx_ctx_thread_resume;
   (*factory)->name = "ctx_thread_factory";
 }
 
-static int
-smx_ctx_thread_factory_create_master_context(smx_process_t * maestro)
+static smx_context_t smx_ctx_thread_factory_create_master_context(void)
 {
-  (*maestro)->context = (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
-  return 0;
+  return (smx_context_t) xbt_new0(s_smx_ctx_thread_t, 1);
 }
 
 static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
@@ -85,26 +69,20 @@ static int smx_ctx_thread_factory_finalize(smx_context_factory_t * factory)
   return 0;
 }
 
-static int
-smx_ctx_thread_factory_create_context(smx_process_t *smx_process, xbt_main_func_t code)
+static smx_context_t smx_ctx_thread_factory_create_context(xbt_main_func_t code)
 {
   smx_ctx_thread_t context = xbt_new0(s_smx_ctx_thread_t, 1);
 
-  VERB1("Create context %s", (*smx_process)->name);
   context->code = code;
   context->begin = xbt_os_sem_init(0);
   context->end = xbt_os_sem_init(0);
 
-  (*smx_process)->context = (smx_context_t)context;
-  (*smx_process)->iwannadie = 0;
-
-  /* FIXME: Check what should return */
-  return 1;
+  return (smx_context_t)context;
 }
 
-static void smx_ctx_thread_free(smx_process_t process)
+static void smx_ctx_thread_free(smx_context_t pcontext)
 {
-  smx_ctx_thread_t context = (smx_ctx_thread_t)process->context;
+  smx_ctx_thread_t context = (smx_ctx_thread_t)pcontext;
 
   /* Check if this is the context of maestro (it doesn't has a real thread) */  
   if (context->thread) {
@@ -121,53 +99,15 @@ static void smx_ctx_thread_free(smx_process_t process)
   free(context);
 }
 
-static void smx_ctx_thread_kill(smx_process_t process)
-{
-  DEBUG1("Kill process '%s'", process->name);
-  process->iwannadie = 1;
-  //smx_ctx_thread_swap(process);
-}
-
-/** 
- * \param context the winner
- *
- * Calling this function blocks the current context and schedule \a context.  
- * When \a context will call smx_context_yield, it will return
- * to this function as if nothing had happened.
- * 
- * Only the maestro can call this function to run a given process.
- */
-static void smx_ctx_thread_schedule(smx_process_t process)
-{
-  DEBUG1("Schedule process '%s'", process->name);
-  /*xbt_assert0((simix_global->current_process == simix_global->maestro_process),
-              "You are not supposed to run this function here!");*/
-  smx_ctx_thread_resume(process);
-  /*smx_ctx_thread_swap(process);*/
-}
-
-/** 
- * Calling this function makes the current context yield. The context
- * that scheduled it returns from smx_context_schedule as if nothing
- * had happened.
- * 
- * Only the processes can call this function, giving back the control
- * to the maestro
- */
-static void smx_ctx_thread_yield(void)
-{
-  smx_ctx_thread_suspend(simix_global->current_process);
-}
-
-static void smx_ctx_thread_start(smx_process_t process)
+static void smx_ctx_thread_start(smx_context_t context)
 {
-  smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t) process->context;
+  smx_ctx_thread_t ctx_thread = (smx_ctx_thread_t)context;
 
-  DEBUG1("Start context '%s'", process->name);
   /* create and start the process */
+  /* NOTE: The first argument to xbt_os_thread_create used to be the process *
+   * name, but now the name is stored at simix level, so we pass a null      */
   ctx_thread->thread =
-    xbt_os_thread_create(process->name, smx_ctx_thread_wrapper,
-                         ctx_thread);
+    xbt_os_thread_create(NULL, smx_ctx_thread_wrapper, ctx_thread);
 
   /* wait the starting of the newly created process */
   xbt_os_sem_acquire(ctx_thread->end);
@@ -177,7 +117,7 @@ static void smx_ctx_thread_stop(int exit_code)
 {
   /* please no debug here: our procdata was already free'd */
   if (simix_global->current_process->cleanup_func)
-    ((*simix_global->current_process->cleanup_func)) (simix_global->current_process->cleanup_arg);
+    (*simix_global->current_process->cleanup_func) (simix_global->current_process->cleanup_arg);
 
   /* signal to the maestro that it has finished */
   xbt_os_sem_release(((smx_ctx_thread_t) simix_global->current_process->context)->end);
@@ -186,12 +126,6 @@ static void smx_ctx_thread_stop(int exit_code)
   xbt_os_thread_exit(NULL);     /* We should provide return value in case other wants it */
 }
 
-/*FIXME: erase this function*/
-static void smx_ctx_thread_swap(smx_process_t process)
-{
-  return;
-}
-
 static void *smx_ctx_thread_wrapper(void *param)
 {
   smx_ctx_thread_t context = (smx_ctx_thread_t) param;
@@ -205,16 +139,15 @@ static void *smx_ctx_thread_wrapper(void *param)
   return NULL;
 }
 
-static void smx_ctx_thread_suspend(smx_process_t process)
+static void smx_ctx_thread_suspend(smx_context_t context)
 {
-  DEBUG1("Suspend context '%s'", process->name);
-  xbt_os_sem_release(((smx_ctx_thread_t) process->context)->end);
-  xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->begin);
+  xbt_os_sem_release(((smx_ctx_thread_t) context)->end);
+  xbt_os_sem_acquire(((smx_ctx_thread_t) context)->begin);
 }
 
-static void smx_ctx_thread_resume(smx_process_t process)
+static void smx_ctx_thread_resume(smx_context_t not_used, 
+                                  smx_context_t new_context)
 {
-  DEBUG1("Resume context '%s'", process->name);
-  xbt_os_sem_release(((smx_ctx_thread_t) process->context)->begin);
-  xbt_os_sem_acquire(((smx_ctx_thread_t) process->context)->end);
+  xbt_os_sem_release(((smx_ctx_thread_t) new_context)->begin);
+  xbt_os_sem_acquire(((smx_ctx_thread_t) new_context)->end);
 }
\ No newline at end of file
index 8f79991..bc2f6f4 100644 (file)
@@ -237,8 +237,8 @@ void SIMIX_clean(void)
   xbt_dict_free(&(simix_global->host));
 
   /* Let's free maestro now */
-  SIMIX_context_free(simix_global->maestro_process);
-  free(simix_global->maestro_process);
+  SIMIX_context_free(simix_global->maestro_process->context);
+  free(simix_global->maestro_process);  
 
   /* Finish context module and SURF */
   SIMIX_context_mod_exit();
index c62f822..80fac61 100644 (file)
@@ -58,7 +58,6 @@ void SIMIX_process_empty_trash(void)
  * \brief Creates and runs the maestro process
  *
  */
-
 void __SIMIX_create_maestro_process()
 {
   smx_process_t process = NULL;
@@ -67,8 +66,8 @@ void __SIMIX_create_maestro_process()
   /* Process data */
   process->name = (char *)"";
 
-  /*Create the right context type (FIXME: check the return value for success)*/
-  SIMIX_context_create_maestro(&process);
+  /* Create the right context type */
+  process->context = SIMIX_context_create_maestro();
 
   /* Set it as the maestro process */
   simix_global->maestro_process = process;
@@ -83,7 +82,7 @@ void __SIMIX_create_maestro_process()
  * A constructor for #m_process_t taking four arguments and returning the corresponding object. The structure (and the corresponding thread) is created, and put in the list of ready process.
  *
  * \param name a name for the object. It is for user-level information and can be NULL.
-* \param data a pointer to any data one may want to attach to the new object.  It is for user-level information and can be NULL. It can be retrieved with the function \ref MSG_process_get_data.
+ * \param data a pointer to any data one may want to attach to the new object.  It is for user-level information and can be NULL. It can be retrieved with the function \ref MSG_process_get_data.
  * \param host the location where the new agent is executed.
  * \param argc first argument passed to \a code
  * \param argv second argument passed to \a code
@@ -116,9 +115,10 @@ smx_process_t SIMIX_process_create(const char *name,
   process->argv = argv;
   process->mutex = NULL;
   process->cond = NULL;
+  process->iwannadie = 0;
 
-  /*Create the right context type (FIXME: check the return value for success)*/
-  SIMIX_context_new(&process, code);
+  VERB1("Create context %s", process->name);
+  process->context = SIMIX_context_new(code);
 
   process->data = data;
   process->cleanup_func = simix_global->cleanup_process_function;
@@ -129,8 +129,9 @@ smx_process_t SIMIX_process_create(const char *name,
 
   /* Add the process to it's host process list */
   xbt_swag_insert(process, host->process_list);
-  
-  SIMIX_context_start(process);
+
+  DEBUG1("Start context '%s'", process->name);
+  SIMIX_context_start(process->context);
    
   /* Now insert it in the global process list and in the process to run list */
   xbt_swag_insert(process, simix_global->process_list);
@@ -177,7 +178,7 @@ void SIMIX_jprocess_create(const char *name, smx_host_t host,
   process->argv = NULL;
   process->mutex = NULL;
   process->cond = NULL;
-  SIMIX_context_new(&process, jprocess);
+  SIMIX_context_new(jprocess);
   process->data = data;
 
   /* Add the process to it's host process list */
@@ -185,7 +186,7 @@ void SIMIX_jprocess_create(const char *name, smx_host_t host,
 
   /* fix current_process, about which xbt_context_start mocks around */
   self = simix_global->current_process;
-  SIMIX_context_start(process);
+  SIMIX_context_start(process->context);
   simix_global->current_process = self;
 
   /* Now insert it in the global process list and in the process to run list */
@@ -213,14 +214,14 @@ void SIMIX_process_kill(smx_process_t process)
 
   DEBUG2("%p here! killing %p", simix_global->current_process, process);
 
-  /*FIXME: If we are killing the running process we might call stop directly */  
   process->iwannadie = 1;
-  __SIMIX_process_schedule(process);
 
-  if (process == SIMIX_process_self()) {
-    /* I just killed myself */
-    SIMIX_context_yield();
-  }
+  /* If I'm killing myself then stop otherwise schedule the process to kill */
+  if (process == SIMIX_process_self())
+    SIMIX_context_stop(1);
+  else
+    __SIMIX_process_schedule(process);
+  
 }
 
 /**
@@ -453,25 +454,25 @@ void __SIMIX_process_yield(void)
   xbt_assert0((simix_global->current_process != simix_global->maestro_process),
               "You are not supposed to run this function here!");
 
-  SIMIX_context_yield();
+  SIMIX_context_suspend(simix_global->current_process->context);
 
   if (simix_global->current_process->iwannadie)
     SIMIX_context_stop(1);
 }
 
-void __SIMIX_process_schedule(smx_process_t process)
+void __SIMIX_process_schedule(smx_process_t new_process)
 {
-  DEBUG1("Scheduling context: '%s'", process->name);
+  DEBUG1("Scheduling context: '%s'", new_process->name);
 
   /* save the current process */
-  smx_process_t self = simix_global->current_process;
+  smx_process_t old_process = simix_global->current_process;
 
   /* update the current process */
-  simix_global->current_process = process;
+  simix_global->current_process = new_process;
 
   /* schedule the context */
-  SIMIX_context_schedule(process);
+  SIMIX_context_resume(old_process->context, new_process->context);
 
   /* restore the current process to the previously saved process */
-  simix_global->current_process = self;
+  simix_global->current_process = old_process;
 }