Logo AND Algorithmique Numérique Distribuée

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