Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
obey a FIXME and factorize some code
[simgrid.git] / src / simix / ActorImpl.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc/mc.h"
7 #include "smx_private.hpp"
8 #include "src/kernel/activity/IoImpl.hpp"
9 #include "src/kernel/activity/SleepImpl.hpp"
10 #include "src/kernel/activity/SynchroRaw.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/mc/remote/Client.hpp"
13 #include "src/simix/smx_host_private.hpp"
14 #include "src/simix/smx_io_private.hpp"
15 #include "src/simix/smx_synchro_private.hpp"
16 #include "src/surf/cpu_interface.hpp"
17 #include "xbt/ex.hpp"
18
19 #ifdef HAVE_SMPI
20 #include "src/smpi/include/private.hpp"
21 #endif
22
23 #include <boost/range/algorithm.hpp>
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_actor_t SIMIX_process_self()
37 {
38   smx_context_t self_context = SIMIX_context_self();
39
40   return (self_context != nullptr) ? self_context->process() : nullptr;
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_actor_t process) {
48
49   return process->comms.size() > 0;
50 }
51
52 /**
53  * \brief Moves a process to the list of processes to destroy.
54  */
55 void SIMIX_process_cleanup(smx_actor_t process)
56 {
57   XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p", process->get_cname(), process,
58             process->waiting_synchro.get());
59
60   process->finished_ = true;
61   SIMIX_process_on_exit_runall(process);
62
63   /* Unregister from the kill timer if any */
64   if (process->kill_timer != nullptr) {
65     SIMIX_timer_remove(process->kill_timer);
66     process->kill_timer = nullptr;
67   }
68
69   xbt_os_mutex_acquire(simix_global->mutex);
70
71   /* cancel non-blocking communications */
72   while (not process->comms.empty()) {
73     smx_activity_t synchro = process->comms.front();
74     process->comms.pop_front();
75     simgrid::kernel::activity::CommImplPtr comm =
76         boost::static_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
77
78     /* make sure no one will finish the comm after this process is destroyed,
79      * because src_proc or dst_proc would be an invalid pointer */
80
81     if (comm->src_proc == process) {
82       XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p", comm.get(),
83                 comm->detached, (int)comm->state_, comm->src_proc, comm->dst_proc);
84       comm->src_proc = nullptr;
85
86     } else if (comm->dst_proc == process) {
87       XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p", comm.get(), (int)comm->state_,
88                 comm->src_proc, comm->dst_proc);
89       comm->dst_proc = nullptr;
90
91       if (comm->detached && comm->src_proc != nullptr) {
92         /* the comm will be freed right now, remove it from the sender */
93         comm->src_proc->comms.remove(comm);
94       }
95     } else {
96       xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro.get());
97     }
98     comm->cancel();
99   }
100
101   XBT_DEBUG("%p should not be run anymore",process);
102   simix_global->process_list.erase(process->pid_);
103   if (process->host_ && process->host_process_list_hook.is_linked())
104     simgrid::xbt::intrusive_erase(process->host_->extension<simgrid::simix::Host>()->process_list, *process);
105   if (not process->smx_destroy_list_hook.is_linked()) {
106 #if SIMGRID_HAVE_MC
107     xbt_dynar_push_as(simix_global->dead_actors_vector, smx_actor_t, process);
108 #endif
109     simix_global->process_to_destroy.push_back(*process);
110   }
111   process->context_->iwannadie = 0;
112
113   xbt_os_mutex_release(simix_global->mutex);
114 }
115
116 /**
117  * Garbage collection
118  *
119  * Should be called some time to time to free the memory allocated for processes that have finished (or killed).
120  */
121 void SIMIX_process_empty_trash()
122 {
123   while (not simix_global->process_to_destroy.empty()) {
124     smx_actor_t process = &simix_global->process_to_destroy.front();
125     simix_global->process_to_destroy.pop_front();
126     XBT_DEBUG("Getting rid of %p",process);
127     intrusive_ptr_release(process);
128   }
129 #if SIMGRID_HAVE_MC
130   xbt_dynar_reset(simix_global->dead_actors_vector);
131 #endif
132 }
133
134 namespace simgrid {
135
136 namespace kernel {
137 namespace actor {
138
139 ActorImpl::~ActorImpl()
140 {
141   delete this->context_;
142 }
143
144 static void dying_daemon(int /*exit_status*/, void* data)
145 {
146   std::vector<ActorImpl*>* vect = &simix_global->daemons;
147
148   auto it = std::find(vect->begin(), vect->end(), static_cast<ActorImpl*>(data));
149   xbt_assert(it != vect->end(), "The dying daemon is not a daemon after all. Please report that bug.");
150
151   /* Don't move the whole content since we don't really care about the order */
152   std::swap(*it, vect->back());
153   vect->pop_back();
154 }
155
156 /** This process will be terminated automatically when the last non-daemon process finishes */
157 void ActorImpl::daemonize()
158 {
159   if (not daemon_) {
160     daemon_ = true;
161     simix_global->daemons.push_back(this);
162     SIMIX_process_on_exit(this, dying_daemon, this);
163   }
164 }
165
166 simgrid::s4u::Actor* ActorImpl::restart()
167 {
168   XBT_DEBUG("Restarting process %s on %s", get_cname(), host_->get_cname());
169
170   // retrieve the arguments of the old process
171   simgrid::kernel::actor::ProcessArg arg = ProcessArg(host_, this);
172
173   // kill the old process
174   SIMIX_process_kill(this, (this == simix_global->maestro_process) ? this : SIMIX_process_self());
175
176   // start the new process
177   ActorImpl* actor = simix_global->create_process_function(arg.name.c_str(), std::move(arg.code), arg.data, arg.host,
178                                                            arg.properties.get(), nullptr);
179   simcall_process_set_kill_time(actor, arg.kill_time);
180   actor->set_auto_restart(arg.auto_restart);
181
182   return actor->ciface();
183 }
184
185 smx_activity_t ActorImpl::suspend(ActorImpl* issuer)
186 {
187   if (suspended_) {
188     XBT_DEBUG("Actor '%s' is already suspended", get_cname());
189     return nullptr;
190   }
191
192   suspended_ = 1;
193
194   /* If we are suspending another actor that is waiting on a sync, suspend its synchronization. */
195   if (this != issuer) {
196     if (waiting_synchro)
197       waiting_synchro->suspend();
198     /* If the other actor is not waiting, its suspension is delayed to when the actor is rescheduled. */
199
200     return nullptr;
201   } else {
202     return SIMIX_execution_start("suspend", 0.0, 1.0, 0.0, this->host_);
203   }
204 }
205
206 void ActorImpl::resume()
207 {
208   XBT_IN("process = %p", this);
209
210   if (context_->iwannadie) {
211     XBT_VERB("Ignoring request to suspend an actor that is currently dying.");
212     return;
213   }
214
215   if (not suspended_)
216     return;
217   suspended_ = 0;
218
219   /* resume the synchronization that was blocking the resumed actor. */
220   if (waiting_synchro)
221     waiting_synchro->resume();
222
223   XBT_OUT();
224 }
225
226 smx_activity_t ActorImpl::sleep(double duration)
227 {
228   if (host_->is_off())
229     THROWF(host_error, 0, "Host %s failed, you cannot sleep there.", host_->get_cname());
230
231   simgrid::kernel::activity::SleepImpl* synchro = new simgrid::kernel::activity::SleepImpl();
232   synchro->host                                 = host_;
233   synchro->surf_sleep                           = host_->pimpl_cpu->sleep(duration);
234   synchro->surf_sleep->set_data(synchro);
235   XBT_DEBUG("Create sleep synchronization %p", synchro);
236
237   return synchro;
238 }
239
240 void create_maestro(simgrid::simix::ActorCode code)
241 {
242   smx_actor_t maestro = nullptr;
243   /* Create maestro process and initialize it */
244   maestro           = new simgrid::kernel::actor::ActorImpl();
245   maestro->pid_     = simix_process_maxpid++;
246   maestro->name_    = "";
247   maestro->set_user_data(nullptr);
248
249   if (not code) {
250     maestro->context_ = SIMIX_context_new(simgrid::simix::ActorCode(), nullptr, maestro);
251   } else {
252     if (not simix_global)
253       xbt_die("simix is not initialized, please call MSG_init first");
254     maestro->context_ = simix_global->context_factory->create_maestro(code, maestro);
255   }
256
257   maestro->simcall.issuer = maestro;
258   simix_global->maestro_process = maestro;
259 }
260
261 } // namespace actor
262 }
263 }
264
265 /** @brief Creates and runs the maestro process */
266 void SIMIX_maestro_create(void (*code)(void*), void* data)
267 {
268   simgrid::kernel::actor::create_maestro(std::bind(code, data));
269 }
270
271 /**
272  * \brief Internal function to create a process.
273  *
274  * This function actually creates the process.
275  * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
276  * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
277  *
278  * \return the process created
279  */
280 smx_actor_t SIMIX_process_create(const char* name, simgrid::simix::ActorCode code, void* data, simgrid::s4u::Host* host,
281                                  std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
282 {
283
284   XBT_DEBUG("Start process %s on host '%s'", name, host->get_cname());
285
286   if (host->is_off()) {
287     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, host->get_cname());
288     return nullptr;
289   }
290
291   smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
292
293   xbt_assert(code && host != nullptr, "Invalid parameters");
294   /* Process data */
295   process->pid_           = simix_process_maxpid++;
296   process->name_          = simgrid::xbt::string(name);
297   process->host_          = host;
298   process->set_user_data(data);
299   process->simcall.issuer = process;
300
301   if (parent_process != nullptr) {
302     process->ppid_ = parent_process->pid_;
303   }
304
305   process->code         = code;
306
307   XBT_VERB("Create context %s", process->get_cname());
308   process->context_ = SIMIX_context_new(std::move(code), simix_global->cleanup_process_function, process);
309
310   /* Add properties */
311   if (properties != nullptr)
312     for (auto const& kv : *properties)
313       process->set_property(kv.first, kv.second);
314
315   /* Make sure that the process is initialized for simix, in case we are called from the Host::onCreation signal */
316   if (host->extension<simgrid::simix::Host>() == nullptr)
317     host->extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
318
319   /* Add the process to its host's process list */
320   host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
321
322   XBT_DEBUG("Start context '%s'", process->get_cname());
323
324   /* Now insert it in the global process list and in the process to run list */
325   simix_global->process_list[process->pid_] = process;
326   XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
327   simix_global->process_to_run.push_back(process);
328   intrusive_ptr_add_ref(process);
329
330   /* The onCreation() signal must be delayed until there, where the pid and everything is set */
331   simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
332   simgrid::s4u::Actor::on_creation(tmp);
333
334   return process;
335 }
336
337 smx_actor_t SIMIX_process_attach(const char* name, void* data, const char* hostname,
338                                  std::unordered_map<std::string, std::string>* properties, smx_actor_t parent_process)
339 {
340   // This is mostly a copy/paste from SIMIX_process_new(),
341   // it'd be nice to share some code between those two functions.
342
343   sg_host_t host = sg_host_by_name(hostname);
344   XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
345
346   if (host->is_off()) {
347     XBT_WARN("Cannot launch process '%s' on failed host '%s'", name, hostname);
348     return nullptr;
349   }
350
351   smx_actor_t process = new simgrid::kernel::actor::ActorImpl();
352   /* Process data */
353   process->pid_  = simix_process_maxpid++;
354   process->name_ = std::string(name);
355   process->host_ = host;
356   process->set_user_data(data);
357   process->simcall.issuer = process;
358
359   if (parent_process != nullptr) {
360     process->ppid_ = parent_process->pid_;
361   }
362
363   /* Process data for auto-restart */
364   process->code = nullptr;
365
366   XBT_VERB("Create context %s", process->get_cname());
367   if (not simix_global)
368     xbt_die("simix is not initialized, please call MSG_init first");
369   process->context_ = simix_global->context_factory->attach(simix_global->cleanup_process_function, process);
370
371   /* Add properties */
372   if (properties != nullptr)
373     for (auto const& kv : *properties)
374       process->set_property(kv.first, kv.second);
375
376   /* Add the process to it's host process list */
377   host->extension<simgrid::simix::Host>()->process_list.push_back(*process);
378
379   /* Now insert it in the global process list and in the process to run list */
380   simix_global->process_list[process->pid_] = process;
381   XBT_DEBUG("Inserting %s(%s) in the to_run list", process->get_cname(), host->get_cname());
382   simix_global->process_to_run.push_back(process);
383   intrusive_ptr_add_ref(process);
384
385   auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(process->context_);
386   if (not context)
387     xbt_die("Not a suitable context");
388
389   context->attach_start();
390
391   /* The onCreation() signal must be delayed until there, where the pid and everything is set */
392   simgrid::s4u::ActorPtr tmp = process->iface(); // Passing this directly to onCreation will lead to crashes
393   simgrid::s4u::Actor::on_creation(tmp);
394
395   return process;
396 }
397
398 void SIMIX_process_detach()
399 {
400   auto* context = dynamic_cast<simgrid::kernel::context::AttachContext*>(SIMIX_context_self());
401   if (not context)
402     xbt_die("Not a suitable context");
403
404   auto process = context->process();
405   simix_global->cleanup_process_function(process);
406   context->attach_stop();
407 }
408
409 /**
410  * \brief Executes the processes from simix_global->process_to_run.
411  *
412  * The processes of simix_global->process_to_run are run (in parallel if
413  * possible).  On exit, simix_global->process_to_run is empty, and
414  * simix_global->process_that_ran contains the list of processes that just ran.
415  * The two lists are swapped so, be careful when using them before and after a
416  * call to this function.
417  */
418 void SIMIX_process_runall()
419 {
420   SIMIX_context_runall();
421
422   simix_global->process_to_run.swap(simix_global->process_that_ran);
423   simix_global->process_to_run.clear();
424 }
425
426 /**
427  * \brief Internal function to kill a SIMIX process.
428  *
429  * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
430  * or directly for SIMIX internal purposes.
431  *
432  * \param process poor victim
433  * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
434  */
435 void SIMIX_process_kill(smx_actor_t process, smx_actor_t issuer) {
436
437   if (process->finished_) {
438     XBT_DEBUG("Ignoring request to kill process %s@%s that is already dead", process->get_cname(),
439               process->host_->get_cname());
440     return;
441   }
442
443   XBT_DEBUG("Actor '%s'@%s is killing actor '%s'@%s", issuer->get_cname(), issuer->host_->get_cname(),
444             process->get_cname(), process->host_->get_cname());
445
446   process->context_->iwannadie = 1;
447   process->blocked_           = 0;
448   process->suspended_         = 0;
449   process->exception = nullptr;
450
451   /* destroy the blocking synchro if any */
452   if (process->waiting_synchro != nullptr) {
453
454     simgrid::kernel::activity::ExecImplPtr exec =
455         boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
456     simgrid::kernel::activity::CommImplPtr comm =
457         boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
458     simgrid::kernel::activity::SleepImplPtr sleep =
459         boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
460     simgrid::kernel::activity::RawImplPtr raw =
461         boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
462     simgrid::kernel::activity::IoImplPtr io =
463         boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
464
465     if (exec != nullptr) {
466       if (exec->surf_action_) {
467         exec->surf_action_->cancel();
468         exec->surf_action_->unref();
469         exec->surf_action_ = nullptr;
470       }
471     } else if (comm != nullptr) {
472       process->comms.remove(process->waiting_synchro);
473       comm->cancel();
474       // Remove first occurrence of &process->simcall:
475       auto i = boost::range::find(process->waiting_synchro->simcalls_, &process->simcall);
476       if (i != process->waiting_synchro->simcalls_.end())
477         process->waiting_synchro->simcalls_.remove(&process->simcall);
478     } else if (sleep != nullptr) {
479       SIMIX_process_sleep_destroy(process->waiting_synchro);
480
481     } else if (raw != nullptr) {
482       SIMIX_synchro_stop_waiting(process, &process->simcall);
483
484     } else if (io != nullptr) {
485       SIMIX_io_destroy(process->waiting_synchro);
486     } else {
487       xbt_die("Unknown type of activity");
488     }
489
490     process->waiting_synchro = nullptr;
491   }
492   if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
493           end(simix_global->process_to_run) &&
494       process != issuer) {
495     XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
496     simix_global->process_to_run.push_back(process);
497   }
498 }
499
500 /** @brief Ask another process to raise the given exception
501  *
502  * @param process The process that should raise that exception
503  * @param cat category of exception
504  * @param value value associated to the exception
505  * @param msg string information associated to the exception
506  */
507 void SIMIX_process_throw(smx_actor_t process, xbt_errcat_t cat, int value, const char *msg) {
508   SMX_EXCEPTION(process, cat, value, msg);
509
510   if (process->suspended_)
511     process->resume();
512
513   /* cancel the blocking synchro if any */
514   if (process->waiting_synchro) {
515
516     simgrid::kernel::activity::ExecImplPtr exec =
517         boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(process->waiting_synchro);
518     if (exec != nullptr && exec->surf_action_)
519       exec->surf_action_->cancel();
520
521     simgrid::kernel::activity::CommImplPtr comm =
522         boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(process->waiting_synchro);
523     if (comm != nullptr) {
524       process->comms.remove(comm);
525       comm->cancel();
526     }
527
528     simgrid::kernel::activity::SleepImplPtr sleep =
529         boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(process->waiting_synchro);
530     if (sleep != nullptr) {
531       SIMIX_process_sleep_destroy(process->waiting_synchro);
532       if (std::find(begin(simix_global->process_to_run), end(simix_global->process_to_run), process) ==
533               end(simix_global->process_to_run) &&
534           process != SIMIX_process_self()) {
535         XBT_DEBUG("Inserting %s in the to_run list", process->get_cname());
536         simix_global->process_to_run.push_back(process);
537       }
538     }
539
540     simgrid::kernel::activity::RawImplPtr raw =
541         boost::dynamic_pointer_cast<simgrid::kernel::activity::RawImpl>(process->waiting_synchro);
542     if (raw != nullptr) {
543       SIMIX_synchro_stop_waiting(process, &process->simcall);
544     }
545
546     simgrid::kernel::activity::IoImplPtr io =
547         boost::dynamic_pointer_cast<simgrid::kernel::activity::IoImpl>(process->waiting_synchro);
548     if (io != nullptr) {
549       SIMIX_io_destroy(process->waiting_synchro);
550     }
551   }
552   process->waiting_synchro = nullptr;
553
554 }
555
556 /**
557  * \brief Kills all running processes.
558  * \param issuer this one will not be killed
559  */
560 void SIMIX_process_killall(smx_actor_t issuer)
561 {
562   for (auto const& kv : simix_global->process_list)
563     if (kv.second != issuer)
564       SIMIX_process_kill(kv.second, issuer);
565 }
566
567 void SIMIX_process_change_host(smx_actor_t actor, sg_host_t dest)
568 {
569   xbt_assert((actor != nullptr), "Invalid parameters");
570   simgrid::xbt::intrusive_erase(actor->host_->extension<simgrid::simix::Host>()->process_list, *actor);
571   actor->host_ = dest;
572   dest->extension<simgrid::simix::Host>()->process_list.push_back(*actor);
573 }
574
575 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_actor_t process)
576 {
577   smx_activity_t sync_suspend = process->suspend(simcall->issuer);
578
579   if (process != simcall->issuer) {
580     SIMIX_simcall_answer(simcall);
581   } else {
582     sync_suspend->simcalls_.push_back(simcall);
583     process->waiting_synchro = sync_suspend;
584     process->waiting_synchro->suspend();
585   }
586   /* If we are suspending ourselves, then just do not finish the simcall now */
587 }
588
589 int SIMIX_process_get_maxpid() {
590   return simix_process_maxpid;
591 }
592
593 int SIMIX_process_count()
594 {
595   return simix_global->process_list.size();
596 }
597
598 void* SIMIX_process_self_get_data()
599 {
600   smx_actor_t self = SIMIX_process_self();
601
602   if (not self) {
603     return nullptr;
604   }
605   return self->get_user_data();
606 }
607
608 void SIMIX_process_self_set_data(void *data)
609 {
610   SIMIX_process_self()->set_user_data(data);
611 }
612
613
614 /* needs to be public and without simcall because it is called
615    by exceptions and logging events */
616 const char* SIMIX_process_self_get_name() {
617
618   smx_actor_t process = SIMIX_process_self();
619   if (process == nullptr || process == simix_global->maestro_process)
620     return "maestro";
621
622   return process->get_cname();
623 }
624
625 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_actor_t process, double timeout)
626 {
627   if (process->finished_) {
628     // The joined process is already finished, just wake up the issuer process right away
629     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
630     SIMIX_simcall_answer(simcall);
631     return;
632   }
633   smx_activity_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
634   sync->simcalls_.push_back(simcall);
635   simcall->issuer->waiting_synchro = sync;
636 }
637
638 smx_activity_t SIMIX_process_join(smx_actor_t issuer, smx_actor_t process, double timeout)
639 {
640   smx_activity_t res = issuer->sleep(timeout);
641   intrusive_ptr_add_ref(res.get());
642   SIMIX_process_on_exit(process,
643                         [](int, void* arg) {
644                           auto sleep = static_cast<simgrid::kernel::activity::SleepImpl*>(arg);
645                           if (sleep->surf_sleep)
646                             sleep->surf_sleep->finish(simgrid::kernel::resource::Action::State::FINISHED);
647                           intrusive_ptr_release(sleep);
648                         },
649                         res.get());
650   return res;
651 }
652
653 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
654 {
655   if (MC_is_active() || MC_record_replay_is_active()) {
656     MC_process_clock_add(simcall->issuer, duration);
657     simcall_process_sleep__set__result(simcall, SIMIX_DONE);
658     SIMIX_simcall_answer(simcall);
659     return;
660   }
661   smx_activity_t sync = simcall->issuer->sleep(duration);
662   sync->simcalls_.push_back(simcall);
663   simcall->issuer->waiting_synchro = sync;
664 }
665
666 void SIMIX_process_sleep_destroy(smx_activity_t synchro)
667 {
668   XBT_DEBUG("Destroy sleep synchro %p", synchro.get());
669   simgrid::kernel::activity::SleepImplPtr sleep =
670       boost::dynamic_pointer_cast<simgrid::kernel::activity::SleepImpl>(synchro);
671
672   if (sleep->surf_sleep) {
673     sleep->surf_sleep->unref();
674     sleep->surf_sleep = nullptr;
675   }
676 }
677
678 /**
679  * \brief Calling this function makes the process to yield.
680  *
681  * Only the current process can call this function, giving back the control to maestro.
682  *
683  * \param self the current process
684  */
685 void SIMIX_process_yield(smx_actor_t self)
686 {
687   XBT_DEBUG("Yield actor '%s'", self->get_cname());
688
689   /* Go into sleep and return control to maestro */
690   self->context_->suspend();
691
692   /* Ok, maestro returned control to us */
693   XBT_DEBUG("Control returned to me: '%s'", self->get_cname());
694
695   if (self->context_->iwannadie) {
696     XBT_DEBUG("I wanna die!");
697     self->finished_ = true;
698     /* execute the on_exit functions */
699     SIMIX_process_on_exit_runall(self);
700     /* Add the process to the list of process to restart, only if the host is down */
701     if (self->auto_restart_ && self->host_->is_off()) {
702       SIMIX_host_add_auto_restart_process(self->host_, self);
703     }
704     XBT_DEBUG("Process %s@%s is dead", self->get_cname(), self->host_->get_cname());
705     self->context_->stop();
706   }
707
708   if (self->suspended_) {
709     XBT_DEBUG("Hey! I'm suspended.");
710     xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
711     self->suspended_ = 0;
712     self->suspend(self);
713   }
714
715   if (self->exception != nullptr) {
716     XBT_DEBUG("Wait, maestro left me an exception");
717     std::exception_ptr exception = std::move(self->exception);
718     self->exception = nullptr;
719     std::rethrow_exception(std::move(exception));
720   }
721
722   if (SMPI_switch_data_segment && not self->finished_) {
723     SMPI_switch_data_segment(self->iface());
724   }
725 }
726
727 /** @brief Returns the list of processes to run. */
728 const std::vector<smx_actor_t>& simgrid::simix::process_get_runnable()
729 {
730   return simix_global->process_to_run;
731 }
732
733 /** @brief Returns the process from PID. */
734 smx_actor_t SIMIX_process_from_PID(aid_t PID)
735 {
736   auto process = simix_global->process_list.find(PID);
737   return process == simix_global->process_list.end() ? nullptr : process->second;
738 }
739
740 void SIMIX_process_on_exit_runall(smx_actor_t process) {
741   simgrid::s4u::Actor::on_destruction(process->iface());
742   smx_process_exit_status_t exit_status = (process->context_->iwannadie) ? SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
743   while (not process->on_exit.empty()) {
744     s_smx_process_exit_fun_t exit_fun = process->on_exit.back();
745     process->on_exit.pop_back();
746     (exit_fun.fun)(exit_status, exit_fun.arg);
747   }
748 }
749
750 void SIMIX_process_on_exit(smx_actor_t process, int_f_pvoid_pvoid_t fun, void* data)
751 {
752   SIMIX_process_on_exit(process, [fun](int a, void* b) { fun((void*)(intptr_t)a, b); }, data);
753 }
754
755 void SIMIX_process_on_exit(smx_actor_t process, std::function<void(int, void*)> fun, void* data)
756 {
757   xbt_assert(process, "current process not found: are you in maestro context ?");
758
759   process->on_exit.emplace_back(s_smx_process_exit_fun_t{fun, data});
760 }
761
762 /** @brief Restart a process, starting it again from the beginning. */
763 /**
764  * \ingroup simix_process_management
765  * \brief Creates and runs a new SIMIX process.
766  *
767  * The structure and the corresponding thread are created and put in the list of ready processes.
768  *
769  * \param name a name for the process. It is for user-level information and can be nullptr.
770  * \param code the main function of the process
771  * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can
772  * be nullptr.
773  * It can be retrieved with the method ActorImpl::getUserData().
774  * \param host where the new agent is executed.
775  * \param argc first argument passed to \a code
776  * \param argv second argument passed to \a code
777  * \param properties the properties of the process
778  */
779 smx_actor_t simcall_process_create(const char* name, xbt_main_func_t code, void* data, sg_host_t host, int argc,
780                                    char** argv, std::unordered_map<std::string, std::string>* properties)
781 {
782   if (name == nullptr)
783     name = "";
784   auto wrapped_code = simgrid::xbt::wrap_main(code, argc, argv);
785   for (int i = 0; i != argc; ++i)
786     xbt_free(argv[i]);
787   xbt_free(argv);
788   smx_actor_t res = simcall_process_create(name, std::move(wrapped_code), data, host, properties);
789   return res;
790 }
791
792 smx_actor_t simcall_process_create(const char* name, simgrid::simix::ActorCode code, void* data, sg_host_t host,
793                                    std::unordered_map<std::string, std::string>* properties)
794 {
795   if (name == nullptr)
796     name = "";
797   smx_actor_t self = SIMIX_process_self();
798   return simgrid::simix::simcall([name, code, data, host, properties, self] {
799     return SIMIX_process_create(name, std::move(code), data, host, properties, self);
800   });
801 }