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 XBT_INLINE 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 = 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 = 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_new(xbt_running_ctx_t, 1);
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 && !SIMIX_host_get_state(arg->host)) {
167 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
168 sg_host_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_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 * \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 smx_process_t SIMIX_process_create(
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)
234 smx_process_t process = NULL;
235 sg_host_t host = sg_host_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->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);
265 /* Process data for auto-restart */
266 process->auto_restart = auto_restart;
267 process->code = code;
268 process->argc = argc;
269 process->argv = argv;
272 XBT_VERB("Create context %s", process->name);
273 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
275 process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
276 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
279 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
283 process->properties = properties;
285 /* Add the process to it's host process list */
286 xbt_swag_insert(process, sg_host_simix(host)->process_list);
288 XBT_DEBUG("Start context '%s'", process->name);
290 /* Now insert it in the global process list and in the process to run list */
291 xbt_swag_insert(process, simix_global->process_list);
292 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_name(host));
293 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
295 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
296 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
297 sg_host_name(process->host), kill_time);
298 process->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, process);
305 * \brief Executes the processes from simix_global->process_to_run.
307 * The processes of simix_global->process_to_run are run (in parallel if
308 * possible). On exit, simix_global->process_to_run is empty, and
309 * simix_global->process_that_ran contains the list of processes that just ran.
310 * The two lists are swapped so, be careful when using them before and after a
311 * call to this function.
313 void SIMIX_process_runall(void)
315 SIMIX_context_runall();
317 xbt_dynar_t tmp = simix_global->process_that_ran;
318 simix_global->process_that_ran = simix_global->process_to_run;
319 simix_global->process_to_run = tmp;
320 xbt_dynar_reset(simix_global->process_to_run);
323 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
324 SIMIX_process_kill(process, simcall->issuer);
327 * \brief Internal function to kill a SIMIX process.
329 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
330 * or directly for SIMIX internal purposes.
332 * \param process poor victim
333 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
335 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
337 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->host));
339 process->context->iwannadie = 1;
340 process->blocked = 0;
341 process->suspended = 0;
342 process->doexception = 0;
344 /* destroy the blocking synchro if any */
345 if (process->waiting_synchro) {
347 switch (process->waiting_synchro->type) {
349 case SIMIX_SYNC_EXECUTE:
350 case SIMIX_SYNC_PARALLEL_EXECUTE:
351 SIMIX_process_execution_destroy(process->waiting_synchro);
354 case SIMIX_SYNC_COMMUNICATE:
355 xbt_fifo_remove(process->comms, process->waiting_synchro);
356 SIMIX_comm_cancel(process->waiting_synchro);
357 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
358 SIMIX_comm_destroy(process->waiting_synchro);
361 case SIMIX_SYNC_SLEEP:
362 SIMIX_process_sleep_destroy(process->waiting_synchro);
365 case SIMIX_SYNC_JOIN:
366 SIMIX_process_sleep_destroy(process->waiting_synchro);
369 case SIMIX_SYNC_SYNCHRO:
370 SIMIX_synchro_stop_waiting(process, &process->simcall);
371 SIMIX_synchro_destroy(process->waiting_synchro);
375 SIMIX_io_destroy(process->waiting_synchro);
380 process->waiting_synchro = NULL;
382 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
383 XBT_DEBUG("Inserting %s in the to_run list", process->name);
384 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
389 /** @brief Ask another process to raise the given exception
391 * @param cat category of exception
392 * @param value value associated to the exception
393 * @param msg string information associated to the exception
395 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
396 SMX_EXCEPTION(process, cat, value, msg);
398 if (process->suspended)
399 SIMIX_process_resume(process,SIMIX_process_self());
401 /* cancel the blocking synchro if any */
402 if (process->waiting_synchro) {
404 switch (process->waiting_synchro->type) {
406 case SIMIX_SYNC_EXECUTE:
407 case SIMIX_SYNC_PARALLEL_EXECUTE:
408 SIMIX_process_execution_cancel(process->waiting_synchro);
411 case SIMIX_SYNC_COMMUNICATE:
412 xbt_fifo_remove(process->comms, process->waiting_synchro);
413 SIMIX_comm_cancel(process->waiting_synchro);
416 case SIMIX_SYNC_SLEEP:
417 case SIMIX_SYNC_JOIN:
418 SIMIX_process_sleep_destroy(process->waiting_synchro);
419 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
420 XBT_DEBUG("Inserting %s in the to_run list", process->name);
421 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
425 case SIMIX_SYNC_SYNCHRO:
426 SIMIX_synchro_stop_waiting(process, &process->simcall);
430 SIMIX_io_destroy(process->waiting_synchro);
435 process->waiting_synchro = NULL;
439 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
440 SIMIX_process_killall(simcall->issuer, reset_pid);
443 * \brief Kills all running processes.
444 * \param issuer this one will not be killed
446 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
448 smx_process_t p = NULL;
450 while ((p = xbt_swag_extract(simix_global->process_list))) {
452 SIMIX_process_kill(p,issuer);
457 simix_process_maxpid = reset_pid;
459 SIMIX_context_runall();
461 SIMIX_process_empty_trash();
464 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
466 process->new_host = dest;
468 void SIMIX_process_change_host(smx_process_t process,
471 xbt_assert((process != NULL), "Invalid parameters");
472 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
473 process->host = dest;
474 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
478 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
480 smx_synchro_t sync_suspend =
481 SIMIX_process_suspend(process, simcall->issuer);
483 if (process != simcall->issuer) {
484 SIMIX_simcall_answer(simcall);
486 xbt_fifo_push(sync_suspend->simcalls, simcall);
487 process->waiting_synchro = sync_suspend;
488 SIMIX_host_execution_suspend(process->waiting_synchro);
490 /* If we are suspending ourselves, then just do not finish the simcall now */
493 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
495 xbt_assert((process != NULL), "Invalid parameters");
497 if (process->suspended) {
498 XBT_DEBUG("Process '%s' is already suspended", process->name);
502 process->suspended = 1;
504 /* If we are suspending another process, and it is waiting on a sync,
505 suspend its synchronization. */
506 if (process != issuer) {
508 if (process->waiting_synchro) {
510 switch (process->waiting_synchro->type) {
512 case SIMIX_SYNC_EXECUTE:
513 case SIMIX_SYNC_PARALLEL_EXECUTE:
514 SIMIX_host_execution_suspend(process->waiting_synchro);
517 case SIMIX_SYNC_COMMUNICATE:
518 SIMIX_comm_suspend(process->waiting_synchro);
521 case SIMIX_SYNC_SLEEP:
522 SIMIX_process_sleep_suspend(process->waiting_synchro);
525 case SIMIX_SYNC_SYNCHRO:
526 /* Suspension is delayed to when the process is rescheduled. */
530 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
531 (int)process->waiting_synchro->type);
535 /* Suspension is delayed to when the process is rescheduled. */
539 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
540 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
544 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
545 SIMIX_process_resume(process, simcall->issuer);
548 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
550 XBT_IN("process = %p, issuer = %p", process, issuer);
552 if(process->context->iwannadie) {
553 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
557 if(!process->suspended) return;
558 process->suspended = 0;
560 /* If we are resuming another process, resume the synchronization it was waiting for
561 if any. Otherwise add it to the list of process to run in the next round. */
562 if (process != issuer) {
564 if (process->waiting_synchro) {
566 switch (process->waiting_synchro->type) {
568 case SIMIX_SYNC_EXECUTE:
569 case SIMIX_SYNC_PARALLEL_EXECUTE:
570 SIMIX_host_execution_resume(process->waiting_synchro);
573 case SIMIX_SYNC_COMMUNICATE:
574 SIMIX_comm_resume(process->waiting_synchro);
577 case SIMIX_SYNC_SLEEP:
578 SIMIX_process_sleep_resume(process->waiting_synchro);
581 case SIMIX_SYNC_SYNCHRO:
582 /* I cannot resume it now. This is delayed to when the process is rescheduled at
583 * the end of the synchro. */
587 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
588 (int)process->waiting_synchro->type);
591 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
596 int SIMIX_process_get_maxpid(void) {
597 return simix_process_maxpid;
600 int SIMIX_process_count(void)
602 return xbt_swag_size(simix_global->process_list);
605 int SIMIX_process_get_PID(smx_process_t self){
612 int SIMIX_process_get_PPID(smx_process_t self){
619 void* SIMIX_process_self_get_data(smx_process_t self)
621 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
626 return SIMIX_process_get_data(self);
629 void SIMIX_process_self_set_data(smx_process_t self, void *data)
631 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
633 SIMIX_process_set_data(self, data);
636 void* SIMIX_process_get_data(smx_process_t process)
638 return process->data;
641 void SIMIX_process_set_data(smx_process_t process, void *data)
643 process->data = data;
646 sg_host_t SIMIX_process_get_host(smx_process_t process)
648 return process->host;
651 /* needs to be public and without simcall because it is called
652 by exceptions and logging events */
653 const char* SIMIX_process_self_get_name(void) {
655 smx_process_t process = SIMIX_process_self();
656 if (process == NULL || process == simix_global->maestro_process)
659 return SIMIX_process_get_name(process);
662 const char* SIMIX_process_get_name(smx_process_t process)
664 return process->name;
667 smx_process_t SIMIX_process_get_by_name(const char* name)
671 xbt_swag_foreach(proc, simix_global->process_list)
673 if(!strcmp(name, proc->name))
679 int SIMIX_process_is_suspended(smx_process_t process)
681 return process->suspended;
684 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
686 return process->properties;
689 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
691 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
692 xbt_fifo_push(sync->simcalls, simcall);
693 simcall->issuer->waiting_synchro = sync;
696 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
697 if (sync->sleep.surf_sleep) {
698 surf_action_cancel(sync->sleep.surf_sleep);
700 smx_simcall_t simcall;
701 while ((simcall = xbt_fifo_shift(sync->simcalls))) {
702 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
703 simcall->issuer->waiting_synchro = NULL;
704 if (simcall->issuer->suspended) {
705 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
706 simcall->issuer->suspended = 0;
707 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
709 SIMIX_simcall_answer(simcall);
712 surf_action_unref(sync->sleep.surf_sleep);
713 sync->sleep.surf_sleep = NULL;
715 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
719 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
721 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
722 res->type = SIMIX_SYNC_JOIN;
723 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
727 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
729 if (MC_is_active() || MC_record_replay_is_active()) {
730 MC_process_clock_add(simcall->issuer, duration);
731 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
732 SIMIX_simcall_answer(simcall);
735 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
736 xbt_fifo_push(sync->simcalls, simcall);
737 simcall->issuer->waiting_synchro = sync;
740 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
742 smx_synchro_t synchro;
743 sg_host_t host = process->host;
745 /* check if the host is active */
746 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
747 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
751 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
752 synchro->type = SIMIX_SYNC_SLEEP;
753 synchro->name = NULL;
754 synchro->category = NULL;
756 synchro->sleep.host = host;
757 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
759 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
760 XBT_DEBUG("Create sleep synchronization %p", synchro);
765 void SIMIX_post_process_sleep(smx_synchro_t synchro)
767 smx_simcall_t simcall;
769 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
771 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
773 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
774 case SURF_ACTION_FAILED:
775 simcall->issuer->context->iwannadie = 1;
776 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
777 state = SIMIX_SRC_HOST_FAILURE;
780 case SURF_ACTION_DONE:
788 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
789 simcall->issuer->context->iwannadie = 1;
791 simcall_process_sleep__set__result(simcall, state);
792 simcall->issuer->waiting_synchro = NULL;
793 if (simcall->issuer->suspended) {
794 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
795 simcall->issuer->suspended = 0;
796 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
798 SIMIX_simcall_answer(simcall);
802 SIMIX_process_sleep_destroy(synchro);
805 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
807 XBT_DEBUG("Destroy synchro %p", synchro);
808 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
810 if (synchro->sleep.surf_sleep) {
811 surf_action_unref(synchro->sleep.surf_sleep);
812 synchro->sleep.surf_sleep = NULL;
814 if (synchro->type == SIMIX_SYNC_SLEEP)
815 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
818 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
820 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
821 surf_action_suspend(synchro->sleep.surf_sleep);
824 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
826 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
827 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
828 surf_action_resume(synchro->sleep.surf_sleep);
832 * \brief Calling this function makes the process to yield.
834 * Only the current process can call this function, giving back the control to
837 * \param self the current process
839 void SIMIX_process_yield(smx_process_t self)
841 XBT_DEBUG("Yield process '%s'", self->name);
843 /* Go into sleep and return control to maestro */
844 SIMIX_context_suspend(self->context);
846 /* Ok, maestro returned control to us */
847 XBT_DEBUG("Control returned to me: '%s'", self->name);
849 if (self->new_host) {
850 SIMIX_process_change_host(self, self->new_host);
851 self->new_host = NULL;
854 if (self->context->iwannadie){
855 XBT_DEBUG("I wanna die!");
856 SIMIX_process_stop(self);
859 if (self->suspended) {
860 XBT_DEBUG("Hey! I'm suspended.");
861 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
863 SIMIX_process_suspend(self, self);
866 if (self->doexception) {
867 XBT_DEBUG("Wait, maestro left me an exception");
868 self->doexception = 0;
874 /* callback: context fetching */
875 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
877 return SIMIX_process_self()->running_ctx;
880 /* callback: termination */
881 void SIMIX_process_exception_terminate(xbt_ex_t * e)
887 smx_context_t SIMIX_process_get_context(smx_process_t p) {
891 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
896 * \brief Returns the list of processes to run.
898 xbt_dynar_t SIMIX_process_get_runnable(void)
900 return simix_global->process_to_run;
904 * \brief Returns the process from PID.
906 smx_process_t SIMIX_process_from_PID(int PID)
909 xbt_swag_foreach(proc, simix_global->process_list)
917 /** @brief returns a dynar containg all currently existing processes */
918 xbt_dynar_t SIMIX_processes_as_dynar(void) {
920 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
921 xbt_swag_foreach(proc, simix_global->process_list) {
922 xbt_dynar_push(res,&proc);
928 void SIMIX_process_on_exit_runall(smx_process_t process) {
929 s_smx_process_exit_fun_t exit_fun;
930 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
931 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
932 while (!xbt_dynar_is_empty(process->on_exit)) {
933 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
934 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
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);
951 * \brief Sets the auto-restart status of the process.
952 * If set to 1, the process will be automatically restarted when its host
955 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
956 process->auto_restart = auto_restart;
959 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
960 return SIMIX_process_restart(process, simcall->issuer);
962 /** @brief Restart a process, starting it again from the beginning. */
963 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
964 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->host));
965 //retrieve the arguments of the old process
966 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
967 s_smx_process_arg_t arg;
968 arg.code = process->code;
969 arg.hostname = sg_host_name(process->host);
970 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
971 arg.argc = process->argc;
972 arg.data = process->data;
974 arg.argv = xbt_new(char*,process->argc + 1);
975 for (i = 0; i < arg.argc; i++) {
976 arg.argv[i] = xbt_strdup(process->argv[i]);
978 arg.argv[process->argc] = NULL;
979 arg.properties = NULL;
980 arg.auto_restart = process->auto_restart;
981 //kill the old process
982 SIMIX_process_kill(process,issuer);
983 //start the new process
984 smx_process_t new_process;
985 if (simix_global->create_process_function) {
986 new_process = simix_global->create_process_function(
998 new_process = simcall_process_create(