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 /* cancel non-blocking communications */
58 smx_synchro_t synchro;
59 while ((synchro = xbt_fifo_pop(process->comms))) {
61 /* make sure no one will finish the comm after this process is destroyed,
62 * because src_proc or dst_proc would be an invalid pointer */
63 SIMIX_comm_cancel(synchro);
65 if (synchro->comm.src_proc == process) {
66 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
67 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
68 synchro->comm.src_proc = NULL;
70 /* I'm not supposed to destroy a detached comm from the sender side, */
71 if (!synchro->comm.detached)
72 SIMIX_comm_destroy(synchro);
74 XBT_DEBUG("Don't destroy it since it's a detached comm");
77 else if (synchro->comm.dst_proc == process){
78 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
79 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
80 synchro->comm.dst_proc = NULL;
82 if (synchro->comm.detached && synchro->comm.refcount == 1
83 && synchro->comm.src_proc != NULL) {
84 /* the comm will be freed right now, remove it from the sender */
85 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
87 SIMIX_comm_destroy(synchro);
90 xbt_die("Communication synchro %p is in my list but I'm not the sender "
91 "or the receiver", synchro);
95 XBT_DEBUG("%p should not be run anymore",process);
96 xbt_swag_remove(process, simix_global->process_list);
97 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
98 xbt_swag_insert(process, simix_global->process_to_destroy);
99 process->context->iwannadie = 0;
105 * Should be called some time to time to free the memory allocated for processes
106 * that have finished (or killed).
108 void SIMIX_process_empty_trash(void)
110 smx_process_t process = NULL;
112 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
113 XBT_DEBUG("Getting rid of %p",process);
115 SIMIX_context_free(process->context);
117 /* Free the exception allocated at creation time */
118 free(process->running_ctx);
119 xbt_dict_free(&process->properties);
121 xbt_fifo_free(process->comms);
123 xbt_dynar_free(&process->on_exit);
125 xbt_free(process->name);
131 * \brief Creates and runs the maestro process
133 void SIMIX_create_maestro_process()
135 smx_process_t maestro = NULL;
137 /* Create maestro process and intilialize it */
138 maestro = xbt_new0(s_smx_process_t, 1);
139 maestro->pid = simix_process_maxpid++;
141 maestro->name = (char *) "";
142 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
143 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
144 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
145 maestro->simcall.issuer = maestro;
146 simix_global->maestro_process = maestro;
150 * \brief Stops a process.
152 * Stops the process, execute all the registered on_exit functions,
153 * register it to the list of the process to restart if needed
154 * and stops its context.
156 void SIMIX_process_stop(smx_process_t arg) {
157 /* execute the on_exit functions */
158 SIMIX_process_on_exit_runall(arg);
159 /* Add the process to the list of process to restart, only if
162 if (arg->auto_restart && !SIMIX_host_get_state(arg->smx_host)) {
163 SIMIX_host_add_auto_restart_process(arg->smx_host,arg->name,arg->code, arg->data,
164 sg_host_name(arg->smx_host),
165 SIMIX_timer_get_date(arg->kill_timer),
166 arg->argc,arg->argv,arg->properties,
169 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->smx_host));
170 /* stop the context */
171 SIMIX_context_stop(arg->context);
175 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
176 * This function frees the argument.
177 * \return the process created
179 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
181 smx_process_t process;
182 simix_global->create_process_function(&process,
198 void simcall_HANDLER_process_create(smx_simcall_t simcall,
199 smx_process_t *process,
201 xbt_main_func_t code,
203 const char *hostname,
205 int argc, char **argv,
206 xbt_dict_t properties,
208 SIMIX_process_create(process, name, code, data, hostname,
209 kill_time, argc, argv, properties, auto_restart,
213 * \brief Internal function to create a process.
215 * This function actually creates the process.
216 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
217 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
219 * \return the process created
221 void SIMIX_process_create(smx_process_t *process,
223 xbt_main_func_t code,
225 const char *hostname,
227 int argc, char **argv,
228 xbt_dict_t properties,
230 smx_process_t parent_process)
233 smx_host_t host = SIMIX_host_get_by_name(hostname);
235 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
237 if (!SIMIX_host_get_state(host)) {
239 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
241 for (i = 0; i < argc; i++)
246 *process = xbt_new0(s_smx_process_t, 1);
248 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
250 (*process)->pid = simix_process_maxpid++;
251 (*process)->name = xbt_strdup(name);
252 (*process)->smx_host = host;
253 (*process)->data = data;
254 (*process)->comms = xbt_fifo_new();
255 (*process)->simcall.issuer = *process;
257 if (parent_process) {
258 (*process)->ppid = SIMIX_process_get_PID(parent_process);
260 (*process)->ppid = -1;
263 /* Process data for auto-restart */
264 (*process)->auto_restart = auto_restart;
265 (*process)->code = code;
266 (*process)->argc = argc;
267 (*process)->argv = argv;
270 XBT_VERB("Create context %s", (*process)->name);
271 (*process)->context = SIMIX_context_new(code, argc, argv,
272 simix_global->cleanup_process_function, *process);
274 (*process)->running_ctx = xbt_new(xbt_running_ctx_t, 1);
275 XBT_RUNNING_CTX_INITIALIZE((*process)->running_ctx);
278 MC_ignore_heap((*process)->running_ctx, sizeof(*(*process)->running_ctx));
282 (*process)->properties = properties;
284 /* Add the process to it's host process list */
285 xbt_swag_insert(*process, SIMIX_host_priv(host)->process_list);
287 XBT_DEBUG("Start context '%s'", (*process)->name);
289 /* Now insert it in the global process list and in the process to run list */
290 xbt_swag_insert(*process, simix_global->process_list);
291 XBT_DEBUG("Inserting %s(%s) in the to_run list", (*process)->name, sg_host_name(host));
292 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, *process);
294 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
295 XBT_DEBUG("Process %s(%s) will be kill at time %f", (*process)->name,
296 sg_host_name((*process)->smx_host), kill_time);
297 (*process)->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, *process);
303 * \brief Executes the processes from simix_global->process_to_run.
305 * The processes of simix_global->process_to_run are run (in parallel if
306 * possible). On exit, simix_global->process_to_run is empty, and
307 * simix_global->process_that_ran contains the list of processes that just ran.
308 * The two lists are swapped so, be careful when using them before and after a
309 * call to this function.
311 void SIMIX_process_runall(void)
313 SIMIX_context_runall();
315 xbt_dynar_t tmp = simix_global->process_that_ran;
316 simix_global->process_that_ran = simix_global->process_to_run;
317 simix_global->process_to_run = tmp;
318 xbt_dynar_reset(simix_global->process_to_run);
321 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
322 SIMIX_process_kill(process, simcall->issuer);
325 * \brief Internal function to kill a SIMIX process.
327 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
328 * or directly for SIMIX internal purposes.
330 * \param process poor victim
331 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
333 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
335 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->smx_host));
337 process->context->iwannadie = 1;
338 process->blocked = 0;
339 process->suspended = 0;
340 process->doexception = 0;
342 /* destroy the blocking synchro if any */
343 if (process->waiting_synchro) {
345 switch (process->waiting_synchro->type) {
347 case SIMIX_SYNC_EXECUTE:
348 case SIMIX_SYNC_PARALLEL_EXECUTE:
349 SIMIX_host_execution_destroy(process->waiting_synchro);
352 case SIMIX_SYNC_COMMUNICATE:
353 xbt_fifo_remove(process->comms, process->waiting_synchro);
354 SIMIX_comm_cancel(process->waiting_synchro);
355 SIMIX_comm_destroy(process->waiting_synchro);
358 case SIMIX_SYNC_SLEEP:
359 SIMIX_process_sleep_destroy(process->waiting_synchro);
362 case SIMIX_SYNC_JOIN:
363 SIMIX_process_sleep_destroy(process->waiting_synchro);
366 case SIMIX_SYNC_SYNCHRO:
367 SIMIX_synchro_stop_waiting(process, &process->simcall);
368 SIMIX_synchro_destroy(process->waiting_synchro);
372 SIMIX_io_destroy(process->waiting_synchro);
377 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
378 XBT_DEBUG("Inserting %s in the to_run list", process->name);
379 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
384 /** @brief Ask another process to raise the given exception
386 * @param cat category of exception
387 * @param value value associated to the exception
388 * @param msg string information associated to the exception
390 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
391 SMX_EXCEPTION(process, cat, value, msg);
393 if (process->suspended)
394 SIMIX_process_resume(process,SIMIX_process_self());
396 /* cancel the blocking synchro if any */
397 if (process->waiting_synchro) {
399 switch (process->waiting_synchro->type) {
401 case SIMIX_SYNC_EXECUTE:
402 case SIMIX_SYNC_PARALLEL_EXECUTE:
403 SIMIX_host_execution_cancel(process->waiting_synchro);
406 case SIMIX_SYNC_COMMUNICATE:
407 xbt_fifo_remove(process->comms, process->waiting_synchro);
408 SIMIX_comm_cancel(process->waiting_synchro);
411 case SIMIX_SYNC_SLEEP:
412 case SIMIX_SYNC_JOIN:
413 SIMIX_process_sleep_destroy(process->waiting_synchro);
414 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
415 XBT_DEBUG("Inserting %s in the to_run list", process->name);
416 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
420 case SIMIX_SYNC_SYNCHRO:
421 SIMIX_synchro_stop_waiting(process, &process->simcall);
425 SIMIX_io_destroy(process->waiting_synchro);
430 process->waiting_synchro = NULL;
434 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
435 SIMIX_process_killall(simcall->issuer, reset_pid);
438 * \brief Kills all running processes.
439 * \param issuer this one will not be killed
441 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
443 smx_process_t p = NULL;
445 while ((p = xbt_swag_extract(simix_global->process_list))) {
447 SIMIX_process_kill(p,issuer);
452 simix_process_maxpid = reset_pid;
454 SIMIX_context_runall();
456 SIMIX_process_empty_trash();
459 void simcall_HANDLER_process_change_host(smx_simcall_t simcall, smx_process_t process,
462 process->new_host = dest;
464 void SIMIX_process_change_host(smx_process_t process,
467 xbt_assert((process != NULL), "Invalid parameters");
468 xbt_swag_remove(process, SIMIX_host_priv(process->smx_host)->process_list);
469 process->smx_host = dest;
470 xbt_swag_insert(process, SIMIX_host_priv(dest)->process_list);
474 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
476 smx_synchro_t sync_suspend =
477 SIMIX_process_suspend(process, simcall->issuer);
479 if (process != simcall->issuer) {
480 SIMIX_simcall_answer(simcall);
482 xbt_fifo_push(sync_suspend->simcalls, simcall);
483 process->waiting_synchro = sync_suspend;
484 SIMIX_host_execution_suspend(process->waiting_synchro);
486 /* If we are suspending ourselves, then just do not finish the simcall now */
489 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
491 xbt_assert((process != NULL), "Invalid parameters");
493 if (process->suspended) {
494 XBT_DEBUG("Process '%s' is already suspended", process->name);
498 process->suspended = 1;
500 /* If we are suspending another process, and it is waiting on a sync,
501 suspend its synchronization. */
502 if (process != issuer) {
504 if (process->waiting_synchro) {
506 switch (process->waiting_synchro->type) {
508 case SIMIX_SYNC_EXECUTE:
509 case SIMIX_SYNC_PARALLEL_EXECUTE:
510 SIMIX_host_execution_suspend(process->waiting_synchro);
513 case SIMIX_SYNC_COMMUNICATE:
514 SIMIX_comm_suspend(process->waiting_synchro);
517 case SIMIX_SYNC_SLEEP:
518 SIMIX_process_sleep_suspend(process->waiting_synchro);
521 case SIMIX_SYNC_SYNCHRO:
522 /* Suspension is delayed to when the process is rescheduled. */
526 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
527 (int)process->waiting_synchro->type);
531 /* Suspension is delayed to when the process is rescheduled. */
535 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
536 return SIMIX_host_execute("suspend", process->smx_host, 0.0, 1.0, 0.0, 0);
540 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
541 SIMIX_process_resume(process, simcall->issuer);
544 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
546 XBT_IN("process = %p, issuer = %p", process, issuer);
548 if(process->context->iwannadie) {
549 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
553 if(!process->suspended) return;
554 process->suspended = 0;
556 /* If we are resuming another process, resume the synchronization it was waiting for
557 if any. Otherwise add it to the list of process to run in the next round. */
558 if (process != issuer) {
560 if (process->waiting_synchro) {
562 switch (process->waiting_synchro->type) {
564 case SIMIX_SYNC_EXECUTE:
565 case SIMIX_SYNC_PARALLEL_EXECUTE:
566 SIMIX_host_execution_resume(process->waiting_synchro);
569 case SIMIX_SYNC_COMMUNICATE:
570 SIMIX_comm_resume(process->waiting_synchro);
573 case SIMIX_SYNC_SLEEP:
574 SIMIX_process_sleep_resume(process->waiting_synchro);
577 case SIMIX_SYNC_SYNCHRO:
578 /* I cannot resume it now. This is delayed to when the process is rescheduled at
579 * the end of the synchro. */
583 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
584 (int)process->waiting_synchro->type);
587 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
592 int SIMIX_process_get_maxpid(void) {
593 return simix_process_maxpid;
596 int SIMIX_process_count(void)
598 return xbt_swag_size(simix_global->process_list);
601 int SIMIX_process_get_PID(smx_process_t self){
608 int SIMIX_process_get_PPID(smx_process_t self){
615 void* SIMIX_process_self_get_data(smx_process_t self)
617 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
622 return SIMIX_process_get_data(self);
625 void SIMIX_process_self_set_data(smx_process_t self, void *data)
627 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
629 SIMIX_process_set_data(self, data);
632 void* SIMIX_process_get_data(smx_process_t process)
634 return process->data;
637 void SIMIX_process_set_data(smx_process_t process, void *data)
639 process->data = data;
642 smx_host_t SIMIX_process_get_host(smx_process_t process)
644 return process->smx_host;
647 /* needs to be public and without simcall because it is called
648 by exceptions and logging events */
649 const char* SIMIX_process_self_get_name(void) {
651 smx_process_t process = SIMIX_process_self();
652 if (process == NULL || process == simix_global->maestro_process)
655 return SIMIX_process_get_name(process);
658 const char* SIMIX_process_get_name(smx_process_t process)
660 return process->name;
663 smx_process_t SIMIX_process_get_by_name(const char* name)
667 xbt_swag_foreach(proc, simix_global->process_list)
669 if(!strcmp(name, proc->name))
675 int SIMIX_process_is_suspended(smx_process_t process)
677 return process->suspended;
680 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
682 return process->properties;
685 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
687 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
688 xbt_fifo_push(sync->simcalls, simcall);
689 simcall->issuer->waiting_synchro = sync;
692 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
693 if (sync->sleep.surf_sleep) {
694 surf_action_cancel(sync->sleep.surf_sleep);
696 smx_simcall_t simcall;
697 while ((simcall = xbt_fifo_shift(sync->simcalls))) {
698 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
699 simcall->issuer->waiting_synchro = NULL;
700 if (simcall->issuer->suspended) {
701 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
702 simcall->issuer->suspended = 0;
703 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
705 SIMIX_simcall_answer(simcall);
708 surf_action_unref(sync->sleep.surf_sleep);
709 sync->sleep.surf_sleep = NULL;
711 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
715 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
717 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
718 res->type = SIMIX_SYNC_JOIN;
719 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
723 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
725 if (MC_is_active() || MC_record_replay_is_active()) {
726 MC_process_clock_add(simcall->issuer, duration);
727 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
728 SIMIX_simcall_answer(simcall);
731 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
732 xbt_fifo_push(sync->simcalls, simcall);
733 simcall->issuer->waiting_synchro = sync;
736 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
738 smx_synchro_t synchro;
739 smx_host_t host = process->smx_host;
741 /* check if the host is active */
742 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
743 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
747 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
748 synchro->type = SIMIX_SYNC_SLEEP;
749 synchro->name = NULL;
750 synchro->category = NULL;
752 synchro->sleep.host = host;
753 synchro->sleep.surf_sleep =
754 surf_workstation_sleep(host, duration);
756 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
757 XBT_DEBUG("Create sleep synchronization %p", synchro);
762 void SIMIX_post_process_sleep(smx_synchro_t synchro)
764 smx_simcall_t simcall;
766 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
768 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
770 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
771 case SURF_ACTION_FAILED:
772 simcall->issuer->context->iwannadie = 1;
773 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
774 state = SIMIX_SRC_HOST_FAILURE;
777 case SURF_ACTION_DONE:
785 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
786 simcall->issuer->context->iwannadie = 1;
788 simcall_process_sleep__set__result(simcall, state);
789 simcall->issuer->waiting_synchro = NULL;
790 if (simcall->issuer->suspended) {
791 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
792 simcall->issuer->suspended = 0;
793 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
795 SIMIX_simcall_answer(simcall);
799 SIMIX_process_sleep_destroy(synchro);
802 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
804 XBT_DEBUG("Destroy synchro %p", synchro);
805 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
807 if (synchro->sleep.surf_sleep) {
808 surf_action_unref(synchro->sleep.surf_sleep);
809 synchro->sleep.surf_sleep = NULL;
811 if (synchro->type == SIMIX_SYNC_SLEEP)
812 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
815 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
817 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
818 surf_action_suspend(synchro->sleep.surf_sleep);
821 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
823 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
824 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
825 surf_action_resume(synchro->sleep.surf_sleep);
829 * \brief Calling this function makes the process to yield.
831 * Only the current process can call this function, giving back the control to
834 * \param self the current process
836 void SIMIX_process_yield(smx_process_t self)
838 XBT_DEBUG("Yield process '%s'", self->name);
840 /* Go into sleep and return control to maestro */
841 SIMIX_context_suspend(self->context);
843 /* Ok, maestro returned control to us */
844 XBT_DEBUG("Control returned to me: '%s'", self->name);
846 if (self->new_host) {
847 SIMIX_process_change_host(self, self->new_host);
848 self->new_host = NULL;
851 if (self->context->iwannadie){
852 XBT_DEBUG("I wanna die!");
853 SIMIX_process_stop(self);
856 if (self->suspended) {
857 XBT_DEBUG("Hey! I'm suspended.");
858 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
860 SIMIX_process_suspend(self, self);
863 if (self->doexception) {
864 XBT_DEBUG("Wait, maestro left me an exception");
865 self->doexception = 0;
871 /* callback: context fetching */
872 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
874 return SIMIX_process_self()->running_ctx;
877 /* callback: termination */
878 void SIMIX_process_exception_terminate(xbt_ex_t * e)
884 smx_context_t SIMIX_process_get_context(smx_process_t p) {
888 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
893 * \brief Returns the list of processes to run.
895 xbt_dynar_t SIMIX_process_get_runnable(void)
897 return simix_global->process_to_run;
901 * \brief Returns the process from PID.
903 smx_process_t SIMIX_process_from_PID(int PID)
906 xbt_swag_foreach(proc, simix_global->process_list)
914 /** @brief returns a dynar containg all currently existing processes */
915 xbt_dynar_t SIMIX_processes_as_dynar(void) {
917 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
918 xbt_swag_foreach(proc, simix_global->process_list) {
919 xbt_dynar_push(res,&proc);
925 void SIMIX_process_on_exit_runall(smx_process_t process) {
926 s_smx_process_exit_fun_t exit_fun;
927 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
928 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
929 while (!xbt_dynar_is_empty(process->on_exit)) {
930 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
931 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
935 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
936 xbt_assert(process, "current process not found: are you in maestro context ?");
938 if (!process->on_exit) {
939 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
942 s_smx_process_exit_fun_t exit_fun = {fun, data};
944 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
948 * \brief Sets the auto-restart status of the process.
949 * If set to 1, the process will be automatically restarted when its host
952 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
953 process->auto_restart = auto_restart;
956 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
957 return SIMIX_process_restart(process, simcall->issuer);
959 /** @brief Restart a process, starting it again from the beginning. */
960 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
961 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->smx_host));
962 //retrieve the arguments of the old process
963 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
964 s_smx_process_arg_t arg;
965 arg.code = process->code;
966 arg.hostname = sg_host_name(process->smx_host);
967 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
968 arg.argc = process->argc;
969 arg.data = process->data;
971 arg.argv = xbt_new(char*,process->argc + 1);
972 for (i = 0; i < arg.argc; i++) {
973 arg.argv[i] = xbt_strdup(process->argv[i]);
975 arg.argv[process->argc] = NULL;
976 arg.properties = NULL;
977 arg.auto_restart = process->auto_restart;
978 //kill the old process
979 SIMIX_process_kill(process,issuer);
980 //start the new process
981 smx_process_t new_process;
982 if (simix_global->create_process_function) {
983 simix_global->create_process_function(&new_process,
995 simcall_process_create(&new_process,