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;
42 * \brief Moves a process to the list of processes to destroy.
44 void SIMIX_process_cleanup(smx_process_t process)
46 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
47 process->name, process, process->waiting_synchro);
49 SIMIX_process_on_exit_runall(process);
51 /* cancel non-blocking communications */
52 smx_synchro_t synchro;
53 while ((synchro = xbt_fifo_pop(process->comms))) {
55 /* make sure no one will finish the comm after this process is destroyed,
56 * because src_proc or dst_proc would be an invalid pointer */
57 SIMIX_comm_cancel(synchro);
59 if (synchro->comm.src_proc == process) {
60 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
61 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
62 synchro->comm.src_proc = NULL;
64 /* I'm not supposed to destroy a detached comm from the sender side, */
65 if (!synchro->comm.detached)
66 SIMIX_comm_destroy(synchro);
68 XBT_DEBUG("Don't destroy it since it's a detached comm");
71 else if (synchro->comm.dst_proc == process){
72 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
73 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
74 synchro->comm.dst_proc = NULL;
76 if (synchro->comm.detached && synchro->comm.refcount == 1
77 && synchro->comm.src_proc != NULL) {
78 /* the comm will be freed right now, remove it from the sender */
79 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
81 SIMIX_comm_destroy(synchro);
84 xbt_die("Communication synchro %p is in my list but I'm not the sender "
85 "or the receiver", synchro);
89 XBT_DEBUG("%p should not be run anymore",process);
90 xbt_swag_remove(process, simix_global->process_list);
91 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
92 xbt_swag_insert(process, simix_global->process_to_destroy);
93 process->context->iwannadie = 0;
99 * Should be called some time to time to free the memory allocated for processes
100 * that have finished (or killed).
102 void SIMIX_process_empty_trash(void)
104 smx_process_t process = NULL;
106 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
107 XBT_DEBUG("Getting rid of %p",process);
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 simcall_HANDLER_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 simcall_HANDLER_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 process->doexception = 0;
337 /* destroy the blocking synchro if any */
338 if (process->waiting_synchro) {
340 switch (process->waiting_synchro->type) {
342 case SIMIX_SYNC_EXECUTE:
343 case SIMIX_SYNC_PARALLEL_EXECUTE:
344 SIMIX_host_execution_destroy(process->waiting_synchro);
347 case SIMIX_SYNC_COMMUNICATE:
348 xbt_fifo_remove(process->comms, process->waiting_synchro);
349 SIMIX_comm_cancel(process->waiting_synchro);
350 SIMIX_comm_destroy(process->waiting_synchro);
353 case SIMIX_SYNC_SLEEP:
354 SIMIX_process_sleep_destroy(process->waiting_synchro);
357 case SIMIX_SYNC_JOIN:
358 SIMIX_process_sleep_destroy(process->waiting_synchro);
361 case SIMIX_SYNC_SYNCHRO:
362 SIMIX_synchro_stop_waiting(process, &process->simcall);
363 SIMIX_synchro_destroy(process->waiting_synchro);
367 SIMIX_io_destroy(process->waiting_synchro);
372 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
373 XBT_DEBUG("Inserting %s in the to_run list", process->name);
374 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
379 /** @brief Ask another process to raise the given exception
381 * @param cat category of exception
382 * @param value value associated to the exception
383 * @param msg string information associated to the exception
385 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
386 SMX_EXCEPTION(process, cat, value, msg);
388 if (process->suspended)
389 SIMIX_process_resume(process,SIMIX_process_self());
391 /* cancel the blocking synchro if any */
392 if (process->waiting_synchro) {
394 switch (process->waiting_synchro->type) {
396 case SIMIX_SYNC_EXECUTE:
397 case SIMIX_SYNC_PARALLEL_EXECUTE:
398 SIMIX_host_execution_cancel(process->waiting_synchro);
401 case SIMIX_SYNC_COMMUNICATE:
402 xbt_fifo_remove(process->comms, process->waiting_synchro);
403 SIMIX_comm_cancel(process->waiting_synchro);
406 case SIMIX_SYNC_SLEEP:
407 case SIMIX_SYNC_JOIN:
408 SIMIX_process_sleep_destroy(process->waiting_synchro);
409 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
410 XBT_DEBUG("Inserting %s in the to_run list", process->name);
411 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
415 case SIMIX_SYNC_SYNCHRO:
416 SIMIX_synchro_stop_waiting(process, &process->simcall);
420 SIMIX_io_destroy(process->waiting_synchro);
425 process->waiting_synchro = NULL;
429 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
430 SIMIX_process_killall(simcall->issuer, reset_pid);
433 * \brief Kills all running processes.
434 * \param issuer this one will not be killed
436 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
438 smx_process_t p = NULL;
440 while ((p = xbt_swag_extract(simix_global->process_list))) {
442 SIMIX_process_kill(p,issuer);
447 simix_process_maxpid = reset_pid;
449 SIMIX_context_runall();
451 SIMIX_process_empty_trash();
454 void simcall_HANDLER_process_change_host(smx_simcall_t simcall, smx_process_t process,
457 process->new_host = dest;
459 void SIMIX_process_change_host(smx_process_t process,
462 xbt_assert((process != NULL), "Invalid parameters");
463 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
464 process->smx_host = dest;
465 xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
469 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
471 smx_synchro_t sync_suspend =
472 SIMIX_process_suspend(process, simcall->issuer);
474 if (process != simcall->issuer) {
475 SIMIX_simcall_answer(simcall);
477 xbt_fifo_push(sync_suspend->simcalls, simcall);
478 process->waiting_synchro = sync_suspend;
479 SIMIX_host_execution_suspend(process->waiting_synchro);
481 /* If we are suspending ourselves, then just do not finish the simcall now */
484 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
486 xbt_assert((process != NULL), "Invalid parameters");
488 if (process->suspended) {
489 XBT_DEBUG("Process '%s' is already suspended", process->name);
493 process->suspended = 1;
495 /* If we are suspending another process, and it is waiting on a sync,
496 suspend its synchronization. */
497 if (process != issuer) {
499 if (process->waiting_synchro) {
501 switch (process->waiting_synchro->type) {
503 case SIMIX_SYNC_EXECUTE:
504 case SIMIX_SYNC_PARALLEL_EXECUTE:
505 SIMIX_host_execution_suspend(process->waiting_synchro);
508 case SIMIX_SYNC_COMMUNICATE:
509 SIMIX_comm_suspend(process->waiting_synchro);
512 case SIMIX_SYNC_SLEEP:
513 SIMIX_process_sleep_suspend(process->waiting_synchro);
516 case SIMIX_SYNC_SYNCHRO:
517 /* Suspension is delayed to when the process is rescheduled. */
521 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
522 (int)process->waiting_synchro->type);
526 /* Suspension is delayed to when the process is rescheduled. */
530 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
531 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
535 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
536 SIMIX_process_resume(process, simcall->issuer);
539 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
541 XBT_IN("process = %p, issuer = %p", process, issuer);
543 if(process->context->iwannadie) {
544 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
548 if(!process->suspended) return;
549 process->suspended = 0;
551 /* If we are resuming another process, resume the synchronization it was waiting for
552 if any. Otherwise add it to the list of process to run in the next round. */
553 if (process != issuer) {
555 if (process->waiting_synchro) {
557 switch (process->waiting_synchro->type) {
559 case SIMIX_SYNC_EXECUTE:
560 case SIMIX_SYNC_PARALLEL_EXECUTE:
561 SIMIX_host_execution_resume(process->waiting_synchro);
564 case SIMIX_SYNC_COMMUNICATE:
565 SIMIX_comm_resume(process->waiting_synchro);
568 case SIMIX_SYNC_SLEEP:
569 SIMIX_process_sleep_resume(process->waiting_synchro);
572 case SIMIX_SYNC_SYNCHRO:
573 /* I cannot resume it now. This is delayed to when the process is rescheduled at
574 * the end of the synchro. */
578 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
579 (int)process->waiting_synchro->type);
582 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
587 int SIMIX_process_get_maxpid(void) {
588 return simix_process_maxpid;
591 int SIMIX_process_count(void)
593 return xbt_swag_size(simix_global->process_list);
596 int SIMIX_process_get_PID(smx_process_t self){
603 int SIMIX_process_get_PPID(smx_process_t self){
610 void* SIMIX_process_self_get_data(smx_process_t self)
612 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
617 return SIMIX_process_get_data(self);
620 void SIMIX_process_self_set_data(smx_process_t self, void *data)
622 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
624 SIMIX_process_set_data(self, data);
627 void* SIMIX_process_get_data(smx_process_t process)
629 return process->data;
632 void SIMIX_process_set_data(smx_process_t process, void *data)
634 process->data = data;
637 smx_host_t SIMIX_process_get_host(smx_process_t process)
639 return process->smx_host;
642 /* needs to be public and without simcall because it is called
643 by exceptions and logging events */
644 const char* SIMIX_process_self_get_name(void) {
646 smx_process_t process = SIMIX_process_self();
647 if (process == NULL || process == simix_global->maestro_process)
650 return SIMIX_process_get_name(process);
653 const char* SIMIX_process_get_name(smx_process_t process)
655 return process->name;
658 smx_process_t SIMIX_process_get_by_name(const char* name)
662 xbt_swag_foreach(proc, simix_global->process_list)
664 if(!strcmp(name, proc->name))
670 int SIMIX_process_is_suspended(smx_process_t process)
672 return process->suspended;
675 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
677 return process->properties;
680 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
682 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
683 xbt_fifo_push(sync->simcalls, simcall);
684 simcall->issuer->waiting_synchro = sync;
687 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
688 if (sync->sleep.surf_sleep) {
689 surf_action_cancel(sync->sleep.surf_sleep);
691 smx_simcall_t simcall;
692 while ((simcall = xbt_fifo_shift(sync->simcalls))) {
693 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
694 simcall->issuer->waiting_synchro = NULL;
695 if (simcall->issuer->suspended) {
696 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
697 simcall->issuer->suspended = 0;
698 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
700 SIMIX_simcall_answer(simcall);
703 surf_action_unref(sync->sleep.surf_sleep);
704 sync->sleep.surf_sleep = NULL;
706 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
710 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
712 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
713 res->type = SIMIX_SYNC_JOIN;
714 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
718 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
720 if (MC_is_active() || MC_record_replay_is_active()) {
721 MC_process_clock_add(simcall->issuer, duration);
722 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
723 SIMIX_simcall_answer(simcall);
726 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
727 xbt_fifo_push(sync->simcalls, simcall);
728 simcall->issuer->waiting_synchro = sync;
731 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
733 smx_synchro_t synchro;
734 smx_host_t host = process->smx_host;
736 /* check if the host is active */
737 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
738 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
742 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
743 synchro->type = SIMIX_SYNC_SLEEP;
744 synchro->name = NULL;
745 synchro->category = NULL;
747 synchro->sleep.host = host;
748 synchro->sleep.surf_sleep =
749 surf_workstation_sleep(host, duration);
751 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
752 XBT_DEBUG("Create sleep synchronization %p", synchro);
757 void SIMIX_post_process_sleep(smx_synchro_t synchro)
759 smx_simcall_t simcall;
761 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
763 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
765 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
766 case SURF_ACTION_FAILED:
767 simcall->issuer->context->iwannadie = 1;
768 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
769 state = SIMIX_SRC_HOST_FAILURE;
772 case SURF_ACTION_DONE:
780 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
781 simcall->issuer->context->iwannadie = 1;
783 simcall_process_sleep__set__result(simcall, state);
784 simcall->issuer->waiting_synchro = NULL;
785 if (simcall->issuer->suspended) {
786 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
787 simcall->issuer->suspended = 0;
788 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
790 SIMIX_simcall_answer(simcall);
794 SIMIX_process_sleep_destroy(synchro);
797 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
799 XBT_DEBUG("Destroy synchro %p", synchro);
800 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
802 if (synchro->sleep.surf_sleep) {
803 surf_action_unref(synchro->sleep.surf_sleep);
804 synchro->sleep.surf_sleep = NULL;
806 if (synchro->type == SIMIX_SYNC_SLEEP)
807 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
810 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
812 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
813 surf_action_suspend(synchro->sleep.surf_sleep);
816 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
818 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
819 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
820 surf_action_resume(synchro->sleep.surf_sleep);
824 * \brief Calling this function makes the process to yield.
826 * Only the current process can call this function, giving back the control to
829 * \param self the current process
831 void SIMIX_process_yield(smx_process_t self)
833 XBT_DEBUG("Yield process '%s'", self->name);
835 /* Go into sleep and return control to maestro */
836 SIMIX_context_suspend(self->context);
838 /* Ok, maestro returned control to us */
839 XBT_DEBUG("Control returned to me: '%s'", self->name);
841 if (self->new_host) {
842 SIMIX_process_change_host(self, self->new_host);
843 self->new_host = NULL;
846 if (self->context->iwannadie){
847 XBT_DEBUG("I wanna die!");
848 SIMIX_process_stop(self);
851 if (self->suspended) {
852 XBT_DEBUG("Hey! I'm suspended.");
853 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
855 SIMIX_process_suspend(self, self);
858 if (self->doexception) {
859 XBT_DEBUG("Wait, maestro left me an exception");
860 self->doexception = 0;
866 /* callback: context fetching */
867 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
869 return SIMIX_process_self()->running_ctx;
872 /* callback: termination */
873 void SIMIX_process_exception_terminate(xbt_ex_t * e)
879 smx_context_t SIMIX_process_get_context(smx_process_t p) {
883 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
888 * \brief Returns the list of processes to run.
890 xbt_dynar_t SIMIX_process_get_runnable(void)
892 return simix_global->process_to_run;
896 * \brief Returns the process from PID.
898 smx_process_t SIMIX_process_from_PID(int PID)
901 xbt_swag_foreach(proc, simix_global->process_list)
909 /** @brief returns a dynar containg all currently existing processes */
910 xbt_dynar_t SIMIX_processes_as_dynar(void) {
912 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
913 xbt_swag_foreach(proc, simix_global->process_list) {
914 xbt_dynar_push(res,&proc);
920 void SIMIX_process_on_exit_runall(smx_process_t process) {
921 s_smx_process_exit_fun_t exit_fun;
922 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
923 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
924 while (!xbt_dynar_is_empty(process->on_exit)) {
925 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
926 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
930 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
931 xbt_assert(process, "current process not found: are you in maestro context ?");
933 if (!process->on_exit) {
934 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
937 s_smx_process_exit_fun_t exit_fun = {fun, data};
939 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
943 * \brief Sets the auto-restart status of the process.
944 * If set to 1, the process will be automatically restarted when its host
947 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
948 process->auto_restart = auto_restart;
951 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
952 return SIMIX_process_restart(process, simcall->issuer);
955 * \brief Restart a process.
956 * Restart a process, starting it again from the beginning.
958 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
959 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
960 //retrieve the arguments of the old process
961 //FIXME: Factorise this with SIMIX_host_add_auto_restart_process ?
962 s_smx_process_arg_t arg;
963 arg.code = process->code;
964 arg.hostname = sg_host_name(process->smx_host);
965 arg.kill_time = process->kill_time;
966 arg.argc = process->argc;
967 arg.data = process->data;
969 arg.argv = xbt_new(char*,process->argc + 1);
970 for (i = 0; i < arg.argc; i++) {
971 arg.argv[i] = xbt_strdup(process->argv[i]);
973 arg.argv[process->argc] = NULL;
974 arg.properties = NULL;
975 arg.auto_restart = process->auto_restart;
976 //kill the old process
977 SIMIX_process_kill(process,issuer);
978 //start the new process
979 smx_process_t new_process;
980 if (simix_global->create_process_function) {
981 simix_global->create_process_function(&new_process,
993 simcall_process_create(&new_process,