1 /* Copyright (c) 2007-2014. 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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
16 unsigned long simix_process_maxpid = 0;
19 * \brief Returns the current agent.
21 * This functions returns the currently running SIMIX process.
23 * \return The SIMIX process
25 XBT_INLINE smx_process_t SIMIX_process_self(void)
27 smx_context_t self_context = SIMIX_context_self();
29 return self_context ? SIMIX_context_get_process(self_context) : NULL;
33 * \brief Returns whether a process has pending asynchronous communications.
34 * \return true if there are asynchronous communications in this process
36 int SIMIX_process_has_pending_comms(smx_process_t process) {
38 return xbt_fifo_size(process->comms) > 0;
41 void SIMIX_pre_process_cleanup(smx_simcall_t simcall, smx_process_t process) {
42 SIMIX_process_cleanup(process);
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 action %p",
50 process->name, process, process->waiting_action);
52 SIMIX_process_on_exit_runall(process);
54 /* cancel non-blocking communications */
56 while ((action = xbt_fifo_pop(process->comms))) {
58 /* make sure no one will finish the comm after this process is destroyed,
59 * because src_proc or dst_proc would be an invalid pointer */
60 SIMIX_comm_cancel(action);
62 if (action->comm.src_proc == process) {
63 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
64 action, action->comm.detached, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
65 action->comm.src_proc = NULL;
67 if (action->comm.detached) {
68 if (action->comm.refcount == 0) {
69 XBT_DEBUG("Increase the refcount before destroying it since it's detached");
70 /* I'm not supposed to destroy a detached comm from the sender side,
71 * unless there is no receiver matching the rdv */
72 action->comm.refcount++;
73 SIMIX_comm_destroy(action);
76 XBT_DEBUG("Don't destroy it since its refcount is %d", action->comm.refcount);
79 SIMIX_comm_destroy(action);
82 else if (action->comm.dst_proc == process){
83 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
84 action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
85 action->comm.dst_proc = NULL;
87 if (action->comm.detached && action->comm.refcount == 1
88 && action->comm.src_proc != NULL) {
89 /* the comm will be freed right now, remove it from the sender */
90 xbt_fifo_remove(action->comm.src_proc->comms, action);
92 SIMIX_comm_destroy(action);
95 xbt_die("Communication action %p is in my list but I'm not the sender "
96 "or the receiver", action);
100 xbt_swag_remove(process, simix_global->process_list);
101 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
102 xbt_swag_insert(process, simix_global->process_to_destroy);
103 process->context->iwannadie = 0;
109 * Should be called some time to time to free the memory allocated for processes
110 * that have finished (or killed).
112 void SIMIX_process_empty_trash(void)
114 smx_process_t process = NULL;
116 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
117 SIMIX_context_free(process->context);
119 /* Free the exception allocated at creation time */
120 free(process->running_ctx);
121 xbt_dict_free(&process->properties);
123 xbt_fifo_free(process->comms);
125 xbt_dynar_free(&process->on_exit);
127 xbt_free(process->name);
133 * \brief Creates and runs the maestro process
135 void SIMIX_create_maestro_process()
137 smx_process_t maestro = NULL;
139 /* Create maestro process and intilialize it */
140 maestro = xbt_new0(s_smx_process_t, 1);
141 maestro->pid = simix_process_maxpid++;
143 maestro->name = (char *) "";
144 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
145 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
146 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
147 maestro->simcall.issuer = maestro;
148 simix_global->maestro_process = maestro;
152 * \brief Stops a process.
154 * Stops the process, execute all the registered on_exit functions,
155 * register it to the list of the process to restart if needed
156 * and stops its context.
158 void SIMIX_process_stop(smx_process_t arg) {
159 /* execute the on_exit functions */
160 SIMIX_process_on_exit_runall(arg);
161 /* Add the process to the list of process to restart, only if
164 if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
165 SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
166 sg_host_name(arg->smx_host),
168 arg->argc,arg->argv,arg->properties,
171 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
172 /* stop the context */
173 SIMIX_context_stop(arg->context);
177 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
178 * This function frees the argument.
179 * \return the process created
181 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
183 smx_process_t process;
184 simix_global->create_process_function(&process,
200 void SIMIX_pre_process_create(smx_simcall_t simcall,
201 smx_process_t *process,
203 xbt_main_func_t code,
205 const char *hostname,
207 int argc, char **argv,
208 xbt_dict_t properties,
210 SIMIX_process_create(process, name, code, data, hostname,
211 kill_time, argc, argv, properties, auto_restart,
215 * \brief Internal function to create a process.
217 * This function actually creates the process.
218 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
219 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
221 * \return the process created
223 void SIMIX_process_create(smx_process_t *process,
225 xbt_main_func_t code,
227 const char *hostname,
229 int argc, char **argv,
230 xbt_dict_t properties,
232 smx_process_t parent_process)
235 smx_host_t host = SIMIX_host_get_by_name(hostname);
237 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
239 if (!SIMIX_host_get_state(host)) {
241 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
243 for (i = 0; i < argc; i++)
248 *process = xbt_new0(s_smx_process_t, 1);
250 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
252 (*process)->pid = simix_process_maxpid++;
253 (*process)->name = xbt_strdup(name);
254 (*process)->smx_host = host;
255 (*process)->data = data;
256 (*process)->comms = xbt_fifo_new();
257 (*process)->simcall.issuer = *process;
259 if (parent_process) {
260 (*process)->ppid = SIMIX_process_get_PID(parent_process);
262 (*process)->ppid = -1;
265 /* Process data for auto-restart */
266 (*process)->auto_restart = auto_restart;
267 (*process)->code = code;
268 (*process)->argc = argc;
269 (*process)->argv = argv;
270 (*process)->kill_time = kill_time;
273 XBT_VERB("Create context %s", (*process)->name);
274 (*process)->context = SIMIX_context_new(code, argc, argv,
275 simix_global->cleanup_process_function, *process);
277 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
278 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
281 MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
285 (*process)->properties = properties;
287 /* Add the process to it's host process list */
288 xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
290 XBT_DEBUG("Start context '%s'", (*process)->name);
292 /* Now insert it in the global process list and in the process to run list */
293 xbt_swag_insert(*process, simix_global->process_list);
294 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
295 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
298 if (kill_time > SIMIX_get_clock()) {
299 if (simix_global->kill_process_function) {
300 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
301 sg_host_name((*process)->smx_host), kill_time);
302 SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
308 * \brief Executes the processes from simix_global->process_to_run.
310 * The processes of simix_global->process_to_run are run (in parallel if
311 * possible). On exit, simix_global->process_to_run is empty, and
312 * simix_global->process_that_ran contains the list of processes that just ran.
313 * The two lists are swapped so, be careful when using them before and after a
314 * call to this function.
316 void SIMIX_process_runall(void)
318 SIMIX_context_runall();
320 xbt_dynar_t tmp = simix_global->process_that_ran;
321 simix_global->process_that_ran = simix_global->process_to_run;
322 simix_global->process_to_run = tmp;
323 xbt_dynar_reset(simix_global->process_to_run);
326 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
327 SIMIX_process_kill(process, simcall->issuer);
330 * \brief Internal function to kill a SIMIX process.
332 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
333 * or directly for SIMIX internal purposes.
335 * \param process poor victim
336 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
338 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
340 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
342 process->context->iwannadie = 1;
343 process->blocked = 0;
344 process->suspended = 0;
345 /* FIXME: set doexception to 0 also? */
347 /* destroy the blocking action if any */
348 if (process->waiting_action) {
350 switch (process->waiting_action->type) {
352 case SIMIX_ACTION_EXECUTE:
353 case SIMIX_ACTION_PARALLEL_EXECUTE:
354 SIMIX_host_execution_destroy(process->waiting_action);
357 case SIMIX_ACTION_COMMUNICATE:
358 xbt_fifo_remove(process->comms, process->waiting_action);
359 SIMIX_comm_cancel(process->waiting_action);
360 SIMIX_comm_destroy(process->waiting_action);
363 case SIMIX_ACTION_SLEEP:
364 SIMIX_process_sleep_destroy(process->waiting_action);
367 case SIMIX_ACTION_JOIN:
368 SIMIX_process_sleep_destroy(process->waiting_action);
371 case SIMIX_ACTION_SYNCHRO:
372 SIMIX_synchro_stop_waiting(process, &process->simcall);
373 SIMIX_synchro_destroy(process->waiting_action);
376 case SIMIX_ACTION_IO:
377 SIMIX_io_destroy(process->waiting_action);
380 /* **************************************/
381 /* TUTORIAL: New API */
382 case SIMIX_ACTION_NEW_API:
383 SIMIX_new_api_destroy(process->waiting_action);
385 /* **************************************/
389 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
390 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
395 void SIMIX_pre_process_killall(smx_simcall_t simcall, int reset_pid) {
396 SIMIX_process_killall(simcall->issuer, reset_pid);
399 * \brief Kills all running processes.
400 * \param issuer this one will not be killed
402 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
404 smx_process_t p = NULL;
406 while ((p = xbt_swag_extract(simix_global->process_list))) {
408 SIMIX_process_kill(p,issuer);
413 simix_process_maxpid = reset_pid;
415 SIMIX_context_runall();
417 SIMIX_process_empty_trash();
420 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
423 process->new_host = dest;
425 void SIMIX_process_change_host(smx_process_t process,
428 xbt_assert((process != NULL), "Invalid parameters");
429 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
430 process->smx_host = dest;
431 xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
435 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
437 smx_action_t action_suspend =
438 SIMIX_process_suspend(process, simcall->issuer);
440 if (process != simcall->issuer) {
441 SIMIX_simcall_answer(simcall);
443 xbt_fifo_push(action_suspend->simcalls, simcall);
444 process->waiting_action = action_suspend;
445 SIMIX_host_execution_suspend(process->waiting_action);
447 /* If we are suspending ourselves, then just do not finish the simcall now */
450 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
452 xbt_assert((process != NULL), "Invalid parameters");
454 if (process->suspended) {
455 XBT_DEBUG("Process '%s' is already suspended", process->name);
459 process->suspended = 1;
461 /* If we are suspending another process, and it is waiting on an action,
462 suspend its action. */
463 if (process != issuer) {
465 if (process->waiting_action) {
467 switch (process->waiting_action->type) {
469 case SIMIX_ACTION_EXECUTE:
470 case SIMIX_ACTION_PARALLEL_EXECUTE:
471 SIMIX_host_execution_suspend(process->waiting_action);
474 case SIMIX_ACTION_COMMUNICATE:
475 SIMIX_comm_suspend(process->waiting_action);
478 case SIMIX_ACTION_SLEEP:
479 SIMIX_process_sleep_suspend(process->waiting_action);
482 case SIMIX_ACTION_SYNCHRO:
483 /* Suspension is delayed to when the process is rescheduled. */
487 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
488 (int)process->waiting_action->type);
492 /* Suspension is delayed to when the process is rescheduled. */
496 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
497 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
501 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
502 SIMIX_process_resume(process, simcall->issuer);
505 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
507 xbt_assert((process != NULL), "Invalid parameters");
509 XBT_IN("process = %p, issuer = %p", process, issuer);
511 if(process->context->iwannadie) {
512 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
516 if(!process->suspended) return;
517 process->suspended = 0;
519 /* If we are resuming another process, resume the action it was waiting for
520 if any. Otherwise add it to the list of process to run in the next round. */
521 if (process != issuer) {
523 if (process->waiting_action) {
525 switch (process->waiting_action->type) {
527 case SIMIX_ACTION_EXECUTE:
528 case SIMIX_ACTION_PARALLEL_EXECUTE:
529 SIMIX_host_execution_resume(process->waiting_action);
532 case SIMIX_ACTION_COMMUNICATE:
533 SIMIX_comm_resume(process->waiting_action);
536 case SIMIX_ACTION_SLEEP:
537 SIMIX_process_sleep_resume(process->waiting_action);
540 case SIMIX_ACTION_SYNCHRO:
541 /* I cannot resume it now. This is delayed to when the process is rescheduled at
542 * the end of the synchro. */
546 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
547 (int)process->waiting_action->type);
550 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
555 int SIMIX_process_get_maxpid(void) {
556 return simix_process_maxpid;
559 int SIMIX_pre_process_count(smx_simcall_t simcall){
560 return SIMIX_process_count();
562 int SIMIX_process_count(void)
564 return xbt_swag_size(simix_global->process_list);
567 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
568 return SIMIX_process_get_PID(self);
571 int SIMIX_process_get_PID(smx_process_t self){
578 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
579 return SIMIX_process_get_PPID(self);
582 int SIMIX_process_get_PPID(smx_process_t self){
589 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
590 return SIMIX_process_self_get_data(self);
593 void* SIMIX_process_self_get_data(smx_process_t self)
595 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
600 return SIMIX_process_get_data(self);
603 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
605 SIMIX_process_set_data(process, data);
607 void SIMIX_process_self_set_data(smx_process_t self, void *data)
609 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
611 SIMIX_process_set_data(self, data);
614 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
615 return SIMIX_process_get_data(process);
617 void* SIMIX_process_get_data(smx_process_t process)
619 return process->data;
622 void SIMIX_process_set_data(smx_process_t process, void *data)
624 process->data = data;
627 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
628 return SIMIX_process_get_host(process);
630 smx_host_t SIMIX_process_get_host(smx_process_t process)
632 return process->smx_host;
635 /* needs to be public and without simcall because it is called
636 by exceptions and logging events */
637 const char* SIMIX_process_self_get_name(void) {
639 smx_process_t process = SIMIX_process_self();
640 if (process == NULL || process == simix_global->maestro_process)
643 return SIMIX_process_get_name(process);
646 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
647 return SIMIX_process_get_name(process);
649 const char* SIMIX_process_get_name(smx_process_t process)
651 return process->name;
654 smx_process_t SIMIX_process_get_by_name(const char* name)
658 xbt_swag_foreach(proc, simix_global->process_list)
660 if(!strcmp(name, proc->name))
666 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
667 return SIMIX_process_is_suspended(process);
669 int SIMIX_process_is_suspended(smx_process_t process)
671 return process->suspended;
674 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
675 return SIMIX_process_get_properties(process);
677 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
679 return process->properties;
682 void SIMIX_pre_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
684 smx_action_t action = SIMIX_process_join(simcall->issuer, process, timeout);
685 xbt_fifo_push(action->simcalls, simcall);
686 simcall->issuer->waiting_action = action;
689 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_action_t action){
690 if (action->sleep.surf_sleep) {
691 surf_action_cancel(action->sleep.surf_sleep);
693 smx_simcall_t simcall;
694 while ((simcall = xbt_fifo_shift(action->simcalls))) {
695 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
696 simcall->issuer->waiting_action = NULL;
697 if (simcall->issuer->suspended) {
698 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
699 simcall->issuer->suspended = 0;
700 SIMIX_pre_process_suspend(simcall, simcall->issuer);
702 SIMIX_simcall_answer(simcall);
706 SIMIX_process_sleep_destroy(action);
711 smx_action_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
713 smx_action_t res = SIMIX_process_sleep(issuer, timeout);
714 res->type = SIMIX_ACTION_JOIN;
715 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
719 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
721 if (MC_is_active()) {
722 MC_process_clock_add(simcall->issuer, duration);
723 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
724 SIMIX_simcall_answer(simcall);
727 smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
728 xbt_fifo_push(action->simcalls, simcall);
729 simcall->issuer->waiting_action = action;
732 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
735 smx_host_t host = process->smx_host;
737 /* check if the host is active */
738 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
739 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
743 action = xbt_mallocator_get(simix_global->action_mallocator);
744 action->type = SIMIX_ACTION_SLEEP;
747 action->category = NULL;
750 action->sleep.host = host;
751 action->sleep.surf_sleep =
752 surf_workstation_sleep(host, duration);
754 surf_action_set_data(action->sleep.surf_sleep, action);
755 XBT_DEBUG("Create sleep action %p", action);
760 void SIMIX_post_process_sleep(smx_action_t action)
762 smx_simcall_t simcall;
764 xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
766 while ((simcall = xbt_fifo_shift(action->simcalls))) {
768 switch(surf_action_get_state(action->sleep.surf_sleep)){
769 case SURF_ACTION_FAILED:
770 simcall->issuer->context->iwannadie = 1;
771 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
772 state = SIMIX_SRC_HOST_FAILURE;
775 case SURF_ACTION_DONE:
783 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
784 simcall->issuer->context->iwannadie = 1;
786 simcall_process_sleep__set__result(simcall, state);
787 simcall->issuer->waiting_action = NULL;
788 if (simcall->issuer->suspended) {
789 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
790 simcall->issuer->suspended = 0;
791 SIMIX_pre_process_suspend(simcall, simcall->issuer);
793 SIMIX_simcall_answer(simcall);
797 SIMIX_process_sleep_destroy(action);
800 void SIMIX_process_sleep_destroy(smx_action_t action)
802 XBT_DEBUG("Destroy action %p", action);
803 xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
805 if (action->sleep.surf_sleep) {
806 surf_action_unref(action->sleep.surf_sleep);
807 action->sleep.surf_sleep = NULL;
809 if (action->type == SIMIX_ACTION_SLEEP)
810 xbt_mallocator_release(simix_global->action_mallocator, action);
813 void SIMIX_process_sleep_suspend(smx_action_t action)
815 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
816 surf_action_suspend(action->sleep.surf_sleep);
819 void SIMIX_process_sleep_resume(smx_action_t action)
821 XBT_DEBUG("Action state is %d on process_sleep_resume.", action->state);
822 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
823 surf_action_resume(action->sleep.surf_sleep);
827 * \brief Calling this function makes the process to yield.
829 * Only the current process can call this function, giving back the control to
832 * \param self the current process
834 void SIMIX_process_yield(smx_process_t self)
836 XBT_DEBUG("Yield process '%s'", self->name);
838 /* Go into sleep and return control to maestro */
839 SIMIX_context_suspend(self->context);
841 /* Ok, maestro returned control to us */
842 XBT_DEBUG("Control returned to me: '%s'", self->name);
844 if (self->new_host) {
845 SIMIX_process_change_host(self, self->new_host);
846 self->new_host = NULL;
849 if (self->context->iwannadie){
850 XBT_DEBUG("I wanna die!");
851 SIMIX_process_stop(self);
854 if (self->suspended) {
855 XBT_DEBUG("Hey! I'm suspended.");
856 xbt_assert(!self->doexception, "Gloups! This exception may be lost by subsequent calls.");
858 SIMIX_process_suspend(self, self);
861 if (self->doexception) {
862 XBT_DEBUG("Wait, maestro left me an exception");
863 self->doexception = 0;
869 /* callback: context fetching */
870 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
872 return SIMIX_process_self()->running_ctx;
875 /* callback: termination */
876 void SIMIX_process_exception_terminate(xbt_ex_t * e)
882 smx_context_t SIMIX_process_get_context(smx_process_t p) {
886 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
891 * \brief Returns the list of processes to run.
893 xbt_dynar_t SIMIX_process_get_runnable(void)
895 return simix_global->process_to_run;
899 * \brief Returns the process from PID.
901 smx_process_t SIMIX_process_from_PID(int PID)
904 xbt_swag_foreach(proc, simix_global->process_list)
912 /** @brief returns a dynar containg all currently existing processes */
913 xbt_dynar_t SIMIX_processes_as_dynar(void) {
915 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
916 xbt_swag_foreach(proc, simix_global->process_list) {
917 xbt_dynar_push(res,&proc);
923 void SIMIX_process_on_exit_runall(smx_process_t process) {
924 s_smx_process_exit_fun_t exit_fun;
925 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
926 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
927 while (!xbt_dynar_is_empty(process->on_exit)) {
928 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
929 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
933 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
934 int_f_pvoid_pvoid_t fun, void *data) {
935 SIMIX_process_on_exit(process, fun, data);
938 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
939 xbt_assert(process, "current process not found: are you in maestro context ?");
941 if (!process->on_exit) {
942 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
945 s_smx_process_exit_fun_t exit_fun = {fun, data};
947 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
950 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
952 SIMIX_process_auto_restart_set(process, auto_restart);
955 * \brief Sets the auto-restart status of the process.
956 * If set to 1, the process will be automatically restarted when its host
959 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
960 process->auto_restart = auto_restart;
963 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
964 return SIMIX_process_restart(process, simcall->issuer);
967 * \brief Restart a process.
968 * Restart a process, starting it again from the beginning.
970 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
971 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
972 //retrieve the arguments of the old process
973 //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
974 s_smx_process_arg_t arg;
975 arg.code = process->code;
976 arg.hostname = sg_host_name(process->smx_host);
977 arg.kill_time = process->kill_time;
978 arg.argc = process->argc;
979 arg.data = process->data;
981 arg.argv = xbt_new(char*,process->argc + 1);
982 for (i = 0; i < arg.argc; i++) {
983 arg.argv[i] = xbt_strdup(process->argv[i]);
985 arg.argv[process->argc] = NULL;
986 arg.properties = NULL;
987 arg.auto_restart = process->auto_restart;
988 //kill the old process
989 SIMIX_process_kill(process,issuer);
990 //start the new process
991 smx_process_t new_process;
992 if (simix_global->create_process_function) {
993 simix_global->create_process_function(&new_process,
1005 simcall_process_create(&new_process,