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"
12 #include "mc/mc_replay.h"
13 #include "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;
186 simix_global->create_process_function(&process,
202 void simcall_HANDLER_process_create(smx_simcall_t simcall,
203 smx_process_t *process,
205 xbt_main_func_t code,
207 const char *hostname,
209 int argc, char **argv,
210 xbt_dict_t properties,
212 SIMIX_process_create(process, name, code, data, hostname,
213 kill_time, argc, argv, properties, auto_restart,
217 * \brief Internal function to create a process.
219 * This function actually creates the process.
220 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
221 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
223 * \return the process created
225 void SIMIX_process_create(smx_process_t *process,
227 xbt_main_func_t code,
229 const char *hostname,
231 int argc, char **argv,
232 xbt_dict_t properties,
234 smx_process_t parent_process)
237 sg_host_t host = sg_host_by_name(hostname);
239 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
241 if (!SIMIX_host_get_state(host)) {
243 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
245 for (i = 0; i < argc; i++)
250 *process = xbt_new0(s_smx_process_t, 1);
252 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
254 (*process)->pid = simix_process_maxpid++;
255 (*process)->name = xbt_strdup(name);
256 (*process)->host = host;
257 (*process)->data = data;
258 (*process)->comms = xbt_fifo_new();
259 (*process)->simcall.issuer = *process;
261 if (parent_process) {
262 (*process)->ppid = SIMIX_process_get_PID(parent_process);
264 (*process)->ppid = -1;
267 /* Process data for auto-restart */
268 (*process)->auto_restart = auto_restart;
269 (*process)->code = code;
270 (*process)->argc = argc;
271 (*process)->argv = argv;
274 XBT_VERB("Create context %s", (*process)->name);
275 (*process)->context = SIMIX_context_new(code, argc, argv,
276 simix_global->cleanup_process_function, *process);
278 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
279 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
282 MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
286 (*process)->properties = properties;
288 /* Add the process to it's host process list */
289 xbt_swag_insert(*process, sg_host_simix(host)->process_list);
291 XBT_DEBUG("Start context '%s'", (*process)->name);
293 /* Now insert it in the global process list and in the process to run list */
294 xbt_swag_insert(*process, simix_global->process_list);
295 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
296 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
298 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
299 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
300 sg_host_name((*process)->host), kill_time);
301 (*process)->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
307 * \brief Executes the processes from simix_global->process_to_run.
309 * The processes of simix_global->process_to_run are run (in parallel if
310 * possible). On exit, simix_global->process_to_run is empty, and
311 * simix_global->process_that_ran contains the list of processes that just ran.
312 * The two lists are swapped so, be careful when using them before and after a
313 * call to this function.
315 void SIMIX_process_runall(void)
317 SIMIX_context_runall();
319 xbt_dynar_t tmp = simix_global->process_that_ran;
320 simix_global->process_that_ran = simix_global->process_to_run;
321 simix_global->process_to_run = tmp;
322 xbt_dynar_reset(simix_global->process_to_run);
325 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
326 SIMIX_process_kill(process, simcall->issuer);
329 * \brief Internal function to kill a SIMIX process.
331 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
332 * or directly for SIMIX internal purposes.
334 * \param process poor victim
335 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
337 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
339 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->host));
341 process->context->iwannadie = 1;
342 process->blocked = 0;
343 process->suspended = 0;
344 process->doexception = 0;
346 /* destroy the blocking synchro if any */
347 if (process->waiting_synchro) {
349 switch (process->waiting_synchro->type) {
351 case SIMIX_SYNC_EXECUTE:
352 case SIMIX_SYNC_PARALLEL_EXECUTE:
353 SIMIX_host_execution_destroy(process->waiting_synchro);
356 case SIMIX_SYNC_COMMUNICATE:
357 xbt_fifo_remove(process->comms, process->waiting_synchro);
358 SIMIX_comm_cancel(process->waiting_synchro);
359 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
360 SIMIX_comm_destroy(process->waiting_synchro);
363 case SIMIX_SYNC_SLEEP:
364 SIMIX_process_sleep_destroy(process->waiting_synchro);
367 case SIMIX_SYNC_JOIN:
368 SIMIX_process_sleep_destroy(process->waiting_synchro);
371 case SIMIX_SYNC_SYNCHRO:
372 SIMIX_synchro_stop_waiting(process, &process->simcall);
373 SIMIX_synchro_destroy(process->waiting_synchro);
377 SIMIX_io_destroy(process->waiting_synchro);
382 process->waiting_synchro = NULL;
384 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
385 XBT_DEBUG("Inserting %s in the to_run list", process->name);
386 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
391 /** @brief Ask another process to raise the given exception
393 * @param cat category of exception
394 * @param value value associated to the exception
395 * @param msg string information associated to the exception
397 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
398 SMX_EXCEPTION(process, cat, value, msg);
400 if (process->suspended)
401 SIMIX_process_resume(process,SIMIX_process_self());
403 /* cancel the blocking synchro if any */
404 if (process->waiting_synchro) {
406 switch (process->waiting_synchro->type) {
408 case SIMIX_SYNC_EXECUTE:
409 case SIMIX_SYNC_PARALLEL_EXECUTE:
410 SIMIX_host_execution_cancel(process->waiting_synchro);
413 case SIMIX_SYNC_COMMUNICATE:
414 xbt_fifo_remove(process->comms, process->waiting_synchro);
415 SIMIX_comm_cancel(process->waiting_synchro);
418 case SIMIX_SYNC_SLEEP:
419 case SIMIX_SYNC_JOIN:
420 SIMIX_process_sleep_destroy(process->waiting_synchro);
421 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
422 XBT_DEBUG("Inserting %s in the to_run list", process->name);
423 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
427 case SIMIX_SYNC_SYNCHRO:
428 SIMIX_synchro_stop_waiting(process, &process->simcall);
432 SIMIX_io_destroy(process->waiting_synchro);
437 process->waiting_synchro = NULL;
441 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
442 SIMIX_process_killall(simcall->issuer, reset_pid);
445 * \brief Kills all running processes.
446 * \param issuer this one will not be killed
448 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
450 smx_process_t p = NULL;
452 while ((p = xbt_swag_extract(simix_global->process_list))) {
454 SIMIX_process_kill(p,issuer);
459 simix_process_maxpid = reset_pid;
461 SIMIX_context_runall();
463 SIMIX_process_empty_trash();
466 void simcall_HANDLER_process_change_host(smx_simcall_t simcall, smx_process_t process,
469 process->new_host = dest;
471 void SIMIX_process_change_host(smx_process_t process,
474 xbt_assert((process != NULL), "Invalid parameters");
475 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
476 process->host = dest;
477 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
481 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
483 smx_synchro_t sync_suspend =
484 SIMIX_process_suspend(process, simcall->issuer);
486 if (process != simcall->issuer) {
487 SIMIX_simcall_answer(simcall);
489 xbt_fifo_push(sync_suspend->simcalls, simcall);
490 process->waiting_synchro = sync_suspend;
491 SIMIX_host_execution_suspend(process->waiting_synchro);
493 /* If we are suspending ourselves, then just do not finish the simcall now */
496 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
498 xbt_assert((process != NULL), "Invalid parameters");
500 if (process->suspended) {
501 XBT_DEBUG("Process '%s' is already suspended", process->name);
505 process->suspended = 1;
507 /* If we are suspending another process, and it is waiting on a sync,
508 suspend its synchronization. */
509 if (process != issuer) {
511 if (process->waiting_synchro) {
513 switch (process->waiting_synchro->type) {
515 case SIMIX_SYNC_EXECUTE:
516 case SIMIX_SYNC_PARALLEL_EXECUTE:
517 SIMIX_host_execution_suspend(process->waiting_synchro);
520 case SIMIX_SYNC_COMMUNICATE:
521 SIMIX_comm_suspend(process->waiting_synchro);
524 case SIMIX_SYNC_SLEEP:
525 SIMIX_process_sleep_suspend(process->waiting_synchro);
528 case SIMIX_SYNC_SYNCHRO:
529 /* Suspension is delayed to when the process is rescheduled. */
533 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
534 (int)process->waiting_synchro->type);
538 /* Suspension is delayed to when the process is rescheduled. */
542 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
543 return SIMIX_host_execute("suspend", process->host, 0.0, 1.0, 0.0, 0);
547 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
548 SIMIX_process_resume(process, simcall->issuer);
551 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
553 XBT_IN("process = %p, issuer = %p", process, issuer);
555 if(process->context->iwannadie) {
556 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
560 if(!process->suspended) return;
561 process->suspended = 0;
563 /* If we are resuming another process, resume the synchronization it was waiting for
564 if any. Otherwise add it to the list of process to run in the next round. */
565 if (process != issuer) {
567 if (process->waiting_synchro) {
569 switch (process->waiting_synchro->type) {
571 case SIMIX_SYNC_EXECUTE:
572 case SIMIX_SYNC_PARALLEL_EXECUTE:
573 SIMIX_host_execution_resume(process->waiting_synchro);
576 case SIMIX_SYNC_COMMUNICATE:
577 SIMIX_comm_resume(process->waiting_synchro);
580 case SIMIX_SYNC_SLEEP:
581 SIMIX_process_sleep_resume(process->waiting_synchro);
584 case SIMIX_SYNC_SYNCHRO:
585 /* I cannot resume it now. This is delayed to when the process is rescheduled at
586 * the end of the synchro. */
590 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
591 (int)process->waiting_synchro->type);
594 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
599 int SIMIX_process_get_maxpid(void) {
600 return simix_process_maxpid;
603 int SIMIX_process_count(void)
605 return xbt_swag_size(simix_global->process_list);
608 int SIMIX_process_get_PID(smx_process_t self){
615 int SIMIX_process_get_PPID(smx_process_t self){
622 void* SIMIX_process_self_get_data(smx_process_t self)
624 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
629 return SIMIX_process_get_data(self);
632 void SIMIX_process_self_set_data(smx_process_t self, void *data)
634 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
636 SIMIX_process_set_data(self, data);
639 void* SIMIX_process_get_data(smx_process_t process)
641 return process->data;
644 void SIMIX_process_set_data(smx_process_t process, void *data)
646 process->data = data;
649 sg_host_t SIMIX_process_get_host(smx_process_t process)
651 return process->host;
654 /* needs to be public and without simcall because it is called
655 by exceptions and logging events */
656 const char* SIMIX_process_self_get_name(void) {
658 smx_process_t process = SIMIX_process_self();
659 if (process == NULL || process == simix_global->maestro_process)
662 return SIMIX_process_get_name(process);
665 const char* SIMIX_process_get_name(smx_process_t process)
667 return process->name;
670 smx_process_t SIMIX_process_get_by_name(const char* name)
674 xbt_swag_foreach(proc, simix_global->process_list)
676 if(!strcmp(name, proc->name))
682 int SIMIX_process_is_suspended(smx_process_t process)
684 return process->suspended;
687 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
689 return process->properties;
692 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
694 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
695 xbt_fifo_push(sync->simcalls, simcall);
696 simcall->issuer->waiting_synchro = sync;
699 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
700 if (sync->sleep.surf_sleep) {
701 surf_action_cancel(sync->sleep.surf_sleep);
703 smx_simcall_t simcall;
704 while ((simcall = xbt_fifo_shift(sync->simcalls))) {
705 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
706 simcall->issuer->waiting_synchro = NULL;
707 if (simcall->issuer->suspended) {
708 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
709 simcall->issuer->suspended = 0;
710 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
712 SIMIX_simcall_answer(simcall);
715 surf_action_unref(sync->sleep.surf_sleep);
716 sync->sleep.surf_sleep = NULL;
718 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
722 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
724 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
725 res->type = SIMIX_SYNC_JOIN;
726 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
730 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
732 if (MC_is_active() || MC_record_replay_is_active()) {
733 MC_process_clock_add(simcall->issuer, duration);
734 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
735 SIMIX_simcall_answer(simcall);
738 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
739 xbt_fifo_push(sync->simcalls, simcall);
740 simcall->issuer->waiting_synchro = sync;
743 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
745 smx_synchro_t synchro;
746 sg_host_t host = process->host;
748 /* check if the host is active */
749 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
750 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
754 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
755 synchro->type = SIMIX_SYNC_SLEEP;
756 synchro->name = NULL;
757 synchro->category = NULL;
759 synchro->sleep.host = host;
760 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
762 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
763 XBT_DEBUG("Create sleep synchronization %p", synchro);
768 void SIMIX_post_process_sleep(smx_synchro_t synchro)
770 smx_simcall_t simcall;
772 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
774 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
776 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
777 case SURF_ACTION_FAILED:
778 simcall->issuer->context->iwannadie = 1;
779 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
780 state = SIMIX_SRC_HOST_FAILURE;
783 case SURF_ACTION_DONE:
791 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
792 simcall->issuer->context->iwannadie = 1;
794 simcall_process_sleep__set__result(simcall, state);
795 simcall->issuer->waiting_synchro = NULL;
796 if (simcall->issuer->suspended) {
797 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
798 simcall->issuer->suspended = 0;
799 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
801 SIMIX_simcall_answer(simcall);
805 SIMIX_process_sleep_destroy(synchro);
808 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
810 XBT_DEBUG("Destroy synchro %p", synchro);
811 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
813 if (synchro->sleep.surf_sleep) {
814 surf_action_unref(synchro->sleep.surf_sleep);
815 synchro->sleep.surf_sleep = NULL;
817 if (synchro->type == SIMIX_SYNC_SLEEP)
818 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
821 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
823 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
824 surf_action_suspend(synchro->sleep.surf_sleep);
827 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
829 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
830 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
831 surf_action_resume(synchro->sleep.surf_sleep);
835 * \brief Calling this function makes the process to yield.
837 * Only the current process can call this function, giving back the control to
840 * \param self the current process
842 void SIMIX_process_yield(smx_process_t self)
844 XBT_DEBUG("Yield process '%s'", self->name);
846 /* Go into sleep and return control to maestro */
847 SIMIX_context_suspend(self->context);
849 /* Ok, maestro returned control to us */
850 XBT_DEBUG("Control returned to me: '%s'", self->name);
852 if (self->new_host) {
853 SIMIX_process_change_host(self, self->new_host);
854 self->new_host = NULL;
857 if (self->context->iwannadie){
858 XBT_DEBUG("I wanna die!");
859 SIMIX_process_stop(self);
862 if (self->suspended) {
863 XBT_DEBUG("Hey! I'm suspended.");
864 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
866 SIMIX_process_suspend(self, self);
869 if (self->doexception) {
870 XBT_DEBUG("Wait, maestro left me an exception");
871 self->doexception = 0;
877 /* callback: context fetching */
878 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
880 return SIMIX_process_self()->running_ctx;
883 /* callback: termination */
884 void SIMIX_process_exception_terminate(xbt_ex_t * e)
890 smx_context_t SIMIX_process_get_context(smx_process_t p) {
894 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
899 * \brief Returns the list of processes to run.
901 xbt_dynar_t SIMIX_process_get_runnable(void)
903 return simix_global->process_to_run;
907 * \brief Returns the process from PID.
909 smx_process_t SIMIX_process_from_PID(int PID)
912 xbt_swag_foreach(proc, simix_global->process_list)
920 /** @brief returns a dynar containg all currently existing processes */
921 xbt_dynar_t SIMIX_processes_as_dynar(void) {
923 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
924 xbt_swag_foreach(proc, simix_global->process_list) {
925 xbt_dynar_push(res,&proc);
931 void SIMIX_process_on_exit_runall(smx_process_t process) {
932 s_smx_process_exit_fun_t exit_fun;
933 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
934 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
935 while (!xbt_dynar_is_empty(process->on_exit)) {
936 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
937 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
941 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
942 xbt_assert(process, "current process not found: are you in maestro context ?");
944 if (!process->on_exit) {
945 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
948 s_smx_process_exit_fun_t exit_fun = {fun, data};
950 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
954 * \brief Sets the auto-restart status of the process.
955 * If set to 1, the process will be automatically restarted when its host
958 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
959 process->auto_restart = auto_restart;
962 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
963 return SIMIX_process_restart(process, simcall->issuer);
965 /** @brief Restart a process, starting it again from the beginning. */
966 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
967 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->host));
968 //retrieve the arguments of the old process
969 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
970 s_smx_process_arg_t arg;
971 arg.code = process->code;
972 arg.hostname = sg_host_name(process->host);
973 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
974 arg.argc = process->argc;
975 arg.data = process->data;
977 arg.argv = xbt_new(char*,process->argc + 1);
978 for (i = 0; i < arg.argc; i++) {
979 arg.argv[i] = xbt_strdup(process->argv[i]);
981 arg.argv[process->argc] = NULL;
982 arg.properties = NULL;
983 arg.auto_restart = process->auto_restart;
984 //kill the old process
985 SIMIX_process_kill(process,issuer);
986 //start the new process
987 smx_process_t new_process;
988 if (simix_global->create_process_function) {
989 simix_global->create_process_function(&new_process,
1001 simcall_process_create(&new_process,