Logo AND Algorithmique Numérique Distribuée

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