Logo AND Algorithmique Numérique Distribuée

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