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"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
17 "Logging specific to SIMIX (process)");
19 unsigned long simix_process_maxpid = 0;
22 * \brief Returns the current agent.
24 * This functions returns the currently running SIMIX process.
26 * \return The SIMIX process
28 smx_process_t SIMIX_process_self(void)
30 smx_context_t self_context = SIMIX_context_self();
32 return self_context ? SIMIX_context_get_process(self_context) : NULL;
36 * \brief Returns whether a process has pending asynchronous communications.
37 * \return true if there are asynchronous communications in this process
39 int SIMIX_process_has_pending_comms(smx_process_t process) {
41 return xbt_fifo_size(process->comms) > 0;
45 * \brief Moves a process to the list of processes to destroy.
47 void SIMIX_process_cleanup(smx_process_t process)
49 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
50 process->name, process, process->waiting_synchro);
52 SIMIX_process_on_exit_runall(process);
54 /* Unregister from the kill timer if any */
55 if (process->kill_timer != NULL)
56 SIMIX_timer_remove(process->kill_timer);
58 xbt_os_mutex_acquire(simix_global->mutex);
60 /* cancel non-blocking communications */
61 smx_synchro_t synchro;
62 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
64 /* make sure no one will finish the comm after this process is destroyed,
65 * because src_proc or dst_proc would be an invalid pointer */
66 SIMIX_comm_cancel(synchro);
68 if (synchro->comm.src_proc == process) {
69 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
70 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
71 synchro->comm.src_proc = NULL;
73 /* I'm not supposed to destroy a detached comm from the sender side, */
74 if (!synchro->comm.detached)
75 SIMIX_comm_destroy(synchro);
77 XBT_DEBUG("Don't destroy it since it's a detached comm");
80 else if (synchro->comm.dst_proc == process){
81 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
82 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
83 synchro->comm.dst_proc = NULL;
85 if (synchro->comm.detached && synchro->comm.refcount == 1
86 && synchro->comm.src_proc != NULL) {
87 /* the comm will be freed right now, remove it from the sender */
88 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
90 SIMIX_comm_destroy(synchro);
93 xbt_die("Communication synchro %p is in my list but I'm not the sender "
94 "or the receiver", synchro);
98 XBT_DEBUG("%p should not be run anymore",process);
99 xbt_swag_remove(process, simix_global->process_list);
100 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
101 xbt_swag_insert(process, simix_global->process_to_destroy);
102 process->context->iwannadie = 0;
104 xbt_os_mutex_release(simix_global->mutex);
110 * Should be called some time to time to free the memory allocated for processes
111 * that have finished (or killed).
113 void SIMIX_process_empty_trash(void)
115 smx_process_t process = NULL;
117 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
118 XBT_DEBUG("Getting rid of %p",process);
120 SIMIX_context_free(process->context);
122 /* Free the exception allocated at creation time */
123 free(process->running_ctx);
124 xbt_dict_free(&process->properties);
126 xbt_fifo_free(process->comms);
128 xbt_dynar_free(&process->on_exit);
130 xbt_free(process->name);
136 * \brief Creates and runs the maestro process
138 void SIMIX_create_maestro_process()
140 smx_process_t maestro = NULL;
142 /* Create maestro process and intilialize it */
143 maestro = xbt_new0(s_smx_process_t, 1);
144 maestro->pid = simix_process_maxpid++;
146 maestro->name = (char *) "";
147 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
148 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
149 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
150 maestro->simcall.issuer = maestro;
151 simix_global->maestro_process = maestro;
155 * \brief Stops a process.
157 * Stops the process, execute all the registered on_exit functions,
158 * register it to the list of the process to restart if needed
159 * and stops its context.
161 void SIMIX_process_stop(smx_process_t arg) {
162 /* execute the on_exit functions */
163 SIMIX_process_on_exit_runall(arg);
164 /* Add the process to the list of process to restart, only if
167 if (arg->auto_restart && !sg_host_get_state(arg->host)) {
168 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
169 sg_host_get_name(arg->host),
170 SIMIX_timer_get_date(arg->kill_timer),
171 arg->argc,arg->argv,arg->properties,
174 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
175 /* stop the context */
176 SIMIX_context_stop(arg->context);
180 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
181 * This function frees the argument.
182 * \return the process created
184 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
186 smx_process_t process = simix_global->create_process_function(
202 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
204 xbt_main_func_t code,
206 const char *hostname,
208 int argc, char **argv,
209 xbt_dict_t properties,
211 return (void*)SIMIX_process_create(name, code, data, hostname,
212 kill_time, argc, argv, properties, auto_restart,
216 static void kill_process(void* process)
218 simix_global->kill_process_function((smx_process_t) process);
222 * \brief Internal function to create a process.
224 * This function actually creates the process.
225 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
226 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
228 * \return the process created
230 smx_process_t SIMIX_process_create(
232 xbt_main_func_t code,
234 const char *hostname,
236 int argc, char **argv,
237 xbt_dict_t properties,
239 smx_process_t parent_process)
241 smx_process_t process = NULL;
242 sg_host_t host = sg_host_by_name(hostname);
244 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
246 if (!sg_host_get_state(host)) {
248 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
250 for (i = 0; i < argc; i++)
255 process = xbt_new0(s_smx_process_t, 1);
257 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
259 process->pid = simix_process_maxpid++;
260 process->name = xbt_strdup(name);
261 process->host = host;
262 process->data = data;
263 process->comms = xbt_fifo_new();
264 process->simcall.issuer = process;
266 if (parent_process) {
267 process->ppid = SIMIX_process_get_PID(parent_process);
272 /* Process data for auto-restart */
273 process->auto_restart = auto_restart;
274 process->code = code;
275 process->argc = argc;
276 process->argv = argv;
279 XBT_VERB("Create context %s", process->name);
280 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
282 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
283 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
286 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
290 process->properties = properties;
292 /* Add the process to it's host process list */
293 xbt_swag_insert(process, sg_host_simix(host)->process_list);
295 XBT_DEBUG("Start context '%s'", process->name);
297 /* Now insert it in the global process list and in the process to run list */
298 xbt_swag_insert(process, simix_global->process_list);
299 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
300 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
302 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
303 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
304 sg_host_get_name(process->host), kill_time);
305 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
312 * \brief Executes the processes from simix_global->process_to_run.
314 * The processes of simix_global->process_to_run are run (in parallel if
315 * possible). On exit, simix_global->process_to_run is empty, and
316 * simix_global->process_that_ran contains the list of processes that just ran.
317 * The two lists are swapped so, be careful when using them before and after a
318 * call to this function.
320 void SIMIX_process_runall(void)
322 SIMIX_context_runall();
324 xbt_dynar_t tmp = simix_global->process_that_ran;
325 simix_global->process_that_ran = simix_global->process_to_run;
326 simix_global->process_to_run = tmp;
327 xbt_dynar_reset(simix_global->process_to_run);
330 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
331 SIMIX_process_kill(process, simcall->issuer);
334 * \brief Internal function to kill a SIMIX process.
336 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
337 * or directly for SIMIX internal purposes.
339 * \param process poor victim
340 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
342 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
344 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
346 process->context->iwannadie = 1;
347 process->blocked = 0;
348 process->suspended = 0;
349 process->doexception = 0;
351 /* destroy the blocking synchro if any */
352 if (process->waiting_synchro) {
354 switch (process->waiting_synchro->type) {
356 case SIMIX_SYNC_EXECUTE:
357 case SIMIX_SYNC_PARALLEL_EXECUTE:
358 SIMIX_process_execution_destroy(process->waiting_synchro);
361 case SIMIX_SYNC_COMMUNICATE:
362 xbt_fifo_remove(process->comms, process->waiting_synchro);
363 SIMIX_comm_cancel(process->waiting_synchro);
364 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
365 SIMIX_comm_destroy(process->waiting_synchro);
368 case SIMIX_SYNC_SLEEP:
369 SIMIX_process_sleep_destroy(process->waiting_synchro);
372 case SIMIX_SYNC_JOIN:
373 SIMIX_process_sleep_destroy(process->waiting_synchro);
376 case SIMIX_SYNC_SYNCHRO:
377 SIMIX_synchro_stop_waiting(process, &process->simcall);
378 SIMIX_synchro_destroy(process->waiting_synchro);
382 SIMIX_io_destroy(process->waiting_synchro);
387 process->waiting_synchro = NULL;
389 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
390 XBT_DEBUG("Inserting %s in the to_run list", process->name);
391 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
396 /** @brief Ask another process to raise the given exception
398 * @param cat category of exception
399 * @param value value associated to the exception
400 * @param msg string information associated to the exception
402 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
403 SMX_EXCEPTION(process, cat, value, msg);
405 if (process->suspended)
406 SIMIX_process_resume(process,SIMIX_process_self());
408 /* cancel the blocking synchro if any */
409 if (process->waiting_synchro) {
411 switch (process->waiting_synchro->type) {
413 case SIMIX_SYNC_EXECUTE:
414 case SIMIX_SYNC_PARALLEL_EXECUTE:
415 SIMIX_process_execution_cancel(process->waiting_synchro);
418 case SIMIX_SYNC_COMMUNICATE:
419 xbt_fifo_remove(process->comms, process->waiting_synchro);
420 SIMIX_comm_cancel(process->waiting_synchro);
423 case SIMIX_SYNC_SLEEP:
424 case SIMIX_SYNC_JOIN:
425 SIMIX_process_sleep_destroy(process->waiting_synchro);
426 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
427 XBT_DEBUG("Inserting %s in the to_run list", process->name);
428 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
432 case SIMIX_SYNC_SYNCHRO:
433 SIMIX_synchro_stop_waiting(process, &process->simcall);
437 SIMIX_io_destroy(process->waiting_synchro);
442 process->waiting_synchro = NULL;
446 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
447 SIMIX_process_killall(simcall->issuer, reset_pid);
450 * \brief Kills all running processes.
451 * \param issuer this one will not be killed
453 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
455 smx_process_t p = NULL;
457 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
459 SIMIX_process_kill(p,issuer);
464 simix_process_maxpid = reset_pid;
466 SIMIX_context_runall();
468 SIMIX_process_empty_trash();
471 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
473 process->new_host = dest;
475 void SIMIX_process_change_host(smx_process_t process,
478 xbt_assert((process != NULL), "Invalid parameters");
479 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
480 process->host = dest;
481 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
485 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
487 smx_synchro_t sync_suspend =
488 SIMIX_process_suspend(process, simcall->issuer);
490 if (process != simcall->issuer) {
491 SIMIX_simcall_answer(simcall);
493 xbt_fifo_push(sync_suspend->simcalls, simcall);
494 process->waiting_synchro = sync_suspend;
495 SIMIX_host_execution_suspend(process->waiting_synchro);
497 /* If we are suspending ourselves, then just do not finish the simcall now */
500 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
502 xbt_assert((process != NULL), "Invalid parameters");
504 if (process->suspended) {
505 XBT_DEBUG("Process '%s' is already suspended", process->name);
509 process->suspended = 1;
511 /* If we are suspending another process, and it is waiting on a sync,
512 suspend its synchronization. */
513 if (process != issuer) {
515 if (process->waiting_synchro) {
517 switch (process->waiting_synchro->type) {
519 case SIMIX_SYNC_EXECUTE:
520 case SIMIX_SYNC_PARALLEL_EXECUTE:
521 SIMIX_host_execution_suspend(process->waiting_synchro);
524 case SIMIX_SYNC_COMMUNICATE:
525 SIMIX_comm_suspend(process->waiting_synchro);
528 case SIMIX_SYNC_SLEEP:
529 SIMIX_process_sleep_suspend(process->waiting_synchro);
532 case SIMIX_SYNC_SYNCHRO:
533 /* Suspension is delayed to when the process is rescheduled. */
537 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
538 (int)process->waiting_synchro->type);
542 /* Suspension is delayed to when the process is rescheduled. */
546 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
547 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
551 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
552 SIMIX_process_resume(process, simcall->issuer);
555 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
557 XBT_IN("process = %p, issuer = %p", process, issuer);
559 if(process->context->iwannadie) {
560 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
564 if(!process->suspended) return;
565 process->suspended = 0;
567 /* If we are resuming another process, resume the synchronization it was waiting for
568 if any. Otherwise add it to the list of process to run in the next round. */
569 if (process != issuer) {
571 if (process->waiting_synchro) {
573 switch (process->waiting_synchro->type) {
575 case SIMIX_SYNC_EXECUTE:
576 case SIMIX_SYNC_PARALLEL_EXECUTE:
577 SIMIX_host_execution_resume(process->waiting_synchro);
580 case SIMIX_SYNC_COMMUNICATE:
581 SIMIX_comm_resume(process->waiting_synchro);
584 case SIMIX_SYNC_SLEEP:
585 SIMIX_process_sleep_resume(process->waiting_synchro);
588 case SIMIX_SYNC_SYNCHRO:
589 /* I cannot resume it now. This is delayed to when the process is rescheduled at
590 * the end of the synchro. */
594 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
595 (int)process->waiting_synchro->type);
598 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
603 int SIMIX_process_get_maxpid(void) {
604 return simix_process_maxpid;
607 int SIMIX_process_count(void)
609 return xbt_swag_size(simix_global->process_list);
612 int SIMIX_process_get_PID(smx_process_t self){
619 int SIMIX_process_get_PPID(smx_process_t self){
626 void* SIMIX_process_self_get_data(smx_process_t self)
628 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
633 return SIMIX_process_get_data(self);
636 void SIMIX_process_self_set_data(smx_process_t self, void *data)
638 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
640 SIMIX_process_set_data(self, data);
643 void* SIMIX_process_get_data(smx_process_t process)
645 return process->data;
648 void SIMIX_process_set_data(smx_process_t process, void *data)
650 process->data = data;
653 sg_host_t SIMIX_process_get_host(smx_process_t process)
655 return process->host;
658 /* needs to be public and without simcall because it is called
659 by exceptions and logging events */
660 const char* SIMIX_process_self_get_name(void) {
662 smx_process_t process = SIMIX_process_self();
663 if (process == NULL || process == simix_global->maestro_process)
666 return SIMIX_process_get_name(process);
669 const char* SIMIX_process_get_name(smx_process_t process)
671 return process->name;
674 smx_process_t SIMIX_process_get_by_name(const char* name)
678 xbt_swag_foreach(proc, simix_global->process_list)
680 if(!strcmp(name, proc->name))
686 int SIMIX_process_is_suspended(smx_process_t process)
688 return process->suspended;
691 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
693 return process->properties;
696 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
698 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
699 xbt_fifo_push(sync->simcalls, simcall);
700 simcall->issuer->waiting_synchro = sync;
703 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
704 if (sync->sleep.surf_sleep) {
705 surf_action_cancel(sync->sleep.surf_sleep);
707 smx_simcall_t simcall;
708 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
709 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
710 simcall->issuer->waiting_synchro = NULL;
711 if (simcall->issuer->suspended) {
712 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
713 simcall->issuer->suspended = 0;
714 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
716 SIMIX_simcall_answer(simcall);
719 surf_action_unref(sync->sleep.surf_sleep);
720 sync->sleep.surf_sleep = NULL;
722 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
726 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
728 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
729 res->type = SIMIX_SYNC_JOIN;
730 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
734 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
736 if (MC_is_active() || MC_record_replay_is_active()) {
737 MC_process_clock_add(simcall->issuer, duration);
738 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
739 SIMIX_simcall_answer(simcall);
742 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
743 xbt_fifo_push(sync->simcalls, simcall);
744 simcall->issuer->waiting_synchro = sync;
747 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
749 sg_host_t host = process->host;
751 /* check if the host is active */
752 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
753 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
754 sg_host_get_name(host));
757 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
758 synchro->type = SIMIX_SYNC_SLEEP;
759 synchro->name = NULL;
760 synchro->category = NULL;
762 synchro->sleep.host = host;
763 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
765 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
766 XBT_DEBUG("Create sleep synchronization %p", synchro);
771 void SIMIX_post_process_sleep(smx_synchro_t synchro)
773 smx_simcall_t simcall;
775 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
777 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
779 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
780 case SURF_ACTION_FAILED:
781 simcall->issuer->context->iwannadie = 1;
782 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
783 state = SIMIX_SRC_HOST_FAILURE;
786 case SURF_ACTION_DONE:
794 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
795 simcall->issuer->context->iwannadie = 1;
797 simcall_process_sleep__set__result(simcall, state);
798 simcall->issuer->waiting_synchro = NULL;
799 if (simcall->issuer->suspended) {
800 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
801 simcall->issuer->suspended = 0;
802 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
804 SIMIX_simcall_answer(simcall);
808 SIMIX_process_sleep_destroy(synchro);
811 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
813 XBT_DEBUG("Destroy synchro %p", synchro);
814 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
816 if (synchro->sleep.surf_sleep) {
817 surf_action_unref(synchro->sleep.surf_sleep);
818 synchro->sleep.surf_sleep = NULL;
820 if (synchro->type == SIMIX_SYNC_SLEEP)
821 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
824 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
826 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
827 surf_action_suspend(synchro->sleep.surf_sleep);
830 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
832 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
833 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
834 surf_action_resume(synchro->sleep.surf_sleep);
838 * \brief Calling this function makes the process to yield.
840 * Only the current process can call this function, giving back the control to
843 * \param self the current process
845 void SIMIX_process_yield(smx_process_t self)
847 XBT_DEBUG("Yield process '%s'", self->name);
849 /* Go into sleep and return control to maestro */
850 SIMIX_context_suspend(self->context);
852 /* Ok, maestro returned control to us */
853 XBT_DEBUG("Control returned to me: '%s'", self->name);
855 if (self->new_host) {
856 SIMIX_process_change_host(self, self->new_host);
857 self->new_host = NULL;
860 if (self->context->iwannadie){
861 XBT_DEBUG("I wanna die!");
862 SIMIX_process_stop(self);
865 if (self->suspended) {
866 XBT_DEBUG("Hey! I'm suspended.");
867 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
869 SIMIX_process_suspend(self, self);
872 if (self->doexception) {
873 XBT_DEBUG("Wait, maestro left me an exception");
874 self->doexception = 0;
880 /* callback: context fetching */
881 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
883 return SIMIX_process_self()->running_ctx;
886 /* callback: termination */
887 void SIMIX_process_exception_terminate(xbt_ex_t * e)
893 smx_context_t SIMIX_process_get_context(smx_process_t p) {
897 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
902 * \brief Returns the list of processes to run.
904 xbt_dynar_t SIMIX_process_get_runnable(void)
906 return simix_global->process_to_run;
910 * \brief Returns the process from PID.
912 smx_process_t SIMIX_process_from_PID(int PID)
915 xbt_swag_foreach(proc, simix_global->process_list) {
916 if (proc->pid == (unsigned long) PID)
922 /** @brief returns a dynar containg all currently existing processes */
923 xbt_dynar_t SIMIX_processes_as_dynar(void) {
925 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
926 xbt_swag_foreach(proc, simix_global->process_list) {
927 xbt_dynar_push(res,&proc);
933 void SIMIX_process_on_exit_runall(smx_process_t process) {
934 s_smx_process_exit_fun_t exit_fun;
935 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
936 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
937 while (!xbt_dynar_is_empty(process->on_exit)) {
938 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
939 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
943 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
944 xbt_assert(process, "current process not found: are you in maestro context ?");
946 if (!process->on_exit) {
947 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
950 s_smx_process_exit_fun_t exit_fun = {fun, data};
952 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
956 * \brief Sets the auto-restart status of the process.
957 * If set to 1, the process will be automatically restarted when its host
960 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
961 process->auto_restart = auto_restart;
964 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
965 return SIMIX_process_restart(process, simcall->issuer);
967 /** @brief Restart a process, starting it again from the beginning. */
968 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
969 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
970 //retrieve the arguments of the old process
971 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
972 s_smx_process_arg_t arg;
973 arg.code = process->code;
974 arg.hostname = sg_host_get_name(process->host);
975 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
976 arg.argc = process->argc;
977 arg.data = process->data;
979 arg.argv = xbt_new(char*,process->argc + 1);
980 for (i = 0; i < arg.argc; i++) {
981 arg.argv[i] = xbt_strdup(process->argv[i]);
983 arg.argv[process->argc] = NULL;
984 arg.properties = NULL;
985 arg.auto_restart = process->auto_restart;
986 //kill the old process
987 SIMIX_process_kill(process,issuer);
988 //start the new process
989 smx_process_t new_process;
990 if (simix_global->create_process_function) {
991 new_process = simix_global->create_process_function(
1003 new_process = simcall_process_create(