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 "smx_private.h"
8 #include "xbt/sysdep.h"
12 #include "src/mc/mc_replay.h"
13 #include "src/mc/mc_client.h"
14 #include "src/simix/smx_private.hpp"
17 #include "src/smpi/private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
21 "Logging specific to SIMIX (process)");
23 unsigned long simix_process_maxpid = 0;
26 * \brief Returns the current agent.
28 * This functions returns the currently running SIMIX process.
30 * \return The SIMIX process
32 smx_process_t SIMIX_process_self(void)
34 smx_context_t self_context = SIMIX_context_self();
36 return self_context ? SIMIX_context_get_process(self_context) : NULL;
40 * \brief Returns whether a process has pending asynchronous communications.
41 * \return true if there are asynchronous communications in this process
43 int SIMIX_process_has_pending_comms(smx_process_t process) {
45 return xbt_fifo_size(process->comms) > 0;
49 * \brief Moves a process to the list of processes to destroy.
51 void SIMIX_process_cleanup(smx_process_t process)
53 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
54 process->name, process, process->waiting_synchro);
56 SIMIX_process_on_exit_runall(process);
58 /* Unregister from the kill timer if any */
59 if (process->kill_timer != NULL)
60 SIMIX_timer_remove(process->kill_timer);
62 xbt_os_mutex_acquire(simix_global->mutex);
64 /* cancel non-blocking communications */
65 smx_synchro_t synchro;
66 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
68 /* make sure no one will finish the comm after this process is destroyed,
69 * because src_proc or dst_proc would be an invalid pointer */
70 SIMIX_comm_cancel(synchro);
72 if (synchro->comm.src_proc == process) {
73 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
74 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
75 synchro->comm.src_proc = NULL;
77 /* I'm not supposed to destroy a detached comm from the sender side, */
78 if (!synchro->comm.detached)
79 SIMIX_comm_destroy(synchro);
81 XBT_DEBUG("Don't destroy it since it's a detached comm");
84 else if (synchro->comm.dst_proc == process){
85 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
86 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
87 synchro->comm.dst_proc = NULL;
89 if (synchro->comm.detached && synchro->comm.refcount == 1
90 && synchro->comm.src_proc != NULL) {
91 /* the comm will be freed right now, remove it from the sender */
92 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
94 SIMIX_comm_destroy(synchro);
97 xbt_die("Communication synchro %p is in my list but I'm not the sender "
98 "or the receiver", synchro);
102 XBT_DEBUG("%p should not be run anymore",process);
103 xbt_swag_remove(process, simix_global->process_list);
104 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
105 xbt_swag_insert(process, simix_global->process_to_destroy);
106 process->context->iwannadie = 0;
108 xbt_os_mutex_release(simix_global->mutex);
114 * Should be called some time to time to free the memory allocated for processes
115 * that have finished (or killed).
117 void SIMIX_process_empty_trash(void)
119 smx_process_t process = NULL;
121 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
122 XBT_DEBUG("Getting rid of %p",process);
124 SIMIX_context_free(process->context);
126 /* Free the exception allocated at creation time */
127 free(process->running_ctx);
128 xbt_dict_free(&process->properties);
130 xbt_fifo_free(process->comms);
132 xbt_dynar_free(&process->on_exit);
134 xbt_free(process->name);
140 * \brief Creates and runs the maestro process
142 void SIMIX_create_maestro_process()
144 smx_process_t maestro = NULL;
146 /* Create maestro process and intilialize it */
147 maestro = xbt_new0(s_smx_process_t, 1);
148 maestro->pid = simix_process_maxpid++;
150 maestro->name = (char *) "";
151 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
152 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
153 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
154 maestro->simcall.issuer = maestro;
155 simix_global->maestro_process = maestro;
159 * \brief Stops a process.
161 * Stops the process, execute all the registered on_exit functions,
162 * register it to the list of the process to restart if needed
163 * and stops its context.
165 void SIMIX_process_stop(smx_process_t arg) {
166 /* execute the on_exit functions */
167 SIMIX_process_on_exit_runall(arg);
168 /* Add the process to the list of process to restart, only if
171 if (arg->auto_restart && !sg_host_get_state(arg->host)) {
172 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
173 sg_host_get_name(arg->host),
174 SIMIX_timer_get_date(arg->kill_timer),
175 arg->argc,arg->argv,arg->properties,
178 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
179 /* stop the context */
180 SIMIX_context_stop(arg->context);
184 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
185 * This function frees the argument.
186 * \return the process created
188 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
190 smx_process_t process = simix_global->create_process_function(
206 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
208 xbt_main_func_t code,
210 const char *hostname,
212 int argc, char **argv,
213 xbt_dict_t properties,
215 return (void*)SIMIX_process_create(name, code, data, hostname,
216 kill_time, argc, argv, properties, auto_restart,
220 static void kill_process(void* process)
222 simix_global->kill_process_function((smx_process_t) process);
226 * \brief Internal function to create a process.
228 * This function actually creates the process.
229 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
230 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
232 * \return the process created
234 smx_process_t SIMIX_process_create(
236 xbt_main_func_t code,
238 const char *hostname,
240 int argc, char **argv,
241 xbt_dict_t properties,
243 smx_process_t parent_process)
245 smx_process_t process = NULL;
246 sg_host_t host = sg_host_by_name(hostname);
248 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
250 if (!sg_host_get_state(host)) {
252 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
254 for (i = 0; i < argc; i++)
259 process = xbt_new0(s_smx_process_t, 1);
261 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
263 process->pid = simix_process_maxpid++;
264 process->name = xbt_strdup(name);
265 process->host = host;
266 process->data = data;
267 process->comms = xbt_fifo_new();
268 process->simcall.issuer = process;
269 /* Initiliaze data segment to default value */
270 SIMIX_segment_index_set(process, -1);
272 if (parent_process != NULL) {
273 process->ppid = SIMIX_process_get_PID(parent_process);
274 /* SMPI process have their own data segment and
275 each other inherit from their father */
277 if(smpi_privatize_global_variables){
278 if( parent_process->pid != 0){
279 SIMIX_segment_index_set(process, parent_process->segment_index);
281 SIMIX_segment_index_set(process, process->pid - 1);
289 /* Process data for auto-restart */
290 process->auto_restart = auto_restart;
291 process->code = code;
292 process->argc = argc;
293 process->argv = argv;
296 XBT_VERB("Create context %s", process->name);
297 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
299 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
300 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
303 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
307 process->properties = properties;
309 /* Add the process to it's host process list */
310 xbt_swag_insert(process, sg_host_simix(host)->process_list);
312 XBT_DEBUG("Start context '%s'", process->name);
314 /* Now insert it in the global process list and in the process to run list */
315 xbt_swag_insert(process, simix_global->process_list);
316 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
317 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
319 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
320 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
321 sg_host_get_name(process->host), kill_time);
322 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
329 * \brief Executes the processes from simix_global->process_to_run.
331 * The processes of simix_global->process_to_run are run (in parallel if
332 * possible). On exit, simix_global->process_to_run is empty, and
333 * simix_global->process_that_ran contains the list of processes that just ran.
334 * The two lists are swapped so, be careful when using them before and after a
335 * call to this function.
337 void SIMIX_process_runall(void)
339 SIMIX_context_runall();
341 xbt_dynar_t tmp = simix_global->process_that_ran;
342 simix_global->process_that_ran = simix_global->process_to_run;
343 simix_global->process_to_run = tmp;
344 xbt_dynar_reset(simix_global->process_to_run);
347 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
348 SIMIX_process_kill(process, simcall->issuer);
351 * \brief Internal function to kill a SIMIX process.
353 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
354 * or directly for SIMIX internal purposes.
356 * \param process poor victim
357 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
359 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
361 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
363 process->context->iwannadie = 1;
364 process->blocked = 0;
365 process->suspended = 0;
366 process->doexception = 0;
368 /* destroy the blocking synchro if any */
369 if (process->waiting_synchro) {
371 switch (process->waiting_synchro->type) {
373 case SIMIX_SYNC_EXECUTE:
374 case SIMIX_SYNC_PARALLEL_EXECUTE:
375 SIMIX_process_execution_destroy(process->waiting_synchro);
378 case SIMIX_SYNC_COMMUNICATE:
379 xbt_fifo_remove(process->comms, process->waiting_synchro);
380 SIMIX_comm_cancel(process->waiting_synchro);
381 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
382 SIMIX_comm_destroy(process->waiting_synchro);
385 case SIMIX_SYNC_SLEEP:
386 SIMIX_process_sleep_destroy(process->waiting_synchro);
389 case SIMIX_SYNC_JOIN:
390 SIMIX_process_sleep_destroy(process->waiting_synchro);
393 case SIMIX_SYNC_SYNCHRO:
394 SIMIX_synchro_stop_waiting(process, &process->simcall);
395 SIMIX_synchro_destroy(process->waiting_synchro);
399 SIMIX_io_destroy(process->waiting_synchro);
404 process->waiting_synchro = NULL;
406 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
407 XBT_DEBUG("Inserting %s in the to_run list", process->name);
408 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
413 /** @brief Ask another process to raise the given exception
415 * @param cat category of exception
416 * @param value value associated to the exception
417 * @param msg string information associated to the exception
419 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
420 SMX_EXCEPTION(process, cat, value, msg);
422 if (process->suspended)
423 SIMIX_process_resume(process,SIMIX_process_self());
425 /* cancel the blocking synchro if any */
426 if (process->waiting_synchro) {
428 switch (process->waiting_synchro->type) {
430 case SIMIX_SYNC_EXECUTE:
431 case SIMIX_SYNC_PARALLEL_EXECUTE:
432 SIMIX_process_execution_cancel(process->waiting_synchro);
435 case SIMIX_SYNC_COMMUNICATE:
436 xbt_fifo_remove(process->comms, process->waiting_synchro);
437 SIMIX_comm_cancel(process->waiting_synchro);
440 case SIMIX_SYNC_SLEEP:
441 case SIMIX_SYNC_JOIN:
442 SIMIX_process_sleep_destroy(process->waiting_synchro);
443 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
444 XBT_DEBUG("Inserting %s in the to_run list", process->name);
445 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
449 case SIMIX_SYNC_SYNCHRO:
450 SIMIX_synchro_stop_waiting(process, &process->simcall);
454 SIMIX_io_destroy(process->waiting_synchro);
459 process->waiting_synchro = NULL;
463 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
464 SIMIX_process_killall(simcall->issuer, reset_pid);
467 * \brief Kills all running processes.
468 * \param issuer this one will not be killed
470 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
472 smx_process_t p = NULL;
474 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
476 SIMIX_process_kill(p,issuer);
481 simix_process_maxpid = reset_pid;
483 SIMIX_context_runall();
485 SIMIX_process_empty_trash();
488 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
490 process->new_host = dest;
492 void SIMIX_process_change_host(smx_process_t process,
495 xbt_assert((process != NULL), "Invalid parameters");
496 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
497 process->host = dest;
498 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
502 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
504 smx_synchro_t sync_suspend =
505 SIMIX_process_suspend(process, simcall->issuer);
507 if (process != simcall->issuer) {
508 SIMIX_simcall_answer(simcall);
510 xbt_fifo_push(sync_suspend->simcalls, simcall);
511 process->waiting_synchro = sync_suspend;
512 SIMIX_host_execution_suspend(process->waiting_synchro);
514 /* If we are suspending ourselves, then just do not finish the simcall now */
517 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
519 xbt_assert((process != NULL), "Invalid parameters");
521 if (process->suspended) {
522 XBT_DEBUG("Process '%s' is already suspended", process->name);
526 process->suspended = 1;
528 /* If we are suspending another process, and it is waiting on a sync,
529 suspend its synchronization. */
530 if (process != issuer) {
532 if (process->waiting_synchro) {
534 switch (process->waiting_synchro->type) {
536 case SIMIX_SYNC_EXECUTE:
537 case SIMIX_SYNC_PARALLEL_EXECUTE:
538 SIMIX_host_execution_suspend(process->waiting_synchro);
541 case SIMIX_SYNC_COMMUNICATE:
542 SIMIX_comm_suspend(process->waiting_synchro);
545 case SIMIX_SYNC_SLEEP:
546 SIMIX_process_sleep_suspend(process->waiting_synchro);
549 case SIMIX_SYNC_SYNCHRO:
550 /* Suspension is delayed to when the process is rescheduled. */
554 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
555 (int)process->waiting_synchro->type);
559 /* Suspension is delayed to when the process is rescheduled. */
563 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
564 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
568 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
569 SIMIX_process_resume(process, simcall->issuer);
572 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
574 XBT_IN("process = %p, issuer = %p", process, issuer);
576 if(process->context->iwannadie) {
577 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
581 if(!process->suspended) return;
582 process->suspended = 0;
584 /* If we are resuming another process, resume the synchronization it was waiting for
585 if any. Otherwise add it to the list of process to run in the next round. */
586 if (process != issuer) {
588 if (process->waiting_synchro) {
590 switch (process->waiting_synchro->type) {
592 case SIMIX_SYNC_EXECUTE:
593 case SIMIX_SYNC_PARALLEL_EXECUTE:
594 SIMIX_host_execution_resume(process->waiting_synchro);
597 case SIMIX_SYNC_COMMUNICATE:
598 SIMIX_comm_resume(process->waiting_synchro);
601 case SIMIX_SYNC_SLEEP:
602 SIMIX_process_sleep_resume(process->waiting_synchro);
605 case SIMIX_SYNC_SYNCHRO:
606 /* I cannot resume it now. This is delayed to when the process is rescheduled at
607 * the end of the synchro. */
611 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
612 (int)process->waiting_synchro->type);
615 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
620 int SIMIX_process_get_maxpid(void) {
621 return simix_process_maxpid;
624 int SIMIX_process_count(void)
626 return xbt_swag_size(simix_global->process_list);
629 int SIMIX_process_get_PID(smx_process_t self){
636 int SIMIX_process_get_PPID(smx_process_t self){
643 void* SIMIX_process_self_get_data(smx_process_t self)
645 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
650 return SIMIX_process_get_data(self);
653 void SIMIX_process_self_set_data(smx_process_t self, void *data)
655 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
657 SIMIX_process_set_data(self, data);
660 void* SIMIX_process_get_data(smx_process_t process)
662 return process->data;
665 void SIMIX_process_set_data(smx_process_t process, void *data)
667 process->data = data;
670 sg_host_t SIMIX_process_get_host(smx_process_t process)
672 return process->host;
675 xbt_main_func_t SIMIX_process_get_code(void){
676 return SIMIX_process_self()->code;
679 /* needs to be public and without simcall because it is called
680 by exceptions and logging events */
681 const char* SIMIX_process_self_get_name(void) {
683 smx_process_t process = SIMIX_process_self();
684 if (process == NULL || process == simix_global->maestro_process)
687 return SIMIX_process_get_name(process);
690 const char* SIMIX_process_get_name(smx_process_t process)
692 return process->name;
695 smx_process_t SIMIX_process_get_by_name(const char* name)
699 xbt_swag_foreach(proc, simix_global->process_list)
701 if(!strcmp(name, proc->name))
707 int SIMIX_process_is_suspended(smx_process_t process)
709 return process->suspended;
712 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
714 return process->properties;
717 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
719 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
720 xbt_fifo_push(sync->simcalls, simcall);
721 simcall->issuer->waiting_synchro = sync;
724 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
725 if (sync->sleep.surf_sleep) {
726 surf_action_cancel(sync->sleep.surf_sleep);
728 smx_simcall_t simcall;
729 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
730 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
731 simcall->issuer->waiting_synchro = NULL;
732 if (simcall->issuer->suspended) {
733 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
734 simcall->issuer->suspended = 0;
735 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
737 SIMIX_simcall_answer(simcall);
740 surf_action_unref(sync->sleep.surf_sleep);
741 sync->sleep.surf_sleep = NULL;
743 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
747 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
749 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
750 res->type = SIMIX_SYNC_JOIN;
751 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
755 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
757 if (MC_is_active() || MC_record_replay_is_active()) {
758 MC_process_clock_add(simcall->issuer, duration);
759 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
760 SIMIX_simcall_answer(simcall);
763 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
764 xbt_fifo_push(sync->simcalls, simcall);
765 simcall->issuer->waiting_synchro = sync;
768 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
770 sg_host_t host = process->host;
772 /* check if the host is active */
773 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
774 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
775 sg_host_get_name(host));
778 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
779 synchro->type = SIMIX_SYNC_SLEEP;
780 synchro->name = NULL;
781 synchro->category = NULL;
783 synchro->sleep.host = host;
784 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
786 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
787 XBT_DEBUG("Create sleep synchronization %p", synchro);
792 void SIMIX_post_process_sleep(smx_synchro_t synchro)
794 smx_simcall_t simcall;
796 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
798 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
800 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
801 case SURF_ACTION_FAILED:
802 simcall->issuer->context->iwannadie = 1;
803 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
804 state = SIMIX_SRC_HOST_FAILURE;
807 case SURF_ACTION_DONE:
815 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
816 simcall->issuer->context->iwannadie = 1;
818 simcall_process_sleep__set__result(simcall, state);
819 simcall->issuer->waiting_synchro = NULL;
820 if (simcall->issuer->suspended) {
821 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
822 simcall->issuer->suspended = 0;
823 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
825 SIMIX_simcall_answer(simcall);
829 SIMIX_process_sleep_destroy(synchro);
832 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
834 XBT_DEBUG("Destroy synchro %p", synchro);
835 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
837 if (synchro->sleep.surf_sleep) {
838 surf_action_unref(synchro->sleep.surf_sleep);
839 synchro->sleep.surf_sleep = NULL;
841 if (synchro->type == SIMIX_SYNC_SLEEP)
842 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
845 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
847 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
848 surf_action_suspend(synchro->sleep.surf_sleep);
851 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
853 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
854 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
855 surf_action_resume(synchro->sleep.surf_sleep);
859 * \brief Calling this function makes the process to yield.
861 * Only the current process can call this function, giving back the control to
864 * \param self the current process
866 void SIMIX_process_yield(smx_process_t self)
868 XBT_DEBUG("Yield process '%s'", self->name);
870 /* Go into sleep and return control to maestro */
871 SIMIX_context_suspend(self->context);
873 /* Ok, maestro returned control to us */
874 XBT_DEBUG("Control returned to me: '%s'", self->name);
876 if (self->new_host) {
877 SIMIX_process_change_host(self, self->new_host);
878 self->new_host = NULL;
881 if (self->context->iwannadie){
882 XBT_DEBUG("I wanna die!");
883 SIMIX_process_stop(self);
886 if (self->suspended) {
887 XBT_DEBUG("Hey! I'm suspended.");
888 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
890 SIMIX_process_suspend(self, self);
893 if (self->doexception) {
894 XBT_DEBUG("Wait, maestro left me an exception");
895 self->doexception = 0;
899 if(SMPI_switch_data_segment && self->segment_index != -1){
900 SMPI_switch_data_segment(self->segment_index);
904 /* callback: context fetching */
905 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
907 return SIMIX_process_self()->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", process->name, sg_host_get_name(process->host));
994 //retrieve the arguments of the old process
995 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
996 s_smx_process_arg_t arg;
997 arg.code = process->code;
998 arg.hostname = sg_host_get_name(process->host);
999 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
1000 arg.argc = process->argc;
1001 arg.data = process->data;
1003 arg.argv = xbt_new(char*,process->argc + 1);
1004 for (i = 0; i < arg.argc; i++) {
1005 arg.argv[i] = xbt_strdup(process->argv[i]);
1007 arg.argv[process->argc] = NULL;
1008 arg.properties = NULL;
1009 arg.auto_restart = process->auto_restart;
1010 //kill the old process
1011 SIMIX_process_kill(process,issuer);
1012 //start the new process
1013 smx_process_t new_process;
1014 if (simix_global->create_process_function) {
1015 new_process = simix_global->create_process_function(
1027 new_process = simcall_process_create(
1042 void SIMIX_segment_index_set(smx_process_t proc, int index){
1043 proc->segment_index = index;