Logo AND Algorithmique Numérique Distribuée

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