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"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
16 "Logging specific to SIMIX (process)");
18 unsigned long simix_process_maxpid = 0;
21 * \brief Returns the current agent.
23 * This functions returns the currently running SIMIX process.
25 * \return The SIMIX process
27 smx_process_t SIMIX_process_self(void)
29 smx_context_t self_context = SIMIX_context_self();
31 return self_context ? SIMIX_context_get_process(self_context) : NULL;
35 * \brief Returns whether a process has pending asynchronous communications.
36 * \return true if there are asynchronous communications in this process
38 int SIMIX_process_has_pending_comms(smx_process_t process) {
40 return xbt_fifo_size(process->comms) > 0;
44 * \brief Moves a process to the list of processes to destroy.
46 void SIMIX_process_cleanup(smx_process_t process)
48 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
49 process->name, process, process->waiting_synchro);
51 SIMIX_process_on_exit_runall(process);
53 /* Unregister from the kill timer if any */
54 if (process->kill_timer != NULL)
55 SIMIX_timer_remove(process->kill_timer);
57 xbt_os_mutex_acquire(simix_global->mutex);
59 /* cancel non-blocking communications */
60 smx_synchro_t synchro;
61 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
63 /* make sure no one will finish the comm after this process is destroyed,
64 * because src_proc or dst_proc would be an invalid pointer */
65 SIMIX_comm_cancel(synchro);
67 if (synchro->comm.src_proc == process) {
68 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
69 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
70 synchro->comm.src_proc = NULL;
72 /* I'm not supposed to destroy a detached comm from the sender side, */
73 if (!synchro->comm.detached)
74 SIMIX_comm_destroy(synchro);
76 XBT_DEBUG("Don't destroy it since it's a detached comm");
79 else if (synchro->comm.dst_proc == process){
80 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
81 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
82 synchro->comm.dst_proc = NULL;
84 if (synchro->comm.detached && synchro->comm.refcount == 1
85 && synchro->comm.src_proc != NULL) {
86 /* the comm will be freed right now, remove it from the sender */
87 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
89 SIMIX_comm_destroy(synchro);
92 xbt_die("Communication synchro %p is in my list but I'm not the sender "
93 "or the receiver", synchro);
97 XBT_DEBUG("%p should not be run anymore",process);
98 xbt_swag_remove(process, simix_global->process_list);
99 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
100 xbt_swag_insert(process, simix_global->process_to_destroy);
101 process->context->iwannadie = 0;
103 xbt_os_mutex_release(simix_global->mutex);
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 = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
117 XBT_DEBUG("Getting rid of %p",process);
119 SIMIX_context_free(process->context);
121 /* Free the exception allocated at creation time */
122 free(process->running_ctx);
123 xbt_dict_free(&process->properties);
125 xbt_fifo_free(process->comms);
127 xbt_dynar_free(&process->on_exit);
129 xbt_free(process->name);
135 * \brief Creates and runs the maestro process
137 void SIMIX_create_maestro_process()
139 smx_process_t maestro = NULL;
141 /* Create maestro process and intilialize it */
142 maestro = xbt_new0(s_smx_process_t, 1);
143 maestro->pid = simix_process_maxpid++;
145 maestro->name = (char *) "";
146 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
147 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
148 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
149 maestro->simcall.issuer = maestro;
150 simix_global->maestro_process = maestro;
154 * \brief Stops a process.
156 * Stops the process, execute all the registered on_exit functions,
157 * register it to the list of the process to restart if needed
158 * and stops its context.
160 void SIMIX_process_stop(smx_process_t arg) {
161 /* execute the on_exit functions */
162 SIMIX_process_on_exit_runall(arg);
163 /* Add the process to the list of process to restart, only if
166 if (arg->auto_restart && !sg_host_get_state(arg->host)) {
167 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
168 sg_host_get_name(arg->host),
169 SIMIX_timer_get_date(arg->kill_timer),
170 arg->argc,arg->argv,arg->properties,
173 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
174 /* stop the context */
175 SIMIX_context_stop(arg->context);
179 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
180 * This function frees the argument.
181 * \return the process created
183 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
185 smx_process_t process = simix_global->create_process_function(
201 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
203 xbt_main_func_t code,
205 const char *hostname,
207 int argc, char **argv,
208 xbt_dict_t properties,
210 return (void*)SIMIX_process_create(name, code, data, hostname,
211 kill_time, argc, argv, properties, auto_restart,
215 static void kill_process(void* process)
217 simix_global->kill_process_function((smx_process_t) process);
221 * \brief Internal function to create a process.
223 * This function actually creates the process.
224 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
225 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
227 * \return the process created
229 smx_process_t SIMIX_process_create(
231 xbt_main_func_t code,
233 const char *hostname,
235 int argc, char **argv,
236 xbt_dict_t properties,
238 smx_process_t parent_process)
240 smx_process_t process = NULL;
241 sg_host_t host = sg_host_by_name(hostname);
243 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
245 if (!sg_host_get_state(host)) {
247 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
249 for (i = 0; i < argc; i++)
254 process = xbt_new0(s_smx_process_t, 1);
256 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
258 process->pid = simix_process_maxpid++;
259 process->name = xbt_strdup(name);
260 process->host = host;
261 process->data = data;
262 process->comms = xbt_fifo_new();
263 process->simcall.issuer = process;
265 if (parent_process) {
266 process->ppid = SIMIX_process_get_PID(parent_process);
271 /* Process data for auto-restart */
272 process->auto_restart = auto_restart;
273 process->code = code;
274 process->argc = argc;
275 process->argv = argv;
278 XBT_VERB("Create context %s", process->name);
279 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
281 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
282 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
285 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
289 process->properties = properties;
291 /* Add the process to it's host process list */
292 xbt_swag_insert(process, sg_host_simix(host)->process_list);
294 XBT_DEBUG("Start context '%s'", process->name);
296 /* Now insert it in the global process list and in the process to run list */
297 xbt_swag_insert(process, simix_global->process_list);
298 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
299 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
301 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
302 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
303 sg_host_get_name(process->host), kill_time);
304 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
311 * \brief Executes the processes from simix_global->process_to_run.
313 * The processes of simix_global->process_to_run are run (in parallel if
314 * possible). On exit, simix_global->process_to_run is empty, and
315 * simix_global->process_that_ran contains the list of processes that just ran.
316 * The two lists are swapped so, be careful when using them before and after a
317 * call to this function.
319 void SIMIX_process_runall(void)
321 SIMIX_context_runall();
323 xbt_dynar_t tmp = simix_global->process_that_ran;
324 simix_global->process_that_ran = simix_global->process_to_run;
325 simix_global->process_to_run = tmp;
326 xbt_dynar_reset(simix_global->process_to_run);
329 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
330 SIMIX_process_kill(process, simcall->issuer);
333 * \brief Internal function to kill a SIMIX process.
335 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
336 * or directly for SIMIX internal purposes.
338 * \param process poor victim
339 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
341 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
343 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
345 process->context->iwannadie = 1;
346 process->blocked = 0;
347 process->suspended = 0;
348 process->doexception = 0;
350 /* destroy the blocking synchro if any */
351 if (process->waiting_synchro) {
353 switch (process->waiting_synchro->type) {
355 case SIMIX_SYNC_EXECUTE:
356 case SIMIX_SYNC_PARALLEL_EXECUTE:
357 SIMIX_process_execution_destroy(process->waiting_synchro);
360 case SIMIX_SYNC_COMMUNICATE:
361 xbt_fifo_remove(process->comms, process->waiting_synchro);
362 SIMIX_comm_cancel(process->waiting_synchro);
363 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
364 SIMIX_comm_destroy(process->waiting_synchro);
367 case SIMIX_SYNC_SLEEP:
368 SIMIX_process_sleep_destroy(process->waiting_synchro);
371 case SIMIX_SYNC_JOIN:
372 SIMIX_process_sleep_destroy(process->waiting_synchro);
375 case SIMIX_SYNC_SYNCHRO:
376 SIMIX_synchro_stop_waiting(process, &process->simcall);
377 SIMIX_synchro_destroy(process->waiting_synchro);
381 SIMIX_io_destroy(process->waiting_synchro);
386 process->waiting_synchro = NULL;
388 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
389 XBT_DEBUG("Inserting %s in the to_run list", process->name);
390 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
395 /** @brief Ask another process to raise the given exception
397 * @param cat category of exception
398 * @param value value associated to the exception
399 * @param msg string information associated to the exception
401 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
402 SMX_EXCEPTION(process, cat, value, msg);
404 if (process->suspended)
405 SIMIX_process_resume(process,SIMIX_process_self());
407 /* cancel the blocking synchro if any */
408 if (process->waiting_synchro) {
410 switch (process->waiting_synchro->type) {
412 case SIMIX_SYNC_EXECUTE:
413 case SIMIX_SYNC_PARALLEL_EXECUTE:
414 SIMIX_process_execution_cancel(process->waiting_synchro);
417 case SIMIX_SYNC_COMMUNICATE:
418 xbt_fifo_remove(process->comms, process->waiting_synchro);
419 SIMIX_comm_cancel(process->waiting_synchro);
422 case SIMIX_SYNC_SLEEP:
423 case SIMIX_SYNC_JOIN:
424 SIMIX_process_sleep_destroy(process->waiting_synchro);
425 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
426 XBT_DEBUG("Inserting %s in the to_run list", process->name);
427 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
431 case SIMIX_SYNC_SYNCHRO:
432 SIMIX_synchro_stop_waiting(process, &process->simcall);
436 SIMIX_io_destroy(process->waiting_synchro);
441 process->waiting_synchro = NULL;
445 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
446 SIMIX_process_killall(simcall->issuer, reset_pid);
449 * \brief Kills all running processes.
450 * \param issuer this one will not be killed
452 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
454 smx_process_t p = NULL;
456 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
458 SIMIX_process_kill(p,issuer);
463 simix_process_maxpid = reset_pid;
465 SIMIX_context_runall();
467 SIMIX_process_empty_trash();
470 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
472 process->new_host = dest;
474 void SIMIX_process_change_host(smx_process_t process,
477 xbt_assert((process != NULL), "Invalid parameters");
478 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
479 process->host = dest;
480 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
484 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
486 smx_synchro_t sync_suspend =
487 SIMIX_process_suspend(process, simcall->issuer);
489 if (process != simcall->issuer) {
490 SIMIX_simcall_answer(simcall);
492 xbt_fifo_push(sync_suspend->simcalls, simcall);
493 process->waiting_synchro = sync_suspend;
494 SIMIX_host_execution_suspend(process->waiting_synchro);
496 /* If we are suspending ourselves, then just do not finish the simcall now */
499 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
501 xbt_assert((process != NULL), "Invalid parameters");
503 if (process->suspended) {
504 XBT_DEBUG("Process '%s' is already suspended", process->name);
508 process->suspended = 1;
510 /* If we are suspending another process, and it is waiting on a sync,
511 suspend its synchronization. */
512 if (process != issuer) {
514 if (process->waiting_synchro) {
516 switch (process->waiting_synchro->type) {
518 case SIMIX_SYNC_EXECUTE:
519 case SIMIX_SYNC_PARALLEL_EXECUTE:
520 SIMIX_host_execution_suspend(process->waiting_synchro);
523 case SIMIX_SYNC_COMMUNICATE:
524 SIMIX_comm_suspend(process->waiting_synchro);
527 case SIMIX_SYNC_SLEEP:
528 SIMIX_process_sleep_suspend(process->waiting_synchro);
531 case SIMIX_SYNC_SYNCHRO:
532 /* Suspension is delayed to when the process is rescheduled. */
536 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
537 (int)process->waiting_synchro->type);
541 /* Suspension is delayed to when the process is rescheduled. */
545 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
546 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
550 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
551 SIMIX_process_resume(process, simcall->issuer);
554 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
556 XBT_IN("process = %p, issuer = %p", process, issuer);
558 if(process->context->iwannadie) {
559 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
563 if(!process->suspended) return;
564 process->suspended = 0;
566 /* If we are resuming another process, resume the synchronization it was waiting for
567 if any. Otherwise add it to the list of process to run in the next round. */
568 if (process != issuer) {
570 if (process->waiting_synchro) {
572 switch (process->waiting_synchro->type) {
574 case SIMIX_SYNC_EXECUTE:
575 case SIMIX_SYNC_PARALLEL_EXECUTE:
576 SIMIX_host_execution_resume(process->waiting_synchro);
579 case SIMIX_SYNC_COMMUNICATE:
580 SIMIX_comm_resume(process->waiting_synchro);
583 case SIMIX_SYNC_SLEEP:
584 SIMIX_process_sleep_resume(process->waiting_synchro);
587 case SIMIX_SYNC_SYNCHRO:
588 /* I cannot resume it now. This is delayed to when the process is rescheduled at
589 * the end of the synchro. */
593 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
594 (int)process->waiting_synchro->type);
597 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
602 int SIMIX_process_get_maxpid(void) {
603 return simix_process_maxpid;
606 int SIMIX_process_count(void)
608 return xbt_swag_size(simix_global->process_list);
611 int SIMIX_process_get_PID(smx_process_t self){
618 int SIMIX_process_get_PPID(smx_process_t self){
625 void* SIMIX_process_self_get_data(smx_process_t self)
627 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
632 return SIMIX_process_get_data(self);
635 void SIMIX_process_self_set_data(smx_process_t self, void *data)
637 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
639 SIMIX_process_set_data(self, data);
642 void* SIMIX_process_get_data(smx_process_t process)
644 return process->data;
647 void SIMIX_process_set_data(smx_process_t process, void *data)
649 process->data = data;
652 sg_host_t SIMIX_process_get_host(smx_process_t process)
654 return process->host;
657 /* needs to be public and without simcall because it is called
658 by exceptions and logging events */
659 const char* SIMIX_process_self_get_name(void) {
661 smx_process_t process = SIMIX_process_self();
662 if (process == NULL || process == simix_global->maestro_process)
665 return SIMIX_process_get_name(process);
668 const char* SIMIX_process_get_name(smx_process_t process)
670 return process->name;
673 smx_process_t SIMIX_process_get_by_name(const char* name)
677 xbt_swag_foreach(proc, simix_global->process_list)
679 if(!strcmp(name, proc->name))
685 int SIMIX_process_is_suspended(smx_process_t process)
687 return process->suspended;
690 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
692 return process->properties;
695 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
697 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
698 xbt_fifo_push(sync->simcalls, simcall);
699 simcall->issuer->waiting_synchro = sync;
702 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
703 if (sync->sleep.surf_sleep) {
704 surf_action_cancel(sync->sleep.surf_sleep);
706 smx_simcall_t simcall;
707 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
708 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
709 simcall->issuer->waiting_synchro = NULL;
710 if (simcall->issuer->suspended) {
711 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
712 simcall->issuer->suspended = 0;
713 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
715 SIMIX_simcall_answer(simcall);
718 surf_action_unref(sync->sleep.surf_sleep);
719 sync->sleep.surf_sleep = NULL;
721 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
725 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
727 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
728 res->type = SIMIX_SYNC_JOIN;
729 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
733 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
735 if (MC_is_active() || MC_record_replay_is_active()) {
736 MC_process_clock_add(simcall->issuer, duration);
737 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
738 SIMIX_simcall_answer(simcall);
741 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
742 xbt_fifo_push(sync->simcalls, simcall);
743 simcall->issuer->waiting_synchro = sync;
746 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
748 sg_host_t host = process->host;
750 /* check if the host is active */
751 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
752 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
753 sg_host_get_name(host));
756 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
757 synchro->type = SIMIX_SYNC_SLEEP;
758 synchro->name = NULL;
759 synchro->category = NULL;
761 synchro->sleep.host = host;
762 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
764 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
765 XBT_DEBUG("Create sleep synchronization %p", synchro);
770 void SIMIX_post_process_sleep(smx_synchro_t synchro)
772 smx_simcall_t simcall;
774 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
776 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
778 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
779 case SURF_ACTION_FAILED:
780 simcall->issuer->context->iwannadie = 1;
781 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
782 state = SIMIX_SRC_HOST_FAILURE;
785 case SURF_ACTION_DONE:
793 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
794 simcall->issuer->context->iwannadie = 1;
796 simcall_process_sleep__set__result(simcall, state);
797 simcall->issuer->waiting_synchro = NULL;
798 if (simcall->issuer->suspended) {
799 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
800 simcall->issuer->suspended = 0;
801 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
803 SIMIX_simcall_answer(simcall);
807 SIMIX_process_sleep_destroy(synchro);
810 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
812 XBT_DEBUG("Destroy synchro %p", synchro);
813 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
815 if (synchro->sleep.surf_sleep) {
816 surf_action_unref(synchro->sleep.surf_sleep);
817 synchro->sleep.surf_sleep = NULL;
819 if (synchro->type == SIMIX_SYNC_SLEEP)
820 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
823 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
825 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
826 surf_action_suspend(synchro->sleep.surf_sleep);
829 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
831 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
832 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
833 surf_action_resume(synchro->sleep.surf_sleep);
837 * \brief Calling this function makes the process to yield.
839 * Only the current process can call this function, giving back the control to
842 * \param self the current process
844 void SIMIX_process_yield(smx_process_t self)
846 XBT_DEBUG("Yield process '%s'", self->name);
848 /* Go into sleep and return control to maestro */
849 SIMIX_context_suspend(self->context);
851 /* Ok, maestro returned control to us */
852 XBT_DEBUG("Control returned to me: '%s'", self->name);
854 if (self->new_host) {
855 SIMIX_process_change_host(self, self->new_host);
856 self->new_host = NULL;
859 if (self->context->iwannadie){
860 XBT_DEBUG("I wanna die!");
861 SIMIX_process_stop(self);
864 if (self->suspended) {
865 XBT_DEBUG("Hey! I'm suspended.");
866 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
868 SIMIX_process_suspend(self, self);
871 if (self->doexception) {
872 XBT_DEBUG("Wait, maestro left me an exception");
873 self->doexception = 0;
879 /* callback: context fetching */
880 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
882 return SIMIX_process_self()->running_ctx;
885 /* callback: termination */
886 void SIMIX_process_exception_terminate(xbt_ex_t * e)
892 smx_context_t SIMIX_process_get_context(smx_process_t p) {
896 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
901 * \brief Returns the list of processes to run.
903 xbt_dynar_t SIMIX_process_get_runnable(void)
905 return simix_global->process_to_run;
909 * \brief Returns the process from PID.
911 smx_process_t SIMIX_process_from_PID(int PID)
914 xbt_swag_foreach(proc, simix_global->process_list) {
915 if (proc->pid == (unsigned long) PID)
921 /** @brief returns a dynar containg all currently existing processes */
922 xbt_dynar_t SIMIX_processes_as_dynar(void) {
924 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
925 xbt_swag_foreach(proc, simix_global->process_list) {
926 xbt_dynar_push(res,&proc);
932 void SIMIX_process_on_exit_runall(smx_process_t process) {
933 s_smx_process_exit_fun_t exit_fun;
934 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
935 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
936 while (!xbt_dynar_is_empty(process->on_exit)) {
937 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
938 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
942 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
943 xbt_assert(process, "current process not found: are you in maestro context ?");
945 if (!process->on_exit) {
946 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
949 s_smx_process_exit_fun_t exit_fun = {fun, data};
951 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
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 simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
964 return SIMIX_process_restart(process, simcall->issuer);
966 /** @brief Restart a process, starting it again from the beginning. */
967 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
968 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
969 //retrieve the arguments of the old process
970 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
971 s_smx_process_arg_t arg;
972 arg.code = process->code;
973 arg.hostname = sg_host_get_name(process->host);
974 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
975 arg.argc = process->argc;
976 arg.data = process->data;
978 arg.argv = xbt_new(char*,process->argc + 1);
979 for (i = 0; i < arg.argc; i++) {
980 arg.argv[i] = xbt_strdup(process->argv[i]);
982 arg.argv[process->argc] = NULL;
983 arg.properties = NULL;
984 arg.auto_restart = process->auto_restart;
985 //kill the old process
986 SIMIX_process_kill(process,issuer);
987 //start the new process
988 smx_process_t new_process;
989 if (simix_global->create_process_function) {
990 new_process = simix_global->create_process_function(
1002 new_process = simcall_process_create(