Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Storage parsing skip empty line
[simgrid.git] / src / simix / smx_process.c
1 /* Copyright (c) 2007-2012. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 unsigned long simix_process_maxpid = 0;
17
18 /**
19  * \brief Returns the current agent.
20  *
21  * This functions returns the currently running SIMIX process.
22  *
23  * \return The SIMIX process
24  */
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
26 {
27   smx_context_t self_context = SIMIX_context_self();
28
29   return self_context ? SIMIX_context_get_data(self_context) : NULL;
30 }
31
32 /**
33  * \brief Returns whether a process has pending asynchronous communications.
34  * \return true if there are asynchronous communications in this process
35  */
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
37
38   return xbt_fifo_size(process->comms) > 0;
39 }
40
41 void SIMIX_pre_process_cleanup(smx_simcall_t simcall, smx_process_t process) {
42   SIMIX_process_cleanup(process);
43 }
44 /**
45  * \brief Moves a process to the list of processes to destroy.
46  */
47 void SIMIX_process_cleanup(smx_process_t process)
48 {
49   XBT_DEBUG("Cleanup process %s (%p), waiting action %p",
50       process->name, process, process->waiting_action);
51
52   /* cancel non-blocking communications */
53   smx_action_t action;
54   while ((action = xbt_fifo_pop(process->comms))) {
55
56     /* make sure no one will finish the comm after this process is destroyed,
57      * because src_proc or dst_proc would be an invalid pointer */
58     SIMIX_comm_cancel(action);
59
60     if (action->comm.src_proc == process) {
61       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
62           action, action->comm.detached, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
63       action->comm.src_proc = NULL;
64
65       if (action->comm.detached) {
66          if (action->comm.refcount == 0) {
67            XBT_DEBUG("Increase the refcount before destroying it since it's detached");
68            /* I'm not supposed to destroy a detached comm from the sender side,
69             * unless there is no receiver matching the rdv */
70            action->comm.refcount++;
71            SIMIX_comm_destroy(action);
72          }
73          else {
74            XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
75          }
76       } else {
77         SIMIX_comm_destroy(action);
78       }
79     }
80     else if (action->comm.dst_proc == process){
81       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
82           action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
83       action->comm.dst_proc = NULL;
84
85       if (action->comm.detached && action->comm.refcount == 1
86           && action->comm.src_proc != NULL) {
87         /* the comm will be freed right now, remove it from the sender */
88         xbt_fifo_remove(action->comm.src_proc->comms, action);
89       }
90       SIMIX_comm_destroy(action);
91     }
92     else {
93       xbt_die("Communication action %p is in my list but I'm not the sender "
94           "or the receiver", action);
95     }
96   }
97
98   /*xbt_swag_remove(process, simix_global->process_to_run);*/
99   xbt_swag_remove(process, simix_global->process_list);
100   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
101   xbt_swag_insert(process, simix_global->process_to_destroy);
102   process->context->iwannadie = 0;
103 }
104
105 /** 
106  * Garbage collection
107  *
108  * Should be called some time to time to free the memory allocated for processes
109  * that have finished (or killed).
110  */
111 void SIMIX_process_empty_trash(void)
112 {
113   smx_process_t process = NULL;
114
115   while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
116     SIMIX_context_free(process->context);
117
118     /* Free the exception allocated at creation time */
119     free(process->running_ctx);
120     xbt_dict_free(&process->properties);
121
122     xbt_fifo_free(process->comms);
123
124     xbt_dynar_free(&process->on_exit);
125
126     free(process->name);
127     free(process);
128   }
129 }
130
131 /**
132  * \brief Creates and runs the maestro process
133  */
134 void SIMIX_create_maestro_process()
135 {
136   smx_process_t maestro = NULL;
137
138   /* Create maestro process and intilialize it */
139   maestro = xbt_new0(s_smx_process_t, 1);
140   maestro->pid = simix_process_maxpid++;
141   maestro->name = (char *) "";
142   maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
143   XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
144   maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
145   maestro->simcall.issuer = maestro;
146
147   simix_global->maestro_process = maestro;
148   return;
149 }
150 /**
151  * \brief Stops a process.
152  *
153  * Stops the process, execute all the registered on_exit functions,
154  * register it to the list of the process to restart if needed
155  * and stops its context.
156  */
157 void SIMIX_process_stop(smx_process_t arg) {
158   /* execute the on_exit functions */
159   SIMIX_process_on_exit_runall(arg);
160   /* Add the process to the list of process to restart, only if
161    * the host is down
162    */
163   if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
164     SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
165                                         sg_host_name(arg->smx_host),
166                                         arg->kill_time,
167                                         arg->argc,arg->argv,arg->properties,
168                                         arg->auto_restart);
169   }
170   XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
171   /* stop the context */
172   SIMIX_context_stop(arg->context);
173 }
174
175 /**
176  * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
177  * This function frees the argument.
178  * \return the process created
179  */
180 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
181
182   smx_process_t process;
183   simix_global->create_process_function(
184       &process,
185       args->name,
186       args->code,
187       args->data,
188       args->hostname,
189       args->kill_time,
190       args->argc,
191       args->argv,
192       args->properties,
193       args->auto_restart);
194   xbt_free(args);
195   return process;
196 }
197
198
199 void SIMIX_pre_process_create(smx_simcall_t simcall,
200                           smx_process_t *process,
201                           const char *name,
202                           xbt_main_func_t code,
203                           void *data,
204                           const char *hostname,
205                           double kill_time,
206                           int argc, char **argv,
207                           xbt_dict_t properties,
208                           int auto_restart){
209   return SIMIX_process_create(process, name, code, data, hostname,
210                               kill_time, argc, argv, properties, auto_restart);
211 }
212 /**
213  * \brief Internal function to create a process.
214  *
215  * This function actually creates the process.
216  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
217  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
218  *
219  * \return the process created
220  */
221 void SIMIX_process_create(smx_process_t *process,
222                           const char *name,
223                           xbt_main_func_t code,
224                           void *data,
225                           const char *hostname,
226                           double kill_time,
227                           int argc, char **argv,
228                           xbt_dict_t properties,
229                           int auto_restart) {
230
231   *process = NULL;
232   smx_host_t host = SIMIX_host_get_by_name(hostname);
233
234   XBT_DEBUG("Start process %s on host '%s'", name, hostname);
235
236   if (!SIMIX_host_get_state(host)) {
237     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
238           hostname);
239   }
240   else {
241     *process = xbt_new0(s_smx_process_t, 1);
242
243     xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
244     /* Process data */
245     (*process)->pid = simix_process_maxpid++;
246     (*process)->name = xbt_strdup(name);
247     (*process)->smx_host = host;
248     (*process)->data = data;
249     (*process)->comms = xbt_fifo_new();
250     (*process)->simcall.issuer = *process;
251     
252     /* Process data for auto-restart */
253     (*process)->auto_restart = auto_restart;
254     (*process)->code = code;
255     (*process)->argc = argc;
256     (*process)->argv = argv;
257     (*process)->kill_time = kill_time;
258
259
260     XBT_VERB("Create context %s", (*process)->name);
261     (*process)->context = SIMIX_context_new(code, argc, argv,
262       simix_global->cleanup_process_function, *process);
263
264     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
265     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
266
267     /* Add properties */
268     (*process)->properties = properties;
269
270     /* Add the process to it's host process list */
271     xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
272
273     XBT_DEBUG("Start context '%s'", (*process)->name);
274
275     /* Now insert it in the global process list and in the process to run list */
276     xbt_swag_insert(*process, simix_global->process_list);
277     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
278     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
279   }
280
281   if (kill_time > SIMIX_get_clock()) {
282     if (simix_global->kill_process_function) {
283       XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
284           sg_host_name((*process)->smx_host), kill_time);
285       SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
286     }
287   }
288 }
289
290 /**
291  * \brief Executes the processes from simix_global->process_to_run.
292  *
293  * The processes of simix_global->process_to_run are run (in parallel if
294  * possible).  On exit, simix_global->process_to_run is empty, and
295  * simix_global->process_that_ran contains the list of processes that just ran.
296  * The two lists are swapped so, be careful when using them before and after a
297  * call to this function.
298  */
299 void SIMIX_process_runall(void)
300 {
301   SIMIX_context_runall();
302
303   xbt_dynar_t tmp = simix_global->process_that_ran;
304   simix_global->process_that_ran = simix_global->process_to_run;
305   simix_global->process_to_run = tmp;
306   xbt_dynar_reset(simix_global->process_to_run);
307 }
308
309 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
310   SIMIX_process_kill(process, simcall->issuer);
311 }
312 /**
313  * \brief Internal function to kill a SIMIX process.
314  *
315  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
316  * or directly for SIMIX internal purposes.
317  *
318  * \param process poor victim
319  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
320  */
321 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
322
323   XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
324
325   process->context->iwannadie = 1;
326   process->blocked = 0;
327   process->suspended = 0;
328   /* FIXME: set doexception to 0 also? */
329
330   /* destroy the blocking action if any */
331   if (process->waiting_action) {
332
333     switch (process->waiting_action->type) {
334
335       case SIMIX_ACTION_EXECUTE:          
336       case SIMIX_ACTION_PARALLEL_EXECUTE:
337         SIMIX_host_execution_destroy(process->waiting_action);
338         break;
339
340       case SIMIX_ACTION_COMMUNICATE:
341         xbt_fifo_remove(process->comms, process->waiting_action);
342         SIMIX_comm_cancel(process->waiting_action);
343         break;
344
345         case SIMIX_ACTION_SLEEP:
346           SIMIX_process_sleep_destroy(process->waiting_action);
347           break;
348
349         case SIMIX_ACTION_SYNCHRO:
350           SIMIX_synchro_stop_waiting(process, &process->simcall);
351           SIMIX_synchro_destroy(process->waiting_action);
352           break;
353
354         case SIMIX_ACTION_IO:
355           SIMIX_io_destroy(process->waiting_action);
356           break;
357
358         /* **************************************/
359         /* TUTORIAL: New API                    */
360         case SIMIX_ACTION_NEW_API:
361           SIMIX_new_api_destroy(process->waiting_action);
362           break;
363         /* **************************************/
364
365     }
366   }
367   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
368     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
369   }
370
371 }
372
373 void SIMIX_pre_process_killall(smx_simcall_t simcall) {
374   SIMIX_process_killall(simcall->issuer);
375 }
376 /**
377  * \brief Kills all running processes.
378  * \param issuer this one will not be killed
379  */
380 void SIMIX_process_killall(smx_process_t issuer)
381 {
382   smx_process_t p = NULL;
383
384   while ((p = xbt_swag_extract(simix_global->process_list))) {
385     if (p != issuer) {
386       SIMIX_process_kill(p,issuer);
387     }
388   }
389
390   SIMIX_context_runall();
391
392   SIMIX_process_empty_trash();
393 }
394
395 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
396                                    smx_host_t dest)
397 {
398   process->new_host = dest;
399 }
400 void SIMIX_process_change_host(smx_process_t process,
401              smx_host_t dest)
402 {
403   xbt_assert((process != NULL), "Invalid parameters");
404   xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
405   process->smx_host = dest;
406   xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
407 }
408
409
410 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
411 {
412   smx_action_t action_suspend =
413       SIMIX_process_suspend(process, simcall->issuer);
414
415   if (process != simcall->issuer) {
416     SIMIX_simcall_answer(simcall);
417   } else {
418     xbt_fifo_push(action_suspend->simcalls, simcall);
419     process->waiting_action = action_suspend;
420     SIMIX_host_execution_suspend(process->waiting_action);
421   }
422   /* If we are suspending ourselves, then just do not finish the simcall now */
423 }
424
425 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
426 {
427   xbt_assert((process != NULL), "Invalid parameters");
428
429   if (process->suspended) {
430     XBT_DEBUG("Process '%s' is already suspended", process->name);
431     return NULL;
432   }
433
434   process->suspended = 1;
435
436   /* If we are suspending another process, and it is waiting on an action,
437      suspend its action. */
438   if (process != issuer) {
439
440     if (process->waiting_action) {
441
442       switch (process->waiting_action->type) {
443
444         case SIMIX_ACTION_EXECUTE:
445         case SIMIX_ACTION_PARALLEL_EXECUTE:
446           SIMIX_host_execution_suspend(process->waiting_action);
447           break;
448
449         case SIMIX_ACTION_COMMUNICATE:
450           SIMIX_comm_suspend(process->waiting_action);
451           break;
452
453         case SIMIX_ACTION_SLEEP:
454           SIMIX_process_sleep_suspend(process->waiting_action);
455           break;
456
457         case SIMIX_ACTION_SYNCHRO:
458           /* Suspension is delayed to when the process is rescheduled. */
459           break;
460
461         default:
462           xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
463               (int)process->waiting_action->type);
464       }
465       return NULL;
466     } else {
467       /* Suspension is delayed to when the process is rescheduled. */
468       return NULL;
469     }
470   } else {
471     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0);
472   }
473 }
474
475 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
476   SIMIX_process_resume(process, simcall->issuer);
477 }
478
479 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
480 {
481   xbt_assert((process != NULL), "Invalid parameters");
482
483   XBT_IN("process = %p, issuer = %p", process, issuer);
484
485   if(process->context->iwannadie) {
486     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
487     return;
488   }
489
490   if(!process->suspended) return;
491   process->suspended = 0;
492
493   /* If we are resuming another process, resume the action it was waiting for
494      if any. Otherwise add it to the list of process to run in the next round. */
495   if (process != issuer) {
496
497     if (process->waiting_action) {
498
499       switch (process->waiting_action->type) {
500
501         case SIMIX_ACTION_EXECUTE:          
502         case SIMIX_ACTION_PARALLEL_EXECUTE:
503           SIMIX_host_execution_resume(process->waiting_action);
504           break;
505
506         case SIMIX_ACTION_COMMUNICATE:
507           SIMIX_comm_resume(process->waiting_action);
508           break;
509
510         case SIMIX_ACTION_SLEEP:
511           SIMIX_process_sleep_resume(process->waiting_action);
512           break;
513
514         case SIMIX_ACTION_SYNCHRO:
515           /* I cannot resume it now. This is delayed to when the process is rescheduled at
516            * the end of the synchro. */
517           break;
518
519         default:
520           xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
521               (int)process->waiting_action->type);
522       }
523     }
524   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
525
526   XBT_OUT();
527 }
528
529 int SIMIX_process_get_maxpid(void) {
530   return simix_process_maxpid;
531 }
532
533 int SIMIX_pre_process_count(smx_simcall_t simcall){
534   return SIMIX_process_count();
535 }
536 int SIMIX_process_count(void)
537 {
538   return xbt_swag_size(simix_global->process_list);
539 }
540
541 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
542   return SIMIX_process_self_get_data(self);     
543 }
544
545 void* SIMIX_process_self_get_data(smx_process_t self)
546 {
547   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
548
549   if (!self) {
550     return NULL;
551   }
552   return SIMIX_process_get_data(self);
553 }
554
555 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
556                                 void *data){
557   SIMIX_process_set_data(process, data);
558 }
559 void SIMIX_process_self_set_data(smx_process_t self, void *data)
560 {
561   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
562
563   SIMIX_process_set_data(self, data);
564 }
565
566 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
567   return SIMIX_process_get_data(process);
568 }
569 void* SIMIX_process_get_data(smx_process_t process)
570 {
571   return process->data;
572 }
573
574 void SIMIX_process_set_data(smx_process_t process, void *data)
575 {
576   process->data = data;
577 }
578
579 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
580   return SIMIX_process_get_host(process);
581 }
582 smx_host_t SIMIX_process_get_host(smx_process_t process)
583 {
584   return process->smx_host;
585 }
586
587 /* needs to be public and without simcall because it is called
588    by exceptions and logging events */
589 const char* SIMIX_process_self_get_name(void) {
590
591   smx_process_t process = SIMIX_process_self();
592   if (process == NULL || process == simix_global->maestro_process)
593     return "";
594
595   return SIMIX_process_get_name(process);
596 }
597
598 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
599   return SIMIX_process_get_name(process);
600 }
601 const char* SIMIX_process_get_name(smx_process_t process)
602 {
603   return process->name;
604 }
605
606 smx_process_t SIMIX_process_get_by_name(const char* name)
607 {
608   smx_process_t proc;
609
610   xbt_swag_foreach(proc, simix_global->process_list)
611   {
612     if(!strcmp(name, proc->name))
613       return proc;
614   }
615   return NULL;
616 }
617
618 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
619   return SIMIX_process_is_suspended(process);
620 }
621 int SIMIX_process_is_suspended(smx_process_t process)
622 {
623   return process->suspended;
624 }
625
626 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
627   return SIMIX_process_get_properties(process);
628 }
629 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
630 {
631   return process->properties;
632 }
633
634 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
635 {
636   if (MC_is_active()) {
637     MC_process_clock_add(simcall->issuer, duration);
638     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
639     SIMIX_simcall_answer(simcall);
640     return;
641   }
642   smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
643   xbt_fifo_push(action->simcalls, simcall);
644   simcall->issuer->waiting_action = action;
645 }
646
647 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
648 {
649   smx_action_t action;
650   smx_host_t host = process->smx_host;
651
652   /* check if the host is active */
653   if (surf_workstation_model->extension.
654       workstation.get_state(host) != SURF_RESOURCE_ON) {
655     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
656            sg_host_name(host));
657   }
658
659   action = xbt_mallocator_get(simix_global->action_mallocator);
660   action->type = SIMIX_ACTION_SLEEP;
661   action->name = NULL;
662 #ifdef HAVE_TRACING
663   action->category = NULL;
664 #endif
665
666   action->sleep.host = host;
667   action->sleep.surf_sleep =
668       surf_workstation_model->extension.workstation.sleep(host, duration);
669
670   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
671   XBT_DEBUG("Create sleep action %p", action);
672
673   return action;
674 }
675
676 void SIMIX_post_process_sleep(smx_action_t action)
677 {
678   smx_simcall_t simcall;
679   e_smx_state_t state;
680
681   while ((simcall = xbt_fifo_shift(action->simcalls))) {
682
683     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
684       case SURF_ACTION_FAILED:
685         simcall->issuer->context->iwannadie = 1;
686         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
687         break;
688
689       case SURF_ACTION_DONE:
690         state = SIMIX_DONE;
691         break;
692
693       default:
694         THROW_IMPOSSIBLE;
695         break;
696     }
697     if (surf_workstation_model->extension.
698         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
699       simcall->issuer->context->iwannadie = 1;
700     }
701     simcall_process_sleep__set__result(simcall, state);
702     simcall->issuer->waiting_action = NULL;
703     SIMIX_simcall_answer(simcall);
704
705   }
706   SIMIX_process_sleep_destroy(action);
707 }
708
709 void SIMIX_process_sleep_destroy(smx_action_t action)
710 {
711   XBT_DEBUG("Destroy action %p", action);
712   if (action->sleep.surf_sleep)
713     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
714   xbt_mallocator_release(simix_global->action_mallocator, action);
715 }
716
717 void SIMIX_process_sleep_suspend(smx_action_t action)
718 {
719   surf_workstation_model->suspend(action->sleep.surf_sleep);
720 }
721
722 void SIMIX_process_sleep_resume(smx_action_t action)
723 {
724   surf_workstation_model->resume(action->sleep.surf_sleep);
725 }
726
727 /** 
728  * \brief Calling this function makes the process to yield.
729  *
730  * Only the current process can call this function, giving back the control to
731  * maestro.
732  *
733  * \param self the current process
734  */
735 void SIMIX_process_yield(smx_process_t self)
736 {
737   XBT_DEBUG("Yield process '%s'", self->name);
738
739   /* Go into sleep and return control to maestro */
740   SIMIX_context_suspend(self->context);
741
742   /* Ok, maestro returned control to us */
743   XBT_DEBUG("Control returned to me: '%s'", self->name);
744
745   if (self->new_host) {
746     SIMIX_process_change_host(self, self->new_host);
747     self->new_host = NULL;
748   }
749
750   if (self->context->iwannadie){
751     XBT_DEBUG("I wanna die!");
752     SIMIX_process_stop(self);
753   }
754
755   if(self->suspended) {
756     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
757     self->suspended = 0;
758     SIMIX_process_suspend(self,self);
759   }
760
761   if (self->doexception) {
762     XBT_DEBUG("Wait, maestro left me an exception");
763     self->doexception = 0;
764     SMX_THROW();
765   }
766
767 }
768
769 /* callback: context fetching */
770 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
771 {
772   return SIMIX_process_self()->running_ctx;
773 }
774
775 /* callback: termination */
776 void SIMIX_process_exception_terminate(xbt_ex_t * e)
777 {
778   xbt_ex_display(e);
779   xbt_abort();
780 }
781
782 smx_context_t SIMIX_process_get_context(smx_process_t p) {
783   return p->context;
784 }
785
786 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
787   p->context = c;
788 }
789
790 /**
791  * \brief Returns the list of processes to run.
792  */
793 xbt_dynar_t SIMIX_process_get_runnable(void)
794 {
795   return simix_global->process_to_run;
796 }
797
798 /**
799  * \brief Returns the process from PID.
800  */
801 smx_process_t SIMIX_process_from_PID(int PID)
802 {
803   smx_process_t proc;
804   xbt_swag_foreach(proc, simix_global->process_list)
805   {
806    if(proc->pid == PID)
807    return proc;
808   }
809   return NULL;
810 }
811
812 /** @brief returns a dynar containg all currently existing processes */
813 xbt_dynar_t SIMIX_processes_as_dynar(void) {
814   smx_process_t proc;
815   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
816   xbt_swag_foreach(proc, simix_global->process_list) {
817     xbt_dynar_push(res,&proc);
818   }
819   return res;
820 }
821
822
823 void SIMIX_process_on_exit_runall(smx_process_t process) {
824   s_smx_process_exit_fun_t exit_fun;
825
826   while (!xbt_dynar_is_empty(process->on_exit)) {
827     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
828     (exit_fun.fun)(exit_fun.arg);
829   }
830 }
831
832 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
833                                int_f_pvoid_t fun, void *data) {
834   SIMIX_process_on_exit(process, fun, data);
835 }
836
837 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
838   xbt_assert(process, "current process not found: are you in maestro context ?");
839
840   if (!process->on_exit) {
841     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
842   }
843
844   s_smx_process_exit_fun_t exit_fun = {fun, data};
845
846   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
847 }
848
849 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
850                                         int auto_restart) {
851   SIMIX_process_auto_restart_set(process, auto_restart);        
852 }
853 /**
854  * \brief Sets the auto-restart status of the process.
855  * If set to 1, the process will be automatically restarted when its host
856  * comes back.
857  */
858 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
859   process->auto_restart = auto_restart;
860 }
861
862 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
863   return SIMIX_process_restart(process, simcall->issuer);       
864 }
865 /**
866  * \brief Restart a process.
867  * Restart a process, starting it again from the beginning.
868  */
869 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
870   XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
871   //retrieve the arguments of the old process
872   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
873   s_smx_process_arg_t arg;
874   arg.code = process->code;
875   arg.hostname = sg_host_name(process->smx_host);
876   arg.kill_time = process->kill_time;
877   arg.argc = process->argc;
878   arg.data = process->data;
879   int i;
880   arg.argv = xbt_new(char*,process->argc + 1);
881   for (i = 0; i < arg.argc; i++) {
882     arg.argv[i] = xbt_strdup(process->argv[i]);
883   }
884   arg.argv[process->argc] = NULL;
885   arg.properties = NULL;
886   arg.auto_restart = process->auto_restart;
887   //kill the old process
888   SIMIX_process_kill(process,issuer);
889   //start the new process
890   smx_process_t new_process;
891   if (simix_global->create_process_function) {
892     simix_global->create_process_function(&new_process,
893                                           arg.argv[0],
894                                           arg.code,
895                                           arg.data,
896                                           arg.hostname,
897                                           arg.kill_time,
898                                           arg.argc,
899                                           arg.argv,
900                                           arg.properties,
901                                           arg.auto_restart);
902   }
903   else {
904     simcall_process_create(&new_process,
905                                           arg.argv[0],
906                                           arg.code,
907                                           arg.data,
908                                           arg.hostname,
909                                           arg.kill_time,
910                                           arg.argc,
911                                           arg.argv,
912                                           arg.properties,
913                                           arg.auto_restart);
914
915   }
916   return new_process;
917 }