1 /* Copyright (c) 2007-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <boost/range/algorithm.hpp>
9 #include "src/surf/surf_interface.hpp"
10 #include "smx_private.h"
11 #include "xbt/sysdep.h"
15 #include "src/mc/mc_replay.h"
16 #include "src/mc/Client.hpp"
17 #include "src/msg/msg_private.h"
19 #include "src/simix/SynchroSleep.hpp"
20 #include "src/simix/SynchroRaw.hpp"
21 #include "src/simix/SynchroIo.hpp"
24 #include "src/smpi/private.h"
27 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
29 unsigned long simix_process_maxpid = 0;
32 * \brief Returns the current agent.
34 * This functions returns the currently running SIMIX process.
36 * \return The SIMIX process
38 smx_process_t SIMIX_process_self(void)
40 smx_context_t self_context = SIMIX_context_self();
42 return self_context ? self_context->process() : nullptr;
46 * \brief Returns whether a process has pending asynchronous communications.
47 * \return true if there are asynchronous communications in this process
49 int SIMIX_process_has_pending_comms(smx_process_t process) {
51 return xbt_fifo_size(process->comms) > 0;
55 * \brief Moves a process to the list of processes to destroy.
57 void SIMIX_process_cleanup(smx_process_t process)
59 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
60 process->name.c_str(), process, process->waiting_synchro);
62 SIMIX_process_on_exit_runall(process);
64 /* Unregister from the kill timer if any */
65 if (process->kill_timer != nullptr)
66 SIMIX_timer_remove(process->kill_timer);
68 xbt_os_mutex_acquire(simix_global->mutex);
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);
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 */
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;
84 /* I'm not supposed to destroy a detached comm from the sender side, */
86 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
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;
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);
103 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
107 XBT_DEBUG("%p should not be run anymore",process);
108 xbt_swag_remove(process, simix_global->process_list);
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;
114 xbt_os_mutex_release(simix_global->mutex);
120 * Should be called some time to time to free the memory allocated for processes
121 * that have finished (or killed).
123 void SIMIX_process_empty_trash(void)
125 smx_process_t process = nullptr;
127 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
128 XBT_DEBUG("Getting rid of %p",process);
129 delete process->context;
130 xbt_dict_free(&process->properties);
131 xbt_fifo_free(process->comms);
132 xbt_dynar_free(&process->on_exit);
140 void create_maestro(std::function<void()> code)
142 smx_process_t maestro = nullptr;
143 /* Create maestro process and intilialize it */
144 maestro = new simgrid::simix::Process();
145 maestro->pid = simix_process_maxpid++;
148 maestro->data = nullptr;
151 maestro->context = SIMIX_context_new(std::function<void()>(), nullptr, maestro);
154 xbt_die("simix is not initialized, please call MSG_init first");
156 simix_global->context_factory->create_maestro(code, maestro);
159 maestro->simcall.issuer = maestro;
160 simix_global->maestro_process = maestro;
167 * \brief Creates and runs the maestro process
169 void SIMIX_maestro_create(void (*code)(void*), void* data)
171 simgrid::simix::create_maestro(std::bind(code, data));
175 * \brief Stops a process.
177 * Stops the process, execute all the registered on_exit functions,
178 * register it to the list of the process to restart if needed
179 * and stops its context.
181 void SIMIX_process_stop(smx_process_t arg) {
182 /* execute the on_exit functions */
183 SIMIX_process_on_exit_runall(arg);
184 /* Add the process to the list of process to restart, only if the host is down */
185 if (arg->auto_restart && arg->host->isOff()) {
186 SIMIX_host_add_auto_restart_process(arg->host, arg->name.c_str(),
187 arg->code, arg->data,
188 sg_host_get_name(arg->host),
189 SIMIX_timer_get_date(arg->kill_timer),
193 XBT_DEBUG("Process %s (%s) is dead",
194 arg->name.c_str(), sg_host_get_name(arg->host));
195 arg->context->stop();
199 * \brief Internal function to create a process.
201 * This function actually creates the process.
202 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
203 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
205 * \return the process created
207 smx_process_t SIMIX_process_create(
209 std::function<void()> code,
211 const char *hostname,
213 xbt_dict_t properties,
215 smx_process_t parent_process)
217 smx_process_t process = nullptr;
218 sg_host_t host = sg_host_by_name(hostname);
220 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
223 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
228 process = new simgrid::simix::Process();
230 xbt_assert(code && host != nullptr, "Invalid parameters");
232 process->pid = simix_process_maxpid++;
233 process->name = simgrid::xbt::string(name);
234 process->host = host;
235 process->data = data;
236 process->comms = xbt_fifo_new();
237 process->simcall.issuer = process;
238 /* Initiliaze data segment to default value */
239 SIMIX_segment_index_set(process, -1);
241 if (parent_process != nullptr) {
242 process->ppid = SIMIX_process_get_PID(parent_process);
243 /* SMPI process have their own data segment and
244 each other inherit from their father */
246 if(smpi_privatize_global_variables){
247 if( parent_process->pid != 0){
248 SIMIX_segment_index_set(process, parent_process->segment_index);
250 SIMIX_segment_index_set(process, process->pid - 1);
258 /* Process data for auto-restart */
259 process->auto_restart = auto_restart;
260 process->code = code;
262 XBT_VERB("Create context %s", process->name.c_str());
263 process->context = SIMIX_context_new(
265 simix_global->cleanup_process_function, process);
268 process->properties = properties;
270 /* Add the process to it's host process list */
271 xbt_swag_insert(process, sg_host_simix(host)->process_list);
273 XBT_DEBUG("Start context '%s'", process->name.c_str());
275 /* Now insert it in the global process list and in the process to run list */
276 xbt_swag_insert(process, simix_global->process_list);
277 XBT_DEBUG("Inserting %s(%s) in the to_run list",
278 process->name.c_str(), sg_host_get_name(host));
279 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
281 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
282 XBT_DEBUG("Process %s(%s) will be kill at time %f",
283 process->name.c_str(), sg_host_get_name(process->host), kill_time);
284 process->kill_timer = SIMIX_timer_set(kill_time, [=]() {
285 simix_global->kill_process_function(process);
289 /* Tracing the process creation */
290 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
295 smx_process_t SIMIX_process_attach(
298 const char* hostname,
299 xbt_dict_t properties,
300 smx_process_t parent_process)
302 // This is mostly a copy/paste from SIMIX_process_new(),
303 // it'd be nice to share some code between those two functions.
305 sg_host_t host = sg_host_by_name(hostname);
306 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
309 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
314 smx_process_t process = new simgrid::simix::Process();
316 process->pid = simix_process_maxpid++;
317 process->name = std::string(name);
318 process->host = host;
319 process->data = data;
320 process->comms = xbt_fifo_new();
321 process->simcall.issuer = process;
323 /* Initiliaze data segment to default value */
324 SIMIX_segment_index_set(process, -1);
325 if (parent_process != nullptr) {
326 process->ppid = SIMIX_process_get_PID(parent_process);
327 /* SMPI process have their own data segment and
328 each other inherit from their father */
330 if(smpi_privatize_global_variables){
331 if(parent_process->pid != 0){
332 SIMIX_segment_index_set(process, parent_process->segment_index);
334 SIMIX_segment_index_set(process, process->pid - 1);
340 /* Process data for auto-restart */
341 process->auto_restart = false;
342 process->code = nullptr;
344 XBT_VERB("Create context %s", process->name.c_str());
346 xbt_die("simix is not initialized, please call MSG_init first");
347 process->context = simix_global->context_factory->attach(
348 simix_global->cleanup_process_function, process);
351 process->properties = properties;
353 /* Add the process to it's host process list */
354 xbt_swag_insert(process, sg_host_simix(host)->process_list);
356 /* Now insert it in the global process list and in the process to run list */
357 xbt_swag_insert(process, simix_global->process_list);
358 XBT_DEBUG("Inserting %s(%s) in the to_run list",
359 process->name.c_str(), sg_host_get_name(host));
360 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
362 /* Tracing the process creation */
363 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
365 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
367 xbt_die("Not a suitable context");
369 context->attach_start();
373 void SIMIX_process_detach(void)
375 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
377 xbt_die("Not a suitable context");
379 simix_global->cleanup_process_function(context->process());
381 // Let maestro ignore we are still alive:
382 // xbt_swag_remove(context->process(), simix_global->process_list);
384 // TODDO, Remove from proces list:
385 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
387 context->attach_stop();
392 * \brief Executes the processes from simix_global->process_to_run.
394 * The processes of simix_global->process_to_run are run (in parallel if
395 * possible). On exit, simix_global->process_to_run is empty, and
396 * simix_global->process_that_ran contains the list of processes that just ran.
397 * The two lists are swapped so, be careful when using them before and after a
398 * call to this function.
400 void SIMIX_process_runall(void)
402 SIMIX_context_runall();
404 xbt_dynar_t tmp = simix_global->process_that_ran;
405 simix_global->process_that_ran = simix_global->process_to_run;
406 simix_global->process_to_run = tmp;
407 xbt_dynar_reset(simix_global->process_to_run);
410 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
411 SIMIX_process_kill(process, simcall->issuer);
414 * \brief Internal function to kill a SIMIX process.
416 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
417 * or directly for SIMIX internal purposes.
419 * \param process poor victim
420 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
422 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
424 XBT_DEBUG("Killing process %s on %s",
425 process->name.c_str(), sg_host_get_name(process->host));
427 process->context->iwannadie = 1;
428 process->blocked = 0;
429 process->suspended = 0;
430 process->exception = nullptr;
432 /* destroy the blocking synchro if any */
433 if (process->waiting_synchro) {
435 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
436 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
437 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
438 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
439 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
441 if (exec != nullptr) {
444 } else if (comm != nullptr) {
445 xbt_fifo_remove(process->comms, process->waiting_synchro);
448 // Remove first occurence of &process->simcall:
449 auto i = boost::range::find(
450 process->waiting_synchro->simcalls,
452 if (i != process->waiting_synchro->simcalls.end())
453 process->waiting_synchro->simcalls.remove(&process->simcall);
457 } else if (sleep != nullptr) {
458 SIMIX_process_sleep_destroy(process->waiting_synchro);
460 } else if (raw != nullptr) {
461 SIMIX_synchro_stop_waiting(process, &process->simcall);
462 delete process->waiting_synchro;
464 } else if (io != nullptr) {
465 SIMIX_io_destroy(process->waiting_synchro);
469 switch (process->waiting_synchro->type) {
470 case SIMIX_SYNC_JOIN:
471 SIMIX_process_sleep_destroy(process->waiting_synchro);
475 process->waiting_synchro = nullptr;
477 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
478 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
479 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
484 /** @brief Ask another process to raise the given exception
486 * @param cat category of exception
487 * @param value value associated to the exception
488 * @param msg string information associated to the exception
490 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
491 SMX_EXCEPTION(process, cat, value, msg);
493 if (process->suspended)
494 SIMIX_process_resume(process,SIMIX_process_self());
496 /* cancel the blocking synchro if any */
497 if (process->waiting_synchro) {
499 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
500 if (exec != nullptr) {
501 SIMIX_execution_cancel(process->waiting_synchro);
504 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
505 if (comm != nullptr) {
506 xbt_fifo_remove(process->comms, comm);
510 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
511 if (sleep != nullptr) {
512 SIMIX_process_sleep_destroy(process->waiting_synchro);
513 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
514 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
515 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
519 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
520 if (raw != nullptr) {
521 SIMIX_synchro_stop_waiting(process, &process->simcall);
524 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
526 SIMIX_io_destroy(process->waiting_synchro);
529 process->waiting_synchro = nullptr;
533 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
534 SIMIX_process_killall(simcall->issuer, reset_pid);
537 * \brief Kills all running processes.
538 * \param issuer this one will not be killed
540 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
542 smx_process_t p = nullptr;
544 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
546 SIMIX_process_kill(p,issuer);
551 simix_process_maxpid = reset_pid;
553 SIMIX_context_runall();
555 SIMIX_process_empty_trash();
558 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
560 process->new_host = dest;
562 void SIMIX_process_change_host(smx_process_t process,
565 xbt_assert((process != nullptr), "Invalid parameters");
566 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
567 process->host = dest;
568 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
572 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
574 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
576 if (process != simcall->issuer) {
577 SIMIX_simcall_answer(simcall);
579 sync_suspend->simcalls.push_back(simcall);
580 process->waiting_synchro = sync_suspend;
581 process->waiting_synchro->suspend();
583 /* If we are suspending ourselves, then just do not finish the simcall now */
586 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
588 if (process->suspended) {
589 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
593 process->suspended = 1;
595 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
596 if (process != issuer) {
598 if (process->waiting_synchro)
599 process->waiting_synchro->suspend();
600 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
604 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
605 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
609 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
610 SIMIX_process_resume(process, simcall->issuer);
613 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
615 XBT_IN("process = %p, issuer = %p", process, issuer);
617 if(process->context->iwannadie) {
618 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
622 if(!process->suspended) return;
623 process->suspended = 0;
625 /* If we are resuming another process, resume the synchronization it was waiting for
626 if any. Otherwise add it to the list of process to run in the next round. */
627 if (process != issuer) {
629 if (process->waiting_synchro) {
630 process->waiting_synchro->resume();
632 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
637 int SIMIX_process_get_maxpid(void) {
638 return simix_process_maxpid;
641 int SIMIX_process_count(void)
643 return xbt_swag_size(simix_global->process_list);
646 int SIMIX_process_get_PID(smx_process_t self){
653 int SIMIX_process_get_PPID(smx_process_t self){
660 void* SIMIX_process_self_get_data()
662 smx_process_t self = SIMIX_process_self();
667 return SIMIX_process_get_data(self);
670 void SIMIX_process_self_set_data(void *data)
672 smx_process_t self = SIMIX_process_self();
674 SIMIX_process_set_data(self, data);
677 void* SIMIX_process_get_data(smx_process_t process)
679 return process->data;
682 void SIMIX_process_set_data(smx_process_t process, void *data)
684 process->data = data;
687 sg_host_t SIMIX_process_get_host(smx_process_t process)
689 return process->host;
692 /* needs to be public and without simcall because it is called
693 by exceptions and logging events */
694 const char* SIMIX_process_self_get_name(void) {
696 smx_process_t process = SIMIX_process_self();
697 if (process == nullptr || process == simix_global->maestro_process)
700 return SIMIX_process_get_name(process);
703 const char* SIMIX_process_get_name(smx_process_t process)
705 return process->name.c_str();
708 smx_process_t SIMIX_process_get_by_name(const char* name)
711 xbt_swag_foreach(proc, simix_global->process_list) {
712 if (proc->name == name)
718 int SIMIX_process_is_suspended(smx_process_t process)
720 return process->suspended;
723 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
725 return process->properties;
728 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
730 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
731 sync->simcalls.push_back(simcall);
732 simcall->issuer->waiting_synchro = sync;
735 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
736 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
738 if (sleep->surf_sleep) {
739 sleep->surf_sleep->cancel();
741 while (!sleep->simcalls.empty()) {
742 smx_simcall_t simcall = sleep->simcalls.front();
743 sleep->simcalls.pop_front();
744 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
745 simcall->issuer->waiting_synchro = nullptr;
746 if (simcall->issuer->suspended) {
747 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
748 simcall->issuer->suspended = 0;
749 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
751 SIMIX_simcall_answer(simcall);
754 sleep->surf_sleep->unref();
755 sleep->surf_sleep = nullptr;
761 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
763 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
764 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
768 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
770 if (MC_is_active() || MC_record_replay_is_active()) {
771 MC_process_clock_add(simcall->issuer, duration);
772 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
773 SIMIX_simcall_answer(simcall);
776 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
777 sync->simcalls.push_back(simcall);
778 simcall->issuer->waiting_synchro = sync;
781 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
783 sg_host_t host = process->host;
785 /* check if the host is active */
787 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
789 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
790 synchro->host = host;
791 synchro->surf_sleep = surf_host_sleep(host, duration);
792 synchro->surf_sleep->setData(synchro);
793 XBT_DEBUG("Create sleep synchronization %p", synchro);
798 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
800 XBT_DEBUG("Destroy synchro %p", synchro);
801 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
803 if (sleep->surf_sleep) {
804 sleep->surf_sleep->unref();
805 sleep->surf_sleep = nullptr;
810 * \brief Calling this function makes the process to yield.
812 * Only the current process can call this function, giving back the control to
815 * \param self the current process
817 void SIMIX_process_yield(smx_process_t self)
819 XBT_DEBUG("Yield process '%s'", self->name.c_str());
821 /* Go into sleep and return control to maestro */
822 self->context->suspend();
824 /* Ok, maestro returned control to us */
825 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
827 if (self->new_host) {
828 SIMIX_process_change_host(self, self->new_host);
829 self->new_host = nullptr;
832 if (self->context->iwannadie){
833 XBT_DEBUG("I wanna die!");
834 SIMIX_process_stop(self);
837 if (self->suspended) {
838 XBT_DEBUG("Hey! I'm suspended.");
839 xbt_assert(self->exception != nullptr, "Gasp! This exception may be lost by subsequent calls.");
841 SIMIX_process_suspend(self, self);
844 if (self->exception != nullptr) {
845 XBT_DEBUG("Wait, maestro left me an exception");
846 std::exception_ptr exception = std::move(self->exception);
847 self->exception = nullptr;
848 std::rethrow_exception(std::move(exception));
851 if(SMPI_switch_data_segment && self->segment_index != -1){
852 SMPI_switch_data_segment(self->segment_index);
856 /* callback: termination */
857 void SIMIX_process_exception_terminate(xbt_ex_t * e)
863 smx_context_t SIMIX_process_get_context(smx_process_t p) {
867 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
872 * \brief Returns the list of processes to run.
874 xbt_dynar_t SIMIX_process_get_runnable(void)
876 return simix_global->process_to_run;
880 * \brief Returns the process from PID.
882 smx_process_t SIMIX_process_from_PID(int PID)
885 xbt_swag_foreach(proc, simix_global->process_list) {
886 if (proc->pid == (unsigned long) PID)
892 /** @brief returns a dynar containg all currently existing processes */
893 xbt_dynar_t SIMIX_processes_as_dynar(void) {
895 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),nullptr);
896 xbt_swag_foreach(proc, simix_global->process_list) {
897 xbt_dynar_push(res,&proc);
903 void SIMIX_process_on_exit_runall(smx_process_t process) {
904 s_smx_process_exit_fun_t exit_fun;
905 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
906 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
907 while (!xbt_dynar_is_empty(process->on_exit)) {
908 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
909 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
913 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
914 xbt_assert(process, "current process not found: are you in maestro context ?");
916 if (!process->on_exit) {
917 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), nullptr);
920 s_smx_process_exit_fun_t exit_fun = {fun, data};
922 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
926 * \brief Sets the auto-restart status of the process.
927 * If set to 1, the process will be automatically restarted when its host
930 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
931 process->auto_restart = auto_restart;
934 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
935 return SIMIX_process_restart(process, simcall->issuer);
937 /** @brief Restart a process, starting it again from the beginning. */
938 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
939 XBT_DEBUG("Restarting process %s on %s",
940 process->name.c_str(), sg_host_get_name(process->host));
942 //retrieve the arguments of the old process
943 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
944 simgrid::simix::ProcessArg arg;
945 arg.name = process->name;
946 arg.code = process->code;
947 arg.hostname = sg_host_get_name(process->host);
948 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
949 arg.data = process->data;
950 arg.properties = nullptr;
951 arg.auto_restart = process->auto_restart;
953 //kill the old process
954 SIMIX_process_kill(process, issuer);
956 //start the new process
957 if (simix_global->create_process_function)
958 return simix_global->create_process_function(
959 arg.name.c_str(), std::move(arg.code), arg.data,
960 arg.hostname, arg.kill_time,
961 arg.properties, arg.auto_restart,
964 return simcall_process_create(
965 arg.name.c_str(), std::move(arg.code), arg.data,
966 arg.hostname, arg.kill_time,
967 arg.properties, arg.auto_restart);
970 void SIMIX_segment_index_set(smx_process_t proc, int index){
971 proc->segment_index = index;
974 smx_process_t simcall_process_create(
975 const char *name, std::function<void()> code, void *data,
976 const char *hostname, double kill_time,
977 xbt_dict_t properties, int auto_restart)
981 smx_process_t self = SIMIX_process_self();
982 return simgrid::simix::kernel([&] {
983 return SIMIX_process_create(name,
984 std::move(code), data, hostname,
985 kill_time, properties, auto_restart,