Logo AND Algorithmique Numérique Distribuée

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