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 /* I'm not supposed to destroy a detached comm from the sender side, */
68 if (!action->comm.detached)
69 SIMIX_comm_destroy(action);
71 XBT_DEBUG("Don't destroy it since it's a detached comm");
74 else if (action->comm.dst_proc == process){
75 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
76 action, (int)action->state, action->comm.src_proc, action->comm.dst_proc);
77 action->comm.dst_proc = NULL;
79 if (action->comm.detached && action->comm.refcount == 1
80 && action->comm.src_proc != NULL) {
81 /* the comm will be freed right now, remove it from the sender */
82 xbt_fifo_remove(action->comm.src_proc->comms, action);
84 SIMIX_comm_destroy(action);
87 xbt_die("Communication action %p is in my list but I'm not the sender "
88 "or the receiver", action);
92 xbt_swag_remove(process, simix_global->process_list);
93 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
94 xbt_swag_insert(process, simix_global->process_to_destroy);
95 process->context->iwannadie = 0;
101 * Should be called some time to time to free the memory allocated for processes
102 * that have finished (or killed).
104 void SIMIX_process_empty_trash(void)
106 smx_process_t process = NULL;
108 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
109 SIMIX_context_free(process->context);
111 /* Free the exception allocated at creation time */
112 free(process->running_ctx);
113 xbt_dict_free(&process->properties);
115 xbt_fifo_free(process->comms);
117 xbt_dynar_free(&process->on_exit);
119 xbt_free(process->name);
125 * \brief Creates and runs the maestro process
127 void SIMIX_create_maestro_process()
129 smx_process_t maestro = NULL;
131 /* Create maestro process and intilialize it */
132 maestro = xbt_new0(s_smx_process_t, 1);
133 maestro->pid = simix_process_maxpid++;
135 maestro->name = (char *) "";
136 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
137 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
138 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
139 maestro->simcall.issuer = maestro;
140 simix_global->maestro_process = maestro;
144 * \brief Stops a process.
146 * Stops the process, execute all the registered on_exit functions,
147 * register it to the list of the process to restart if needed
148 * and stops its context.
150 void SIMIX_process_stop(smx_process_t arg) {
151 /* execute the on_exit functions */
152 SIMIX_process_on_exit_runall(arg);
153 /* Add the process to the list of process to restart, only if
156 if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
157 SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
158 sg_host_name(arg->smx_host),
160 arg->argc,arg->argv,arg->properties,
163 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
164 /* stop the context */
165 SIMIX_context_stop(arg->context);
169 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
170 * This function frees the argument.
171 * \return the process created
173 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
175 smx_process_t process;
176 simix_global->create_process_function(&process,
192 void SIMIX_pre_process_create(smx_simcall_t simcall,
193 smx_process_t *process,
195 xbt_main_func_t code,
197 const char *hostname,
199 int argc, char **argv,
200 xbt_dict_t properties,
202 SIMIX_process_create(process, name, code, data, hostname,
203 kill_time, argc, argv, properties, auto_restart,
207 * \brief Internal function to create a process.
209 * This function actually creates the process.
210 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
211 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
213 * \return the process created
215 void SIMIX_process_create(smx_process_t *process,
217 xbt_main_func_t code,
219 const char *hostname,
221 int argc, char **argv,
222 xbt_dict_t properties,
224 smx_process_t parent_process)
227 smx_host_t host = SIMIX_host_get_by_name(hostname);
229 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
231 if (!SIMIX_host_get_state(host)) {
233 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
235 for (i = 0; i < argc; i++)
240 *process = xbt_new0(s_smx_process_t, 1);
242 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
244 (*process)->pid = simix_process_maxpid++;
245 (*process)->name = xbt_strdup(name);
246 (*process)->smx_host = host;
247 (*process)->data = data;
248 (*process)->comms = xbt_fifo_new();
249 (*process)->simcall.issuer = *process;
251 if (parent_process) {
252 (*process)->ppid = SIMIX_process_get_PID(parent_process);
254 (*process)->ppid = -1;
257 /* Process data for auto-restart */
258 (*process)->auto_restart = auto_restart;
259 (*process)->code = code;
260 (*process)->argc = argc;
261 (*process)->argv = argv;
262 (*process)->kill_time = kill_time;
265 XBT_VERB("Create context %s", (*process)->name);
266 (*process)->context = SIMIX_context_new(code, argc, argv,
267 simix_global->cleanup_process_function, *process);
269 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
270 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
273 MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
277 (*process)->properties = properties;
279 /* Add the process to it's host process list */
280 xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
282 XBT_DEBUG("Start context '%s'", (*process)->name);
284 /* Now insert it in the global process list and in the process to run list */
285 xbt_swag_insert(*process, simix_global->process_list);
286 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
287 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
289 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
290 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
291 sg_host_name((*process)->smx_host), kill_time);
292 SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
298 * \brief Executes the processes from simix_global->process_to_run.
300 * The processes of simix_global->process_to_run are run (in parallel if
301 * possible). On exit, simix_global->process_to_run is empty, and
302 * simix_global->process_that_ran contains the list of processes that just ran.
303 * The two lists are swapped so, be careful when using them before and after a
304 * call to this function.
306 void SIMIX_process_runall(void)
308 SIMIX_context_runall();
310 xbt_dynar_t tmp = simix_global->process_that_ran;
311 simix_global->process_that_ran = simix_global->process_to_run;
312 simix_global->process_to_run = tmp;
313 xbt_dynar_reset(simix_global->process_to_run);
316 void SIMIX_pre_process_kill(smx_simcall_t simcall, smx_process_t process) {
317 SIMIX_process_kill(process, simcall->issuer);
320 * \brief Internal function to kill a SIMIX process.
322 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
323 * or directly for SIMIX internal purposes.
325 * \param process poor victim
326 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
328 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
330 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
332 process->context->iwannadie = 1;
333 process->blocked = 0;
334 process->suspended = 0;
335 /* FIXME: set doexception to 0 also? */
337 /* destroy the blocking action if any */
338 if (process->waiting_action) {
340 switch (process->waiting_action->type) {
342 case SIMIX_ACTION_EXECUTE:
343 case SIMIX_ACTION_PARALLEL_EXECUTE:
344 SIMIX_host_execution_destroy(process->waiting_action);
347 case SIMIX_ACTION_COMMUNICATE:
348 xbt_fifo_remove(process->comms, process->waiting_action);
349 SIMIX_comm_cancel(process->waiting_action);
350 SIMIX_comm_destroy(process->waiting_action);
353 case SIMIX_ACTION_SLEEP:
354 SIMIX_process_sleep_destroy(process->waiting_action);
357 case SIMIX_ACTION_JOIN:
358 SIMIX_process_sleep_destroy(process->waiting_action);
361 case SIMIX_ACTION_SYNCHRO:
362 SIMIX_synchro_stop_waiting(process, &process->simcall);
363 SIMIX_synchro_destroy(process->waiting_action);
366 case SIMIX_ACTION_IO:
367 SIMIX_io_destroy(process->waiting_action);
370 /* **************************************/
371 /* TUTORIAL: New API */
372 case SIMIX_ACTION_NEW_API:
373 SIMIX_new_api_destroy(process->waiting_action);
375 /* **************************************/
379 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
380 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
385 void SIMIX_pre_process_killall(smx_simcall_t simcall, int reset_pid) {
386 SIMIX_process_killall(simcall->issuer, reset_pid);
389 * \brief Kills all running processes.
390 * \param issuer this one will not be killed
392 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
394 smx_process_t p = NULL;
396 while ((p = xbt_swag_extract(simix_global->process_list))) {
398 SIMIX_process_kill(p,issuer);
403 simix_process_maxpid = reset_pid;
405 SIMIX_context_runall();
407 SIMIX_process_empty_trash();
410 void SIMIX_pre_process_change_host(smx_simcall_t simcall, smx_process_t process,
413 process->new_host = dest;
415 void SIMIX_process_change_host(smx_process_t process,
418 xbt_assert((process != NULL), "Invalid parameters");
419 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
420 process->smx_host = dest;
421 xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
425 void SIMIX_pre_process_suspend(smx_simcall_t simcall, smx_process_t process)
427 smx_action_t action_suspend =
428 SIMIX_process_suspend(process, simcall->issuer);
430 if (process != simcall->issuer) {
431 SIMIX_simcall_answer(simcall);
433 xbt_fifo_push(action_suspend->simcalls, simcall);
434 process->waiting_action = action_suspend;
435 SIMIX_host_execution_suspend(process->waiting_action);
437 /* If we are suspending ourselves, then just do not finish the simcall now */
440 smx_action_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
442 xbt_assert((process != NULL), "Invalid parameters");
444 if (process->suspended) {
445 XBT_DEBUG("Process '%s' is already suspended", process->name);
449 process->suspended = 1;
451 /* If we are suspending another process, and it is waiting on an action,
452 suspend its action. */
453 if (process != issuer) {
455 if (process->waiting_action) {
457 switch (process->waiting_action->type) {
459 case SIMIX_ACTION_EXECUTE:
460 case SIMIX_ACTION_PARALLEL_EXECUTE:
461 SIMIX_host_execution_suspend(process->waiting_action);
464 case SIMIX_ACTION_COMMUNICATE:
465 SIMIX_comm_suspend(process->waiting_action);
468 case SIMIX_ACTION_SLEEP:
469 SIMIX_process_sleep_suspend(process->waiting_action);
472 case SIMIX_ACTION_SYNCHRO:
473 /* Suspension is delayed to when the process is rescheduled. */
477 xbt_die("Internal error in SIMIX_process_suspend: unexpected action type %d",
478 (int)process->waiting_action->type);
482 /* Suspension is delayed to when the process is rescheduled. */
486 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
487 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
491 void SIMIX_pre_process_resume(smx_simcall_t simcall, smx_process_t process){
492 SIMIX_process_resume(process, simcall->issuer);
495 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
497 XBT_IN("process = %p, issuer = %p", process, issuer);
499 if(process->context->iwannadie) {
500 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
504 if(!process->suspended) return;
505 process->suspended = 0;
507 /* If we are resuming another process, resume the action it was waiting for
508 if any. Otherwise add it to the list of process to run in the next round. */
509 if (process != issuer) {
511 if (process->waiting_action) {
513 switch (process->waiting_action->type) {
515 case SIMIX_ACTION_EXECUTE:
516 case SIMIX_ACTION_PARALLEL_EXECUTE:
517 SIMIX_host_execution_resume(process->waiting_action);
520 case SIMIX_ACTION_COMMUNICATE:
521 SIMIX_comm_resume(process->waiting_action);
524 case SIMIX_ACTION_SLEEP:
525 SIMIX_process_sleep_resume(process->waiting_action);
528 case SIMIX_ACTION_SYNCHRO:
529 /* I cannot resume it now. This is delayed to when the process is rescheduled at
530 * the end of the synchro. */
534 xbt_die("Internal error in SIMIX_process_resume: unexpected action type %d",
535 (int)process->waiting_action->type);
538 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
543 int SIMIX_process_get_maxpid(void) {
544 return simix_process_maxpid;
547 int SIMIX_pre_process_count(smx_simcall_t simcall){
548 return SIMIX_process_count();
550 int SIMIX_process_count(void)
552 return xbt_swag_size(simix_global->process_list);
555 int SIMIX_pre_process_get_PID(smx_simcall_t simcall, smx_process_t self){
556 return SIMIX_process_get_PID(self);
559 int SIMIX_process_get_PID(smx_process_t self){
566 int SIMIX_pre_process_get_PPID(smx_simcall_t simcall, smx_process_t self){
567 return SIMIX_process_get_PPID(self);
570 int SIMIX_process_get_PPID(smx_process_t self){
577 void* SIMIX_pre_process_self_get_data(smx_simcall_t simcall, smx_process_t self){
578 return SIMIX_process_self_get_data(self);
581 void* SIMIX_process_self_get_data(smx_process_t self)
583 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
588 return SIMIX_process_get_data(self);
591 void SIMIX_pre_process_set_data(smx_simcall_t simcall, smx_process_t process,
593 SIMIX_process_set_data(process, data);
595 void SIMIX_process_self_set_data(smx_process_t self, void *data)
597 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
599 SIMIX_process_set_data(self, data);
602 void* SIMIX_pre_process_get_data(smx_simcall_t simcall, smx_process_t process){
603 return SIMIX_process_get_data(process);
605 void* SIMIX_process_get_data(smx_process_t process)
607 return process->data;
610 void SIMIX_process_set_data(smx_process_t process, void *data)
612 process->data = data;
615 smx_host_t SIMIX_pre_process_get_host(smx_simcall_t simcall, smx_process_t process){
616 return SIMIX_process_get_host(process);
618 smx_host_t SIMIX_process_get_host(smx_process_t process)
620 return process->smx_host;
623 /* needs to be public and without simcall because it is called
624 by exceptions and logging events */
625 const char* SIMIX_process_self_get_name(void) {
627 smx_process_t process = SIMIX_process_self();
628 if (process == NULL || process == simix_global->maestro_process)
631 return SIMIX_process_get_name(process);
634 const char* SIMIX_pre_process_get_name(smx_simcall_t simcall, smx_process_t process) {
635 return SIMIX_process_get_name(process);
637 const char* SIMIX_process_get_name(smx_process_t process)
639 return process->name;
642 smx_process_t SIMIX_process_get_by_name(const char* name)
646 xbt_swag_foreach(proc, simix_global->process_list)
648 if(!strcmp(name, proc->name))
654 int SIMIX_pre_process_is_suspended(smx_simcall_t simcall, smx_process_t process){
655 return SIMIX_process_is_suspended(process);
657 int SIMIX_process_is_suspended(smx_process_t process)
659 return process->suspended;
662 xbt_dict_t SIMIX_pre_process_get_properties(smx_simcall_t simcall, smx_process_t process){
663 return SIMIX_process_get_properties(process);
665 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
667 return process->properties;
670 void SIMIX_pre_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
672 smx_action_t action = SIMIX_process_join(simcall->issuer, process, timeout);
673 xbt_fifo_push(action->simcalls, simcall);
674 simcall->issuer->waiting_action = action;
677 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_action_t action){
678 if (action->sleep.surf_sleep) {
679 surf_action_cancel(action->sleep.surf_sleep);
681 smx_simcall_t simcall;
682 while ((simcall = xbt_fifo_shift(action->simcalls))) {
683 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
684 simcall->issuer->waiting_action = NULL;
685 if (simcall->issuer->suspended) {
686 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
687 simcall->issuer->suspended = 0;
688 SIMIX_pre_process_suspend(simcall, simcall->issuer);
690 SIMIX_simcall_answer(simcall);
693 surf_action_unref(action->sleep.surf_sleep);
694 action->sleep.surf_sleep = NULL;
696 xbt_mallocator_release(simix_global->action_mallocator, action);
700 smx_action_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
702 smx_action_t res = SIMIX_process_sleep(issuer, timeout);
703 res->type = SIMIX_ACTION_JOIN;
704 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
708 void SIMIX_pre_process_sleep(smx_simcall_t simcall, double duration)
710 if (MC_is_active()) {
711 MC_process_clock_add(simcall->issuer, duration);
712 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
713 SIMIX_simcall_answer(simcall);
716 smx_action_t action = SIMIX_process_sleep(simcall->issuer, duration);
717 xbt_fifo_push(action->simcalls, simcall);
718 simcall->issuer->waiting_action = action;
721 smx_action_t SIMIX_process_sleep(smx_process_t process, double duration)
724 smx_host_t host = process->smx_host;
726 /* check if the host is active */
727 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
728 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
732 action = xbt_mallocator_get(simix_global->action_mallocator);
733 action->type = SIMIX_ACTION_SLEEP;
736 action->category = NULL;
739 action->sleep.host = host;
740 action->sleep.surf_sleep =
741 surf_workstation_sleep(host, duration);
743 surf_action_set_data(action->sleep.surf_sleep, action);
744 XBT_DEBUG("Create sleep action %p", action);
749 void SIMIX_post_process_sleep(smx_action_t action)
751 smx_simcall_t simcall;
753 xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
755 while ((simcall = xbt_fifo_shift(action->simcalls))) {
757 switch(surf_action_get_state(action->sleep.surf_sleep)){
758 case SURF_ACTION_FAILED:
759 simcall->issuer->context->iwannadie = 1;
760 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
761 state = SIMIX_SRC_HOST_FAILURE;
764 case SURF_ACTION_DONE:
772 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
773 simcall->issuer->context->iwannadie = 1;
775 simcall_process_sleep__set__result(simcall, state);
776 simcall->issuer->waiting_action = NULL;
777 if (simcall->issuer->suspended) {
778 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
779 simcall->issuer->suspended = 0;
780 SIMIX_pre_process_suspend(simcall, simcall->issuer);
782 SIMIX_simcall_answer(simcall);
786 SIMIX_process_sleep_destroy(action);
789 void SIMIX_process_sleep_destroy(smx_action_t action)
791 XBT_DEBUG("Destroy action %p", action);
792 xbt_assert(action->type == SIMIX_ACTION_SLEEP || action->type == SIMIX_ACTION_JOIN);
794 if (action->sleep.surf_sleep) {
795 surf_action_unref(action->sleep.surf_sleep);
796 action->sleep.surf_sleep = NULL;
798 if (action->type == SIMIX_ACTION_SLEEP)
799 xbt_mallocator_release(simix_global->action_mallocator, action);
802 void SIMIX_process_sleep_suspend(smx_action_t action)
804 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
805 surf_action_suspend(action->sleep.surf_sleep);
808 void SIMIX_process_sleep_resume(smx_action_t action)
810 XBT_DEBUG("Action state is %d on process_sleep_resume.", action->state);
811 xbt_assert(action->type == SIMIX_ACTION_SLEEP);
812 surf_action_resume(action->sleep.surf_sleep);
816 * \brief Calling this function makes the process to yield.
818 * Only the current process can call this function, giving back the control to
821 * \param self the current process
823 void SIMIX_process_yield(smx_process_t self)
825 XBT_DEBUG("Yield process '%s'", self->name);
827 /* Go into sleep and return control to maestro */
828 SIMIX_context_suspend(self->context);
830 /* Ok, maestro returned control to us */
831 XBT_DEBUG("Control returned to me: '%s'", self->name);
833 if (self->new_host) {
834 SIMIX_process_change_host(self, self->new_host);
835 self->new_host = NULL;
838 if (self->context->iwannadie){
839 XBT_DEBUG("I wanna die!");
840 SIMIX_process_stop(self);
843 if (self->suspended) {
844 XBT_DEBUG("Hey! I'm suspended.");
845 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
847 SIMIX_process_suspend(self, self);
850 if (self->doexception) {
851 XBT_DEBUG("Wait, maestro left me an exception");
852 self->doexception = 0;
858 /* callback: context fetching */
859 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
861 return SIMIX_process_self()->running_ctx;
864 /* callback: termination */
865 void SIMIX_process_exception_terminate(xbt_ex_t * e)
871 smx_context_t SIMIX_process_get_context(smx_process_t p) {
875 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
880 * \brief Returns the list of processes to run.
882 xbt_dynar_t SIMIX_process_get_runnable(void)
884 return simix_global->process_to_run;
888 * \brief Returns the process from PID.
890 smx_process_t SIMIX_process_from_PID(int PID)
893 xbt_swag_foreach(proc, simix_global->process_list)
901 /** @brief returns a dynar containg all currently existing processes */
902 xbt_dynar_t SIMIX_processes_as_dynar(void) {
904 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
905 xbt_swag_foreach(proc, simix_global->process_list) {
906 xbt_dynar_push(res,&proc);
912 void SIMIX_process_on_exit_runall(smx_process_t process) {
913 s_smx_process_exit_fun_t exit_fun;
914 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
915 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
916 while (!xbt_dynar_is_empty(process->on_exit)) {
917 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
918 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
922 void SIMIX_pre_process_on_exit(smx_simcall_t simcall, smx_process_t process,
923 int_f_pvoid_pvoid_t fun, void *data) {
924 SIMIX_process_on_exit(process, fun, data);
927 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
928 xbt_assert(process, "current process not found: are you in maestro context ?");
930 if (!process->on_exit) {
931 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
934 s_smx_process_exit_fun_t exit_fun = {fun, data};
936 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
939 void SIMIX_pre_process_auto_restart_set(smx_simcall_t simcall, smx_process_t process,
941 SIMIX_process_auto_restart_set(process, auto_restart);
944 * \brief Sets the auto-restart status of the process.
945 * If set to 1, the process will be automatically restarted when its host
948 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
949 process->auto_restart = auto_restart;
952 smx_process_t SIMIX_pre_process_restart(smx_simcall_t simcall, smx_process_t process) {
953 return SIMIX_process_restart(process, simcall->issuer);
956 * \brief Restart a process.
957 * Restart a process, starting it again from the beginning.
959 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
960 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
961 //retrieve the arguments of the old process
962 //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
963 s_smx_process_arg_t arg;
964 arg.code = process->code;
965 arg.hostname = sg_host_name(process->smx_host);
966 arg.kill_time = process->kill_time;
967 arg.argc = process->argc;
968 arg.data = process->data;
970 arg.argv = xbt_new(char*,process->argc + 1);
971 for (i = 0; i < arg.argc; i++) {
972 arg.argv[i] = xbt_strdup(process->argv[i]);
974 arg.argv[process->argc] = NULL;
975 arg.properties = NULL;
976 arg.auto_restart = process->auto_restart;
977 //kill the old process
978 SIMIX_process_kill(process,issuer);
979 //start the new process
980 smx_process_t new_process;
981 if (simix_global->create_process_function) {
982 simix_global->create_process_function(&new_process,
994 simcall_process_create(&new_process,