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 "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
9 #include "xbt/sysdep.h"
13 #include "src/mc/mc_replay.h"
14 #include "src/mc/Client.hpp"
15 #include "src/msg/msg_private.h"
17 #include "src/simix/SynchroSleep.hpp"
18 #include "src/simix/SynchroRaw.hpp"
19 #include "src/simix/SynchroIo.hpp"
22 #include "src/smpi/private.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix, "Logging specific to SIMIX (process)");
27 unsigned long simix_process_maxpid = 0;
30 * \brief Returns the current agent.
32 * This functions returns the currently running SIMIX process.
34 * \return The SIMIX process
36 smx_process_t SIMIX_process_self(void)
38 smx_context_t self_context = SIMIX_context_self();
40 return self_context ? self_context->process() : NULL;
44 * \brief Returns whether a process has pending asynchronous communications.
45 * \return true if there are asynchronous communications in this process
47 int SIMIX_process_has_pending_comms(smx_process_t process) {
49 return xbt_fifo_size(process->comms) > 0;
53 * \brief Moves a process to the list of processes to destroy.
55 void SIMIX_process_cleanup(smx_process_t process)
57 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
58 process->name.c_str(), process, process->waiting_synchro);
60 SIMIX_process_on_exit_runall(process);
62 /* Unregister from the kill timer if any */
63 if (process->kill_timer != NULL)
64 SIMIX_timer_remove(process->kill_timer);
66 xbt_os_mutex_acquire(simix_global->mutex);
68 /* cancel non-blocking communications */
69 smx_synchro_t synchro;
70 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
71 simgrid::simix::Comm *comm = static_cast<simgrid::simix::Comm*>(synchro);
73 /* make sure no one will finish the comm after this process is destroyed,
74 * because src_proc or dst_proc would be an invalid pointer */
77 if (comm->src_proc == process) {
78 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
79 comm, comm->detached, (int)comm->state, comm->src_proc, comm->dst_proc);
80 comm->src_proc = NULL;
82 /* I'm not supposed to destroy a detached comm from the sender side, */
84 XBT_DEBUG("Don't destroy it since it's a detached comm and I'm the sender");
89 else if (comm->dst_proc == process){
90 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
91 comm, (int)comm->state, comm->src_proc, comm->dst_proc);
92 comm->dst_proc = NULL;
94 if (comm->detached && comm->src_proc != NULL) {
95 /* the comm will be freed right now, remove it from the sender */
96 xbt_fifo_remove(comm->src_proc->comms, comm);
101 xbt_die("Communication synchro %p is in my list but I'm not the sender nor the receiver", synchro);
105 XBT_DEBUG("%p should not be run anymore",process);
106 xbt_swag_remove(process, simix_global->process_list);
108 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
109 xbt_swag_insert(process, simix_global->process_to_destroy);
110 process->context->iwannadie = 0;
112 xbt_os_mutex_release(simix_global->mutex);
118 * Should be called some time to time to free the memory allocated for processes
119 * that have finished (or killed).
121 void SIMIX_process_empty_trash(void)
123 smx_process_t process = NULL;
125 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
126 XBT_DEBUG("Getting rid of %p",process);
128 delete process->context;
130 /* Free the exception allocated at creation time */
131 free(process->running_ctx);
132 xbt_dict_free(&process->properties);
134 xbt_fifo_free(process->comms);
136 xbt_dynar_free(&process->on_exit);
145 void create_maestro(std::function<void()> code)
147 smx_process_t maestro = NULL;
148 /* Create maestro process and intilialize it */
149 maestro = new simgrid::simix::Process();
150 maestro->pid = simix_process_maxpid++;
153 maestro->data = nullptr;
154 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
155 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
158 maestro->context = SIMIX_context_new(std::function<void()>(), NULL, maestro);
161 xbt_die("simix is not initialized, please call MSG_init first");
163 simix_global->context_factory->create_maestro(code, maestro);
166 maestro->simcall.issuer = maestro;
167 simix_global->maestro_process = maestro;
174 * \brief Creates and runs the maestro process
176 void SIMIX_maestro_create(void (*code)(void*), void* data)
178 simgrid::simix::create_maestro(std::bind(code, data));
182 * \brief Stops a process.
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.
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->args.argc(), arg->args.argv(), arg->properties,
200 XBT_DEBUG("Process %s (%s) is dead",
201 arg->name.c_str(), sg_host_get_name(arg->host));
202 arg->context->stop();
205 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
207 xbt_main_func_t code,
209 const char *hostname,
211 int argc, char **argv,
212 xbt_dict_t properties,
214 return (void*)SIMIX_process_create(name, code, data, hostname,
215 kill_time, argc, argv, properties, auto_restart,
219 static void kill_process(void* process)
221 simix_global->kill_process_function((smx_process_t) process);
225 * \brief Internal function to create a process.
227 * This function actually creates the process.
228 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
229 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
231 * \return the process created
233 smx_process_t SIMIX_process_create(
235 xbt_main_func_t code,
237 const char *hostname,
239 int argc, char **argv,
240 xbt_dict_t properties,
242 smx_process_t parent_process)
244 smx_process_t process = NULL;
245 sg_host_t host = sg_host_by_name(hostname);
247 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
251 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
253 for (i = 0; i < argc; i++)
258 process = new simgrid::simix::Process();
260 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
262 process->pid = simix_process_maxpid++;
263 process->name = std::string(name);
264 process->host = host;
265 process->data = data;
266 process->comms = xbt_fifo_new();
267 process->simcall.issuer = process;
268 /* Initiliaze data segment to default value */
269 SIMIX_segment_index_set(process, -1);
271 if (parent_process != NULL) {
272 process->ppid = SIMIX_process_get_PID(parent_process);
273 /* SMPI process have their own data segment and
274 each other inherit from their father */
276 if(smpi_privatize_global_variables){
277 if( parent_process->pid != 0){
278 SIMIX_segment_index_set(process, parent_process->segment_index);
280 SIMIX_segment_index_set(process, process->pid - 1);
288 /* Process data for auto-restart */
289 process->auto_restart = auto_restart;
290 process->code = code;
291 process->args.assign(argc, argv);
293 XBT_VERB("Create context %s", process->name.c_str());
294 process->context = SIMIX_context_new(
295 simgrid::simix::wrap_main(code, argc, argv),
296 simix_global->cleanup_process_function, process);
298 for (int i = 0; i < argc; i++)
302 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
303 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
306 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
310 process->properties = properties;
312 /* Add the process to it's host process list */
313 xbt_swag_insert(process, sg_host_simix(host)->process_list);
315 XBT_DEBUG("Start context '%s'", process->name.c_str());
317 /* Now insert it in the global process list and in the process to run list */
318 xbt_swag_insert(process, simix_global->process_list);
319 XBT_DEBUG("Inserting %s(%s) in the to_run list",
320 process->name.c_str(), sg_host_get_name(host));
321 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
323 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
324 XBT_DEBUG("Process %s(%s) will be kill at time %f",
325 process->name.c_str(), sg_host_get_name(process->host), kill_time);
326 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
329 /* Tracing the process creation */
330 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
335 smx_process_t SIMIX_process_attach(
338 const char* hostname,
339 xbt_dict_t properties,
340 smx_process_t parent_process)
342 // This is mostly a copy/paste from SIMIX_process_new(),
343 // it'd be nice to share some code between those two functions.
345 sg_host_t host = sg_host_by_name(hostname);
346 XBT_DEBUG("Attach process %s on host '%s'", name, hostname);
349 XBT_WARN("Cannot launch process '%s' on failed host '%s'",
354 smx_process_t process = new simgrid::simix::Process();
356 process->pid = simix_process_maxpid++;
357 process->name = std::string(name);
358 process->host = host;
359 process->data = data;
360 process->comms = xbt_fifo_new();
361 process->simcall.issuer = process;
363 /* Initiliaze data segment to default value */
364 SIMIX_segment_index_set(process, -1);
365 if (parent_process != NULL) {
366 process->ppid = SIMIX_process_get_PID(parent_process);
367 /* SMPI process have their own data segment and
368 each other inherit from their father */
370 if(smpi_privatize_global_variables){
371 if(parent_process->pid != 0){
372 SIMIX_segment_index_set(process, parent_process->segment_index);
374 SIMIX_segment_index_set(process, process->pid - 1);
380 /* Process data for auto-restart */
381 process->auto_restart = false;
382 process->code = nullptr;
384 XBT_VERB("Create context %s", process->name.c_str());
386 xbt_die("simix is not initialized, please call MSG_init first");
387 process->context = simix_global->context_factory->attach(
388 simix_global->cleanup_process_function, process);
390 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
391 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
394 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
398 process->properties = properties;
400 /* Add the process to it's host process list */
401 xbt_swag_insert(process, sg_host_simix(host)->process_list);
403 /* Now insert it in the global process list and in the process to run list */
404 xbt_swag_insert(process, simix_global->process_list);
405 XBT_DEBUG("Inserting %s(%s) in the to_run list",
406 process->name.c_str(), sg_host_get_name(host));
407 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
409 /* Tracing the process creation */
410 TRACE_msg_process_create(process->name.c_str(), process->pid, process->host);
412 auto context = dynamic_cast<simgrid::simix::AttachContext*>(process->context);
414 xbt_die("Not a suitable context");
416 context->attach_start();
420 void SIMIX_process_detach(void)
422 auto context = dynamic_cast<simgrid::simix::AttachContext*>(SIMIX_context_self());
424 xbt_die("Not a suitable context");
426 simix_global->cleanup_process_function(context->process());
428 // Let maestro ignore we are still alive:
429 // xbt_swag_remove(context->process(), simix_global->process_list);
431 // TODDO, Remove from proces list:
432 // xbt_swag_remove(process, sg_host_simix(host)->process_list);
434 context->attach_stop();
439 * \brief Executes the processes from simix_global->process_to_run.
441 * The processes of simix_global->process_to_run are run (in parallel if
442 * possible). On exit, simix_global->process_to_run is empty, and
443 * simix_global->process_that_ran contains the list of processes that just ran.
444 * The two lists are swapped so, be careful when using them before and after a
445 * call to this function.
447 void SIMIX_process_runall(void)
449 SIMIX_context_runall();
451 xbt_dynar_t tmp = simix_global->process_that_ran;
452 simix_global->process_that_ran = simix_global->process_to_run;
453 simix_global->process_to_run = tmp;
454 xbt_dynar_reset(simix_global->process_to_run);
457 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
458 SIMIX_process_kill(process, simcall->issuer);
461 * \brief Internal function to kill a SIMIX process.
463 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
464 * or directly for SIMIX internal purposes.
466 * \param process poor victim
467 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
469 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
471 XBT_DEBUG("Killing process %s on %s",
472 process->name.c_str(), sg_host_get_name(process->host));
474 process->context->iwannadie = 1;
475 process->blocked = 0;
476 process->suspended = 0;
477 process->doexception = 0;
479 /* destroy the blocking synchro if any */
480 if (process->waiting_synchro) {
482 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
483 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
484 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
485 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
486 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
488 if (exec != nullptr) {
491 } else if (comm != nullptr) {
492 xbt_fifo_remove(process->comms, process->waiting_synchro);
494 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
497 } else if (sleep != nullptr) {
498 SIMIX_process_sleep_destroy(process->waiting_synchro);
500 } else if (raw != nullptr) {
501 SIMIX_synchro_stop_waiting(process, &process->simcall);
502 delete process->waiting_synchro;
504 } else if (io != nullptr) {
505 SIMIX_io_destroy(process->waiting_synchro);
509 switch (process->waiting_synchro->type) {
510 case SIMIX_SYNC_JOIN:
511 SIMIX_process_sleep_destroy(process->waiting_synchro);
515 process->waiting_synchro = NULL;
517 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
518 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
519 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
524 /** @brief Ask another process to raise the given exception
526 * @param cat category of exception
527 * @param value value associated to the exception
528 * @param msg string information associated to the exception
530 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
531 SMX_EXCEPTION(process, cat, value, msg);
533 if (process->suspended)
534 SIMIX_process_resume(process,SIMIX_process_self());
536 /* cancel the blocking synchro if any */
537 if (process->waiting_synchro) {
539 simgrid::simix::Exec *exec = dynamic_cast<simgrid::simix::Exec*>(process->waiting_synchro);
540 if (exec != nullptr) {
541 SIMIX_execution_cancel(process->waiting_synchro);
544 simgrid::simix::Comm *comm = dynamic_cast<simgrid::simix::Comm*>(process->waiting_synchro);
545 if (comm != nullptr) {
546 xbt_fifo_remove(process->comms, comm);
550 simgrid::simix::Sleep *sleep = dynamic_cast<simgrid::simix::Sleep*>(process->waiting_synchro);
551 if (sleep != nullptr) {
552 SIMIX_process_sleep_destroy(process->waiting_synchro);
553 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
554 XBT_DEBUG("Inserting %s in the to_run list", process->name.c_str());
555 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
559 simgrid::simix::Raw *raw = dynamic_cast<simgrid::simix::Raw*>(process->waiting_synchro);
560 if (raw != nullptr) {
561 SIMIX_synchro_stop_waiting(process, &process->simcall);
564 simgrid::simix::Io *io = dynamic_cast<simgrid::simix::Io*>(process->waiting_synchro);
566 SIMIX_io_destroy(process->waiting_synchro);
569 process->waiting_synchro = NULL;
573 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
574 SIMIX_process_killall(simcall->issuer, reset_pid);
577 * \brief Kills all running processes.
578 * \param issuer this one will not be killed
580 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
582 smx_process_t p = NULL;
584 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
586 SIMIX_process_kill(p,issuer);
591 simix_process_maxpid = reset_pid;
593 SIMIX_context_runall();
595 SIMIX_process_empty_trash();
598 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
600 process->new_host = dest;
602 void SIMIX_process_change_host(smx_process_t process,
605 xbt_assert((process != NULL), "Invalid parameters");
606 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
607 process->host = dest;
608 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
612 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
614 smx_synchro_t sync_suspend = SIMIX_process_suspend(process, simcall->issuer);
616 if (process != simcall->issuer) {
617 SIMIX_simcall_answer(simcall);
619 xbt_fifo_push(sync_suspend->simcalls, simcall);
620 process->waiting_synchro = sync_suspend;
621 process->waiting_synchro->suspend();
623 /* If we are suspending ourselves, then just do not finish the simcall now */
626 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
628 if (process->suspended) {
629 XBT_DEBUG("Process '%s' is already suspended", process->name.c_str());
633 process->suspended = 1;
635 /* If we are suspending another process that is waiting on a sync, suspend its synchronization. */
636 if (process != issuer) {
638 if (process->waiting_synchro)
639 process->waiting_synchro->suspend();
640 /* If the other process is not waiting, its suspension is delayed to when the process is rescheduled. */
644 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
645 return SIMIX_execution_start(process, "suspend", 0.0, 1.0, 0.0, 0);
649 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
650 SIMIX_process_resume(process, simcall->issuer);
653 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
655 XBT_IN("process = %p, issuer = %p", process, issuer);
657 if(process->context->iwannadie) {
658 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
662 if(!process->suspended) return;
663 process->suspended = 0;
665 /* If we are resuming another process, resume the synchronization it was waiting for
666 if any. Otherwise add it to the list of process to run in the next round. */
667 if (process != issuer) {
669 if (process->waiting_synchro) {
670 process->waiting_synchro->resume();
672 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
677 int SIMIX_process_get_maxpid(void) {
678 return simix_process_maxpid;
681 int SIMIX_process_count(void)
683 return xbt_swag_size(simix_global->process_list);
686 int SIMIX_process_get_PID(smx_process_t self){
693 int SIMIX_process_get_PPID(smx_process_t self){
700 void* SIMIX_process_self_get_data()
702 smx_process_t self = SIMIX_process_self();
707 return SIMIX_process_get_data(self);
710 void SIMIX_process_self_set_data(void *data)
712 smx_process_t self = SIMIX_process_self();
714 SIMIX_process_set_data(self, data);
717 void* SIMIX_process_get_data(smx_process_t process)
719 return process->data;
722 void SIMIX_process_set_data(smx_process_t process, void *data)
724 process->data = data;
727 sg_host_t SIMIX_process_get_host(smx_process_t process)
729 return process->host;
732 xbt_main_func_t SIMIX_process_get_code(void){
733 return SIMIX_process_self()->code;
736 /* needs to be public and without simcall because it is called
737 by exceptions and logging events */
738 const char* SIMIX_process_self_get_name(void) {
740 smx_process_t process = SIMIX_process_self();
741 if (process == NULL || process == simix_global->maestro_process)
744 return SIMIX_process_get_name(process);
747 const char* SIMIX_process_get_name(smx_process_t process)
749 return process->name.c_str();
752 smx_process_t SIMIX_process_get_by_name(const char* name)
755 xbt_swag_foreach(proc, simix_global->process_list) {
756 if (proc->name == name)
762 int SIMIX_process_is_suspended(smx_process_t process)
764 return process->suspended;
767 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
769 return process->properties;
772 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
774 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
775 xbt_fifo_push(sync->simcalls, simcall);
776 simcall->issuer->waiting_synchro = sync;
779 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t synchro){
780 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
782 if (sleep->surf_sleep) {
783 sleep->surf_sleep->cancel();
785 smx_simcall_t simcall;
786 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sleep->simcalls))) {
787 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
788 simcall->issuer->waiting_synchro = NULL;
789 if (simcall->issuer->suspended) {
790 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
791 simcall->issuer->suspended = 0;
792 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
794 SIMIX_simcall_answer(simcall);
797 sleep->surf_sleep->unref();
798 sleep->surf_sleep = NULL;
804 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
806 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
807 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
811 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
813 if (MC_is_active() || MC_record_replay_is_active()) {
814 MC_process_clock_add(simcall->issuer, duration);
815 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
816 SIMIX_simcall_answer(simcall);
819 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
820 xbt_fifo_push(sync->simcalls, simcall);
821 simcall->issuer->waiting_synchro = sync;
824 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
826 sg_host_t host = process->host;
828 /* check if the host is active */
830 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
832 simgrid::simix::Sleep *synchro = new simgrid::simix::Sleep();
833 synchro->name = NULL;
835 synchro->host = host;
836 synchro->surf_sleep = surf_host_sleep(host, duration);
837 synchro->surf_sleep->setData(synchro);
838 XBT_DEBUG("Create sleep synchronization %p", synchro);
843 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
845 XBT_DEBUG("Destroy synchro %p", synchro);
846 simgrid::simix::Sleep *sleep = static_cast<simgrid::simix::Sleep*>(synchro);
848 if (sleep->surf_sleep) {
849 sleep->surf_sleep->unref();
850 sleep->surf_sleep = NULL;
855 * \brief Calling this function makes the process to yield.
857 * Only the current process can call this function, giving back the control to
860 * \param self the current process
862 void SIMIX_process_yield(smx_process_t self)
864 XBT_DEBUG("Yield process '%s'", self->name.c_str());
866 /* Go into sleep and return control to maestro */
867 self->context->suspend();
869 /* Ok, maestro returned control to us */
870 XBT_DEBUG("Control returned to me: '%s'", self->name.c_str());
872 if (self->new_host) {
873 SIMIX_process_change_host(self, self->new_host);
874 self->new_host = NULL;
877 if (self->context->iwannadie){
878 XBT_DEBUG("I wanna die!");
879 SIMIX_process_stop(self);
882 if (self->suspended) {
883 XBT_DEBUG("Hey! I'm suspended.");
884 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
886 SIMIX_process_suspend(self, self);
889 if (self->doexception) {
890 XBT_DEBUG("Wait, maestro left me an exception");
891 self->doexception = 0;
895 if(SMPI_switch_data_segment && self->segment_index != -1){
896 SMPI_switch_data_segment(self->segment_index);
900 /* callback: context fetching */
901 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
903 smx_process_t process = SIMIX_process_self();
905 return process->running_ctx;
910 /* callback: termination */
911 void SIMIX_process_exception_terminate(xbt_ex_t * e)
917 smx_context_t SIMIX_process_get_context(smx_process_t p) {
921 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
926 * \brief Returns the list of processes to run.
928 xbt_dynar_t SIMIX_process_get_runnable(void)
930 return simix_global->process_to_run;
934 * \brief Returns the process from PID.
936 smx_process_t SIMIX_process_from_PID(int PID)
939 xbt_swag_foreach(proc, simix_global->process_list) {
940 if (proc->pid == (unsigned long) PID)
946 /** @brief returns a dynar containg all currently existing processes */
947 xbt_dynar_t SIMIX_processes_as_dynar(void) {
949 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
950 xbt_swag_foreach(proc, simix_global->process_list) {
951 xbt_dynar_push(res,&proc);
957 void SIMIX_process_on_exit_runall(smx_process_t process) {
958 s_smx_process_exit_fun_t exit_fun;
959 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
960 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
961 while (!xbt_dynar_is_empty(process->on_exit)) {
962 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
963 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
967 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
968 xbt_assert(process, "current process not found: are you in maestro context ?");
970 if (!process->on_exit) {
971 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
974 s_smx_process_exit_fun_t exit_fun = {fun, data};
976 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
980 * \brief Sets the auto-restart status of the process.
981 * If set to 1, the process will be automatically restarted when its host
984 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
985 process->auto_restart = auto_restart;
988 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
989 return SIMIX_process_restart(process, simcall->issuer);
991 /** @brief Restart a process, starting it again from the beginning. */
992 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
993 XBT_DEBUG("Restarting process %s on %s",
994 process->name.c_str(), sg_host_get_name(process->host));
996 //retrieve the arguments of the old process
997 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
998 simgrid::simix::ProcessArg arg;
999 arg.name = process->name;
1000 arg.code = process->code;
1001 arg.hostname = sg_host_get_name(process->host);
1002 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1003 arg.data = process->data;
1004 arg.properties = NULL;
1005 arg.auto_restart = process->auto_restart;
1006 arg.args = process->args;
1008 //kill the old process
1009 SIMIX_process_kill(process, issuer);
1011 //start the new process
1012 if (simix_global->create_process_function)
1013 return simix_global->create_process_function(
1014 arg.name.c_str(), arg.code, arg.data,
1015 arg.hostname, arg.kill_time,
1016 arg.args.argc(), arg.args.to_argv(),
1017 arg.properties, arg.auto_restart,
1020 return simcall_process_create(
1021 arg.name.c_str(), arg.code, arg.data,
1022 arg.hostname, arg.kill_time,
1023 arg.args.argc(), arg.args.to_argv(),
1024 arg.properties, arg.auto_restart);
1027 void SIMIX_segment_index_set(smx_process_t proc, int index){
1028 proc->segment_index = index;