Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove comm from rdv if comm destroyed before being finished
[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, 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                                         arg->smx_host->name,
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,arg->smx_host->name);
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     if(MC_is_active())
253       MC_ignore_heap(&((*process)->simcall.result), sizeof((*process)->simcall.result));
254     
255     /* Process data for auto-restart */
256     (*process)->auto_restart = auto_restart;
257     (*process)->code = code;
258     (*process)->argc = argc;
259     (*process)->argv = argv;
260     (*process)->kill_time = kill_time;
261
262
263     XBT_VERB("Create context %s", (*process)->name);
264     (*process)->context = SIMIX_context_new(code, argc, argv,
265       simix_global->cleanup_process_function, *process);
266
267     (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
268     XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
269
270     /* Add properties */
271     (*process)->properties = properties;
272
273     /* Add the process to it's host process list */
274     xbt_swag_insert(*process, host->process_list);
275
276     XBT_DEBUG("Start context '%s'", (*process)->name);
277
278     /* Now insert it in the global process list and in the process to run list */
279     xbt_swag_insert(*process, simix_global->process_list);
280     XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, host->name);
281     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
282   }
283
284   if (kill_time > SIMIX_get_clock()) {
285     if (simix_global->kill_process_function) {
286       XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
287           (*process)->smx_host->name, kill_time);
288       SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
289     }
290   }
291 }
292
293 /**
294  * \brief Executes the processes from simix_global->process_to_run.
295  *
296  * The processes of simix_global->process_to_run are run (in parallel if
297  * possible).  On exit, simix_global->process_to_run is empty, and
298  * simix_global->process_that_ran contains the list of processes that just ran.
299  * The two lists are swapped so, be careful when using them before and after a
300  * call to this function.
301  */
302 void SIMIX_process_runall(void)
303 {
304   SIMIX_context_runall();
305
306   xbt_dynar_t tmp = simix_global->process_that_ran;
307   simix_global->process_that_ran = simix_global->process_to_run;
308   simix_global->process_to_run = tmp;
309   xbt_dynar_reset(simix_global->process_to_run);
310 }
311
312 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
313   SIMIX_process_kill(process, simcall->issuer);
314 }
315 /**
316  * \brief Internal function to kill a SIMIX process.
317  *
318  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
319  * or directly for SIMIX internal purposes.
320  *
321  * \param process poor victim
322  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
323  */
324 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
325
326   XBT_DEBUG("Killing process %s on %s", process->name, process->smx_host->name);
327
328   process->context->iwannadie = 1;
329   process->blocked = 0;
330   process->suspended = 0;
331   /* FIXME: set doexception to 0 also? */
332
333   /* destroy the blocking action if any */
334   if (process->waiting_action) {
335
336     switch (process->waiting_action->type) {
337
338       case SIMIX_ACTION_EXECUTE:          
339       case SIMIX_ACTION_PARALLEL_EXECUTE:
340         SIMIX_host_execution_destroy(process->waiting_action);
341         break;
342
343       case SIMIX_ACTION_COMMUNICATE:
344         xbt_fifo_remove(process->comms, process->waiting_action);
345         SIMIX_comm_cancel(process->waiting_action);
346         break;
347
348         case SIMIX_ACTION_SLEEP:
349           SIMIX_process_sleep_destroy(process->waiting_action);
350           break;
351
352         case SIMIX_ACTION_SYNCHRO:
353           SIMIX_synchro_stop_waiting(process, &process->simcall);
354           SIMIX_synchro_destroy(process->waiting_action);
355           break;
356
357         case SIMIX_ACTION_IO:
358           SIMIX_io_destroy(process->waiting_action);
359           break;
360
361         /* **************************************/
362         /* TUTORIAL: New API                    */
363         case SIMIX_ACTION_NEW_API:
364           SIMIX_new_api_destroy(process->waiting_action);
365           break;
366         /* **************************************/
367
368     }
369   }
370   if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
371     xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
372   }
373
374 }
375
376 void SIMIX_pre_process_killall(smx_simcall_t simcall) {
377   SIMIX_process_killall(simcall->issuer);
378 }
379 /**
380  * \brief Kills all running processes.
381  * \param issuer this one will not be killed
382  */
383 void SIMIX_process_killall(smx_process_t issuer)
384 {
385   smx_process_t p = NULL;
386
387   while ((p = xbt_swag_extract(simix_global->process_list))) {
388     if (p != issuer) {
389       SIMIX_process_kill(p,issuer);
390     }
391   }
392
393   SIMIX_context_runall();
394
395   SIMIX_process_empty_trash();
396 }
397
398 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
399                                    smx_host_t dest)
400 {
401   process->new_host = dest;
402 }
403 void SIMIX_process_change_host(smx_process_t process,
404              smx_host_t dest)
405 {
406   xbt_assert((process != NULL), "Invalid parameters");
407   xbt_swag_remove(process, process->smx_host->process_list);
408   process->smx_host = dest;
409   xbt_swag_insert(process, dest->process_list);
410 }
411
412
413 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
414 {
415   smx_action_t action_suspend =
416       SIMIX_process_suspend(process, simcall->issuer);
417
418   if (process != simcall->issuer) {
419     SIMIX_simcall_answer(simcall);
420   } else {
421     xbt_fifo_push(action_suspend->simcalls, simcall);
422     process->waiting_action = action_suspend;
423     SIMIX_host_execution_suspend(process->waiting_action);
424   }
425   /* If we are suspending ourselves, then just do not finish the simcall now */
426 }
427
428 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
429 {
430   xbt_assert((process != NULL), "Invalid parameters");
431
432   if (process->suspended) {
433     XBT_DEBUG("Process '%s' is already suspended", process->name);
434     return NULL;
435   }
436
437   process->suspended = 1;
438
439   /* If we are suspending another process, and it is waiting on an action,
440      suspend its action. */
441   if (process != issuer) {
442
443     if (process->waiting_action) {
444
445       switch (process->waiting_action->type) {
446
447         case SIMIX_ACTION_EXECUTE:
448         case SIMIX_ACTION_PARALLEL_EXECUTE:
449           SIMIX_host_execution_suspend(process->waiting_action);
450           break;
451
452         case SIMIX_ACTION_COMMUNICATE:
453           SIMIX_comm_suspend(process->waiting_action);
454           break;
455
456         case SIMIX_ACTION_SLEEP:
457           SIMIX_process_sleep_suspend(process->waiting_action);
458           break;
459
460         case SIMIX_ACTION_SYNCHRO:
461           /* Suspension is delayed to when the process is rescheduled. */
462           break;
463
464         default:
465           xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
466               (int)process->waiting_action->type);
467       }
468       return NULL;
469     } else {
470       /* Suspension is delayed to when the process is rescheduled. */
471       return NULL;
472     }
473   } else {
474     return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0);
475   }
476 }
477
478 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
479   SIMIX_process_resume(process, simcall->issuer);
480 }
481
482 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
483 {
484   xbt_assert((process != NULL), "Invalid parameters");
485
486   XBT_IN("process = %p, issuer = %p", process, issuer);
487
488   if(process->context->iwannadie) {
489     XBT_VERB("Ignoring request to suspend a process that is currently dying.");
490     return;
491   }
492
493   if(!process->suspended) return;
494   process->suspended = 0;
495
496   /* If we are resuming another process, resume the action it was waiting for
497      if any. Otherwise add it to the list of process to run in the next round. */
498   if (process != issuer) {
499
500     if (process->waiting_action) {
501
502       switch (process->waiting_action->type) {
503
504         case SIMIX_ACTION_EXECUTE:          
505         case SIMIX_ACTION_PARALLEL_EXECUTE:
506           SIMIX_host_execution_resume(process->waiting_action);
507           break;
508
509         case SIMIX_ACTION_COMMUNICATE:
510           SIMIX_comm_resume(process->waiting_action);
511           break;
512
513         case SIMIX_ACTION_SLEEP:
514           SIMIX_process_sleep_resume(process->waiting_action);
515           break;
516
517         case SIMIX_ACTION_SYNCHRO:
518           /* I cannot resume it now. This is delayed to when the process is rescheduled at
519            * the end of the synchro. */
520           break;
521
522         default:
523           xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
524               (int)process->waiting_action->type);
525       }
526     }
527   } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
528
529   XBT_OUT();
530 }
531
532 int SIMIX_process_get_maxpid(void) {
533   return simix_process_maxpid;
534 }
535
536 int SIMIX_pre_process_count(smx_simcall_t simcall){
537   return SIMIX_process_count();
538 }
539 int SIMIX_process_count(void)
540 {
541   return xbt_swag_size(simix_global->process_list);
542 }
543
544 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
545   return SIMIX_process_self_get_data(self);     
546 }
547
548 void* SIMIX_process_self_get_data(smx_process_t self)
549 {
550   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
551
552   if (!self) {
553     return NULL;
554   }
555   return SIMIX_process_get_data(self);
556 }
557
558 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
559                                 void *data){
560   SIMIX_process_set_data(process, data);
561 }
562 void SIMIX_process_self_set_data(smx_process_t self, void *data)
563 {
564   xbt_assert(self == SIMIX_process_self(), "This is not the current process");
565
566   SIMIX_process_set_data(self, data);
567 }
568
569 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
570   return SIMIX_process_get_data(process);
571 }
572 void* SIMIX_process_get_data(smx_process_t process)
573 {
574   return process->data;
575 }
576
577 void SIMIX_process_set_data(smx_process_t process, void *data)
578 {
579   process->data = data;
580 }
581
582 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
583   return SIMIX_process_get_host(process);
584 }
585 smx_host_t SIMIX_process_get_host(smx_process_t process)
586 {
587   return process->smx_host;
588 }
589
590 /* needs to be public and without simcall because it is called
591    by exceptions and logging events */
592 const char* SIMIX_process_self_get_name(void) {
593
594   smx_process_t process = SIMIX_process_self();
595   if (process == NULL || process == simix_global->maestro_process)
596     return "";
597
598   return SIMIX_process_get_name(process);
599 }
600
601 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
602   return SIMIX_process_get_name(process);
603 }
604 const char* SIMIX_process_get_name(smx_process_t process)
605 {
606   return process->name;
607 }
608
609 smx_process_t SIMIX_process_get_by_name(const char* name)
610 {
611   smx_process_t proc;
612
613   xbt_swag_foreach(proc, simix_global->process_list)
614   {
615     if(!strcmp(name, proc->name))
616       return proc;
617   }
618   return NULL;
619 }
620
621 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
622   return SIMIX_process_is_suspended(process);
623 }
624 int SIMIX_process_is_suspended(smx_process_t process)
625 {
626   return process->suspended;
627 }
628
629 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
630   return SIMIX_process_get_properties(process);
631 }
632 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
633 {
634   return process->properties;
635 }
636
637 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
638 {
639   if (MC_is_active()) {
640     MC_process_clock_add(simcall->issuer, duration);
641     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
642     SIMIX_simcall_answer(simcall);
643     return;
644   }
645   smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
646   xbt_fifo_push(action->simcalls, simcall);
647   simcall->issuer->waiting_action = action;
648 }
649
650 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
651 {
652   smx_action_t action;
653   smx_host_t host = process->smx_host;
654
655   /* check if the host is active */
656   if (surf_workstation_model->extension.
657       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
658     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
659            host->name);
660   }
661
662   action = xbt_mallocator_get(simix_global->action_mallocator);
663   action->type = SIMIX_ACTION_SLEEP;
664   action->name = NULL;
665 #ifdef HAVE_TRACING
666   action->category = NULL;
667 #endif
668
669   action->sleep.host = host;
670   action->sleep.surf_sleep =
671       surf_workstation_model->extension.workstation.sleep(host->host, duration);
672
673   surf_workstation_model->action_data_set(action->sleep.surf_sleep, action);
674   XBT_DEBUG("Create sleep action %p", action);
675
676   return action;
677 }
678
679 void SIMIX_post_process_sleep(smx_action_t action)
680 {
681   smx_simcall_t simcall;
682   e_smx_state_t state;
683
684   while ((simcall = xbt_fifo_shift(action->simcalls))) {
685
686     switch(surf_workstation_model->action_state_get(action->sleep.surf_sleep)){
687       case SURF_ACTION_FAILED:
688         simcall->issuer->context->iwannadie = 1;
689         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
690         break;
691
692       case SURF_ACTION_DONE:
693         state = SIMIX_DONE;
694         break;
695
696       default:
697         THROW_IMPOSSIBLE;
698         break;
699     }
700     if (surf_workstation_model->extension.
701         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
702       simcall->issuer->context->iwannadie = 1;
703     }
704     simcall_process_sleep__set__result(simcall, state);
705     simcall->issuer->waiting_action = NULL;
706     SIMIX_simcall_answer(simcall);
707
708   }
709   SIMIX_process_sleep_destroy(action);
710 }
711
712 void SIMIX_process_sleep_destroy(smx_action_t action)
713 {
714   XBT_DEBUG("Destroy action %p", action);
715   if (action->sleep.surf_sleep)
716     action->sleep.surf_sleep->model_type->action_unref(action->sleep.surf_sleep);
717   xbt_mallocator_release(simix_global->action_mallocator, action);
718 }
719
720 void SIMIX_process_sleep_suspend(smx_action_t action)
721 {
722   surf_workstation_model->suspend(action->sleep.surf_sleep);
723 }
724
725 void SIMIX_process_sleep_resume(smx_action_t action)
726 {
727   surf_workstation_model->resume(action->sleep.surf_sleep);
728 }
729
730 /** 
731  * \brief Calling this function makes the process to yield.
732  *
733  * Only the current process can call this function, giving back the control to
734  * maestro.
735  *
736  * \param self the current process
737  */
738 void SIMIX_process_yield(smx_process_t self)
739 {
740   XBT_DEBUG("Yield process '%s'", self->name);
741
742   /* Go into sleep and return control to maestro */
743   SIMIX_context_suspend(self->context);
744
745   /* Ok, maestro returned control to us */
746   XBT_DEBUG("Control returned to me: '%s'", self->name);
747
748   if (self->new_host) {
749     SIMIX_process_change_host(self, self->new_host);
750     self->new_host = NULL;
751   }
752
753   if (self->context->iwannadie){
754     XBT_DEBUG("I wanna die!");
755     SIMIX_process_stop(self);
756   }
757
758   if(self->suspended) {
759     xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
760     self->suspended = 0;
761     SIMIX_process_suspend(self,self);
762   }
763
764   if (self->doexception) {
765     XBT_DEBUG("Wait, maestro left me an exception");
766     self->doexception = 0;
767     SMX_THROW();
768   }
769
770 }
771
772 /* callback: context fetching */
773 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
774 {
775   return SIMIX_process_self()->running_ctx;
776 }
777
778 /* callback: termination */
779 void SIMIX_process_exception_terminate(xbt_ex_t * e)
780 {
781   xbt_ex_display(e);
782   xbt_abort();
783 }
784
785 smx_context_t SIMIX_process_get_context(smx_process_t p) {
786   return p->context;
787 }
788
789 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
790   p->context = c;
791 }
792
793 /**
794  * \brief Returns the list of processes to run.
795  */
796 xbt_dynar_t SIMIX_process_get_runnable(void)
797 {
798   return simix_global->process_to_run;
799 }
800
801 /**
802  * \brief Returns the process from PID.
803  */
804 smx_process_t SIMIX_process_from_PID(int PID)
805 {
806   smx_process_t proc;
807   xbt_swag_foreach(proc, simix_global->process_list)
808   {
809    if(proc->pid == PID)
810    return proc;
811   }
812   return NULL;
813 }
814
815 /** @brief returns a dynar containg all currently existing processes */
816 xbt_dynar_t SIMIX_processes_as_dynar(void) {
817   smx_process_t proc;
818   xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
819   xbt_swag_foreach(proc, simix_global->process_list) {
820     xbt_dynar_push(res,&proc);
821   }
822   return res;
823 }
824
825
826 void SIMIX_process_on_exit_runall(smx_process_t process) {
827   s_smx_process_exit_fun_t exit_fun;
828
829   while (!xbt_dynar_is_empty(process->on_exit)) {
830     exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
831     (exit_fun.fun)(exit_fun.arg);
832   }
833 }
834
835 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
836                                int_f_pvoid_t fun, void *data) {
837   SIMIX_process_on_exit(process, fun, data);
838 }
839
840 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data) {
841   xbt_assert(process, "current process not found: are you in maestro context ?");
842
843   if (!process->on_exit) {
844     process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
845   }
846
847   s_smx_process_exit_fun_t exit_fun = {fun, data};
848
849   xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
850 }
851
852 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
853                                         int auto_restart) {
854   SIMIX_process_auto_restart_set(process, auto_restart);        
855 }
856 /**
857  * \brief Sets the auto-restart status of the process.
858  * If set to 1, the process will be automatically restarted when its host
859  * comes back.
860  */
861 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
862   process->auto_restart = auto_restart;
863 }
864
865 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
866   return SIMIX_process_restart(process, simcall->issuer);       
867 }
868 /**
869  * \brief Restart a process.
870  * Restart a process, starting it again from the beginning.
871  */
872 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
873   XBT_DEBUG("Restarting process %s on %s", process->name, process->smx_host->name);
874   //retrieve the arguments of the old process
875   //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
876   s_smx_process_arg_t arg;
877   arg.code = process->code;
878   arg.hostname = process->smx_host->name;
879   arg.kill_time = process->kill_time;
880   arg.argc = process->argc;
881   arg.data = process->data;
882   int i;
883   arg.argv = xbt_new(char*,process->argc + 1);
884   for (i = 0; i < arg.argc; i++) {
885     arg.argv[i] = xbt_strdup(process->argv[i]);
886   }
887   arg.argv[process->argc] = NULL;
888   arg.properties = NULL;
889   arg.auto_restart = process->auto_restart;
890   //kill the old process
891   SIMIX_process_kill(process,issuer);
892   //start the new process
893   smx_process_t new_process;
894   if (simix_global->create_process_function) {
895     simix_global->create_process_function(&new_process,
896                                           arg.argv[0],
897                                           arg.code,
898                                           arg.data,
899                                           arg.hostname,
900                                           arg.kill_time,
901                                           arg.argc,
902                                           arg.argv,
903                                           arg.properties,
904                                           arg.auto_restart);
905   }
906   else {
907     simcall_process_create(&new_process,
908                                           arg.argv[0],
909                                           arg.code,
910                                           arg.data,
911                                           arg.hostname,
912                                           arg.kill_time,
913                                           arg.argc,
914                                           arg.argv,
915                                           arg.properties,
916                                           arg.auto_restart);
917
918   }
919   return new_process;
920 }