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"
14 #include "src/simix/smx_private.hpp"
17 #include "src/smpi/private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
21 "Logging specific to SIMIX (process)");
23 unsigned long simix_process_maxpid = 0;
26 * \brief Returns the current agent.
28 * This functions returns the currently running SIMIX process.
30 * \return The SIMIX process
32 smx_process_t SIMIX_process_self(void)
34 smx_context_t self_context = SIMIX_context_self();
36 return self_context ? SIMIX_context_get_process(self_context) : NULL;
40 * \brief Returns whether a process has pending asynchronous communications.
41 * \return true if there are asynchronous communications in this process
43 int SIMIX_process_has_pending_comms(smx_process_t process) {
45 return xbt_fifo_size(process->comms) > 0;
49 * \brief Moves a process to the list of processes to destroy.
51 void SIMIX_process_cleanup(smx_process_t process)
53 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
54 process->name, process, process->waiting_synchro);
56 SIMIX_process_on_exit_runall(process);
58 /* Unregister from the kill timer if any */
59 if (process->kill_timer != NULL)
60 SIMIX_timer_remove(process->kill_timer);
62 xbt_os_mutex_acquire(simix_global->mutex);
64 /* cancel non-blocking communications */
65 smx_synchro_t synchro;
66 while ((synchro = (smx_synchro_t) xbt_fifo_pop(process->comms))) {
68 /* make sure no one will finish the comm after this process is destroyed,
69 * because src_proc or dst_proc would be an invalid pointer */
70 SIMIX_comm_cancel(synchro);
72 if (synchro->comm.src_proc == process) {
73 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
74 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
75 synchro->comm.src_proc = NULL;
77 /* I'm not supposed to destroy a detached comm from the sender side, */
78 if (!synchro->comm.detached)
79 SIMIX_comm_destroy(synchro);
81 XBT_DEBUG("Don't destroy it since it's a detached comm");
84 else if (synchro->comm.dst_proc == process){
85 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
86 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
87 synchro->comm.dst_proc = NULL;
89 if (synchro->comm.detached && synchro->comm.refcount == 1
90 && synchro->comm.src_proc != NULL) {
91 /* the comm will be freed right now, remove it from the sender */
92 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
94 SIMIX_comm_destroy(synchro);
97 xbt_die("Communication synchro %p is in my list but I'm not the sender "
98 "or the receiver", synchro);
102 XBT_DEBUG("%p should not be run anymore",process);
103 xbt_swag_remove(process, simix_global->process_list);
104 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
105 xbt_swag_insert(process, simix_global->process_to_destroy);
106 process->context->iwannadie = 0;
108 xbt_os_mutex_release(simix_global->mutex);
114 * Should be called some time to time to free the memory allocated for processes
115 * that have finished (or killed).
117 void SIMIX_process_empty_trash(void)
119 smx_process_t process = NULL;
121 while ((process = (smx_process_t) xbt_swag_extract(simix_global->process_to_destroy))) {
122 XBT_DEBUG("Getting rid of %p",process);
124 SIMIX_context_free(process->context);
126 /* Free the exception allocated at creation time */
127 free(process->running_ctx);
128 xbt_dict_free(&process->properties);
130 xbt_fifo_free(process->comms);
132 xbt_dynar_free(&process->on_exit);
134 xbt_free(process->name);
140 * \brief Creates and runs the maestro process
142 void SIMIX_create_maestro_process()
144 smx_process_t maestro = NULL;
146 /* Create maestro process and intilialize it */
147 maestro = xbt_new0(s_smx_process_t, 1);
148 maestro->pid = simix_process_maxpid++;
150 maestro->name = (char *) "";
151 maestro->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
152 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
153 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
154 maestro->simcall.issuer = maestro;
155 simix_global->maestro_process = maestro;
159 * \brief Stops a process.
161 * Stops the process, execute all the registered on_exit functions,
162 * register it to the list of the process to restart if needed
163 * and stops its context.
165 void SIMIX_process_stop(smx_process_t arg) {
166 /* execute the on_exit functions */
167 SIMIX_process_on_exit_runall(arg);
168 /* Add the process to the list of process to restart, only if the host is down */
169 if (arg->auto_restart && arg->host->isOff()) {
170 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
171 sg_host_get_name(arg->host),
172 SIMIX_timer_get_date(arg->kill_timer),
173 arg->argc,arg->argv,arg->properties,
176 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_get_name(arg->host));
177 /* stop the context */
178 SIMIX_context_stop(arg->context);
182 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
183 * This function frees the argument.
184 * \return the process created
186 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
188 smx_process_t process = simix_global->create_process_function(
204 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
206 xbt_main_func_t code,
208 const char *hostname,
210 int argc, char **argv,
211 xbt_dict_t properties,
213 return (void*)SIMIX_process_create(name, code, data, hostname,
214 kill_time, argc, argv, properties, auto_restart,
218 static void kill_process(void* process)
220 simix_global->kill_process_function((smx_process_t) process);
224 * \brief Internal function to create a process.
226 * This function actually creates the process.
227 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
228 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
230 * \return the process created
232 smx_process_t SIMIX_process_create(
234 xbt_main_func_t code,
236 const char *hostname,
238 int argc, char **argv,
239 xbt_dict_t properties,
241 smx_process_t parent_process)
243 smx_process_t process = NULL;
244 sg_host_t host = sg_host_by_name(hostname);
246 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
250 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
252 for (i = 0; i < argc; i++)
257 process = xbt_new0(s_smx_process_t, 1);
259 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
261 process->pid = simix_process_maxpid++;
262 process->name = xbt_strdup(name);
263 process->host = host;
264 process->data = data;
265 process->comms = xbt_fifo_new();
266 process->simcall.issuer = process;
267 /* Initiliaze data segment to default value */
268 SIMIX_segment_index_set(process, -1);
270 if (parent_process != NULL) {
271 process->ppid = SIMIX_process_get_PID(parent_process);
272 /* SMPI process have their own data segment and
273 each other inherit from their father */
275 if(smpi_privatize_global_variables){
276 if( parent_process->pid != 0){
277 SIMIX_segment_index_set(process, parent_process->segment_index);
279 SIMIX_segment_index_set(process, process->pid - 1);
287 /* Process data for auto-restart */
288 process->auto_restart = auto_restart;
289 process->code = code;
290 process->argc = argc;
291 process->argv = argv;
294 XBT_VERB("Create context %s", process->name);
295 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
297 process->running_ctx = (xbt_running_ctx_t*) xbt_malloc0(sizeof(xbt_running_ctx_t));
298 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
301 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
305 process->properties = properties;
307 /* Add the process to it's host process list */
308 xbt_swag_insert(process, sg_host_simix(host)->process_list);
310 XBT_DEBUG("Start context '%s'", process->name);
312 /* Now insert it in the global process list and in the process to run list */
313 xbt_swag_insert(process, simix_global->process_list);
314 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_get_name(host));
315 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
317 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
318 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
319 sg_host_get_name(process->host), kill_time);
320 process->kill_timer = SIMIX_timer_set(kill_time, kill_process, process);
327 * \brief Executes the processes from simix_global->process_to_run.
329 * The processes of simix_global->process_to_run are run (in parallel if
330 * possible). On exit, simix_global->process_to_run is empty, and
331 * simix_global->process_that_ran contains the list of processes that just ran.
332 * The two lists are swapped so, be careful when using them before and after a
333 * call to this function.
335 void SIMIX_process_runall(void)
337 SIMIX_context_runall();
339 xbt_dynar_t tmp = simix_global->process_that_ran;
340 simix_global->process_that_ran = simix_global->process_to_run;
341 simix_global->process_to_run = tmp;
342 xbt_dynar_reset(simix_global->process_to_run);
345 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
346 SIMIX_process_kill(process, simcall->issuer);
349 * \brief Internal function to kill a SIMIX process.
351 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
352 * or directly for SIMIX internal purposes.
354 * \param process poor victim
355 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
357 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
359 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_get_name(process->host));
361 process->context->iwannadie = 1;
362 process->blocked = 0;
363 process->suspended = 0;
364 process->doexception = 0;
366 /* destroy the blocking synchro if any */
367 if (process->waiting_synchro) {
369 switch (process->waiting_synchro->type) {
371 case SIMIX_SYNC_EXECUTE:
372 case SIMIX_SYNC_PARALLEL_EXECUTE:
373 SIMIX_process_execution_destroy(process->waiting_synchro);
376 case SIMIX_SYNC_COMMUNICATE:
377 xbt_fifo_remove(process->comms, process->waiting_synchro);
378 SIMIX_comm_cancel(process->waiting_synchro);
379 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
380 SIMIX_comm_destroy(process->waiting_synchro);
383 case SIMIX_SYNC_SLEEP:
384 SIMIX_process_sleep_destroy(process->waiting_synchro);
387 case SIMIX_SYNC_JOIN:
388 SIMIX_process_sleep_destroy(process->waiting_synchro);
391 case SIMIX_SYNC_SYNCHRO:
392 SIMIX_synchro_stop_waiting(process, &process->simcall);
393 SIMIX_synchro_destroy(process->waiting_synchro);
397 SIMIX_io_destroy(process->waiting_synchro);
402 process->waiting_synchro = NULL;
404 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
405 XBT_DEBUG("Inserting %s in the to_run list", process->name);
406 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
411 /** @brief Ask another process to raise the given exception
413 * @param cat category of exception
414 * @param value value associated to the exception
415 * @param msg string information associated to the exception
417 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
418 SMX_EXCEPTION(process, cat, value, msg);
420 if (process->suspended)
421 SIMIX_process_resume(process,SIMIX_process_self());
423 /* cancel the blocking synchro if any */
424 if (process->waiting_synchro) {
426 switch (process->waiting_synchro->type) {
428 case SIMIX_SYNC_EXECUTE:
429 case SIMIX_SYNC_PARALLEL_EXECUTE:
430 SIMIX_process_execution_cancel(process->waiting_synchro);
433 case SIMIX_SYNC_COMMUNICATE:
434 xbt_fifo_remove(process->comms, process->waiting_synchro);
435 SIMIX_comm_cancel(process->waiting_synchro);
438 case SIMIX_SYNC_SLEEP:
439 case SIMIX_SYNC_JOIN:
440 SIMIX_process_sleep_destroy(process->waiting_synchro);
441 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
442 XBT_DEBUG("Inserting %s in the to_run list", process->name);
443 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
447 case SIMIX_SYNC_SYNCHRO:
448 SIMIX_synchro_stop_waiting(process, &process->simcall);
452 SIMIX_io_destroy(process->waiting_synchro);
457 process->waiting_synchro = NULL;
461 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
462 SIMIX_process_killall(simcall->issuer, reset_pid);
465 * \brief Kills all running processes.
466 * \param issuer this one will not be killed
468 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
470 smx_process_t p = NULL;
472 while ((p = (smx_process_t) xbt_swag_extract(simix_global->process_list))) {
474 SIMIX_process_kill(p,issuer);
479 simix_process_maxpid = reset_pid;
481 SIMIX_context_runall();
483 SIMIX_process_empty_trash();
486 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
488 process->new_host = dest;
490 void SIMIX_process_change_host(smx_process_t process,
493 xbt_assert((process != NULL), "Invalid parameters");
494 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
495 process->host = dest;
496 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
500 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
502 smx_synchro_t sync_suspend =
503 SIMIX_process_suspend(process, simcall->issuer);
505 if (process != simcall->issuer) {
506 SIMIX_simcall_answer(simcall);
508 xbt_fifo_push(sync_suspend->simcalls, simcall);
509 process->waiting_synchro = sync_suspend;
510 SIMIX_host_execution_suspend(process->waiting_synchro);
512 /* If we are suspending ourselves, then just do not finish the simcall now */
515 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
517 xbt_assert((process != NULL), "Invalid parameters");
519 if (process->suspended) {
520 XBT_DEBUG("Process '%s' is already suspended", process->name);
524 process->suspended = 1;
526 /* If we are suspending another process, and it is waiting on a sync,
527 suspend its synchronization. */
528 if (process != issuer) {
530 if (process->waiting_synchro) {
532 switch (process->waiting_synchro->type) {
534 case SIMIX_SYNC_EXECUTE:
535 case SIMIX_SYNC_PARALLEL_EXECUTE:
536 SIMIX_host_execution_suspend(process->waiting_synchro);
539 case SIMIX_SYNC_COMMUNICATE:
540 SIMIX_comm_suspend(process->waiting_synchro);
543 case SIMIX_SYNC_SLEEP:
544 SIMIX_process_sleep_suspend(process->waiting_synchro);
547 case SIMIX_SYNC_SYNCHRO:
548 /* Suspension is delayed to when the process is rescheduled. */
552 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
553 (int)process->waiting_synchro->type);
557 /* Suspension is delayed to when the process is rescheduled. */
561 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
562 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
566 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
567 SIMIX_process_resume(process, simcall->issuer);
570 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
572 XBT_IN("process = %p, issuer = %p", process, issuer);
574 if(process->context->iwannadie) {
575 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
579 if(!process->suspended) return;
580 process->suspended = 0;
582 /* If we are resuming another process, resume the synchronization it was waiting for
583 if any. Otherwise add it to the list of process to run in the next round. */
584 if (process != issuer) {
586 if (process->waiting_synchro) {
588 switch (process->waiting_synchro->type) {
590 case SIMIX_SYNC_EXECUTE:
591 case SIMIX_SYNC_PARALLEL_EXECUTE:
592 SIMIX_host_execution_resume(process->waiting_synchro);
595 case SIMIX_SYNC_COMMUNICATE:
596 SIMIX_comm_resume(process->waiting_synchro);
599 case SIMIX_SYNC_SLEEP:
600 SIMIX_process_sleep_resume(process->waiting_synchro);
603 case SIMIX_SYNC_SYNCHRO:
604 /* I cannot resume it now. This is delayed to when the process is rescheduled at
605 * the end of the synchro. */
609 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
610 (int)process->waiting_synchro->type);
613 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
618 int SIMIX_process_get_maxpid(void) {
619 return simix_process_maxpid;
622 int SIMIX_process_count(void)
624 return xbt_swag_size(simix_global->process_list);
627 int SIMIX_process_get_PID(smx_process_t self){
634 int SIMIX_process_get_PPID(smx_process_t self){
641 void* SIMIX_process_self_get_data(smx_process_t self)
643 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
648 return SIMIX_process_get_data(self);
651 void SIMIX_process_self_set_data(smx_process_t self, void *data)
653 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
655 SIMIX_process_set_data(self, data);
658 void* SIMIX_process_get_data(smx_process_t process)
660 return process->data;
663 void SIMIX_process_set_data(smx_process_t process, void *data)
665 process->data = data;
668 sg_host_t SIMIX_process_get_host(smx_process_t process)
670 return process->host;
673 xbt_main_func_t SIMIX_process_get_code(void){
674 return SIMIX_process_self()->code;
677 /* needs to be public and without simcall because it is called
678 by exceptions and logging events */
679 const char* SIMIX_process_self_get_name(void) {
681 smx_process_t process = SIMIX_process_self();
682 if (process == NULL || process == simix_global->maestro_process)
685 return SIMIX_process_get_name(process);
688 const char* SIMIX_process_get_name(smx_process_t process)
690 return process->name;
693 smx_process_t SIMIX_process_get_by_name(const char* name)
697 xbt_swag_foreach(proc, simix_global->process_list)
699 if(!strcmp(name, proc->name))
705 int SIMIX_process_is_suspended(smx_process_t process)
707 return process->suspended;
710 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
712 return process->properties;
715 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
717 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
718 xbt_fifo_push(sync->simcalls, simcall);
719 simcall->issuer->waiting_synchro = sync;
722 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
723 if (sync->sleep.surf_sleep) {
724 surf_action_cancel(sync->sleep.surf_sleep);
726 smx_simcall_t simcall;
727 while ((simcall = (smx_simcall_t) xbt_fifo_shift(sync->simcalls))) {
728 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
729 simcall->issuer->waiting_synchro = NULL;
730 if (simcall->issuer->suspended) {
731 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
732 simcall->issuer->suspended = 0;
733 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
735 SIMIX_simcall_answer(simcall);
738 surf_action_unref(sync->sleep.surf_sleep);
739 sync->sleep.surf_sleep = NULL;
741 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
745 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
747 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
748 res->type = SIMIX_SYNC_JOIN;
749 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
753 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
755 if (MC_is_active() || MC_record_replay_is_active()) {
756 MC_process_clock_add(simcall->issuer, duration);
757 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
758 SIMIX_simcall_answer(simcall);
761 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
762 xbt_fifo_push(sync->simcalls, simcall);
763 simcall->issuer->waiting_synchro = sync;
766 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
768 sg_host_t host = process->host;
770 /* check if the host is active */
772 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
773 sg_host_get_name(host));
776 smx_synchro_t synchro = (smx_synchro_t) xbt_mallocator_get(simix_global->synchro_mallocator);
777 synchro->type = SIMIX_SYNC_SLEEP;
778 synchro->name = NULL;
779 synchro->category = NULL;
781 synchro->sleep.host = host;
782 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
784 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
785 XBT_DEBUG("Create sleep synchronization %p", synchro);
790 void SIMIX_post_process_sleep(smx_synchro_t synchro)
792 smx_simcall_t simcall;
794 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
796 while ((simcall = (smx_simcall_t) xbt_fifo_shift(synchro->simcalls))) {
798 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
799 case SURF_ACTION_FAILED:
800 simcall->issuer->context->iwannadie = 1;
801 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
802 state = SIMIX_SRC_HOST_FAILURE;
805 case SURF_ACTION_DONE:
813 if (simcall->issuer->host->isOff()) {
814 simcall->issuer->context->iwannadie = 1;
816 simcall_process_sleep__set__result(simcall, state);
817 simcall->issuer->waiting_synchro = NULL;
818 if (simcall->issuer->suspended) {
819 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
820 simcall->issuer->suspended = 0;
821 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
823 SIMIX_simcall_answer(simcall);
827 SIMIX_process_sleep_destroy(synchro);
830 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
832 XBT_DEBUG("Destroy synchro %p", synchro);
833 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
835 if (synchro->sleep.surf_sleep) {
836 surf_action_unref(synchro->sleep.surf_sleep);
837 synchro->sleep.surf_sleep = NULL;
839 if (synchro->type == SIMIX_SYNC_SLEEP)
840 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
843 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
845 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
846 surf_action_suspend(synchro->sleep.surf_sleep);
849 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
851 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
852 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
853 surf_action_resume(synchro->sleep.surf_sleep);
857 * \brief Calling this function makes the process to yield.
859 * Only the current process can call this function, giving back the control to
862 * \param self the current process
864 void SIMIX_process_yield(smx_process_t self)
866 XBT_DEBUG("Yield process '%s'", self->name);
868 /* Go into sleep and return control to maestro */
869 SIMIX_context_suspend(self->context);
871 /* Ok, maestro returned control to us */
872 XBT_DEBUG("Control returned to me: '%s'", self->name);
874 if (self->new_host) {
875 SIMIX_process_change_host(self, self->new_host);
876 self->new_host = NULL;
879 if (self->context->iwannadie){
880 XBT_DEBUG("I wanna die!");
881 SIMIX_process_stop(self);
884 if (self->suspended) {
885 XBT_DEBUG("Hey! I'm suspended.");
886 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
888 SIMIX_process_suspend(self, self);
891 if (self->doexception) {
892 XBT_DEBUG("Wait, maestro left me an exception");
893 self->doexception = 0;
897 if(SMPI_switch_data_segment && self->segment_index != -1){
898 SMPI_switch_data_segment(self->segment_index);
902 /* callback: context fetching */
903 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
905 return SIMIX_process_self()->running_ctx;
908 /* callback: termination */
909 void SIMIX_process_exception_terminate(xbt_ex_t * e)
915 smx_context_t SIMIX_process_get_context(smx_process_t p) {
919 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
924 * \brief Returns the list of processes to run.
926 xbt_dynar_t SIMIX_process_get_runnable(void)
928 return simix_global->process_to_run;
932 * \brief Returns the process from PID.
934 smx_process_t SIMIX_process_from_PID(int PID)
937 xbt_swag_foreach(proc, simix_global->process_list) {
938 if (proc->pid == (unsigned long) PID)
944 /** @brief returns a dynar containg all currently existing processes */
945 xbt_dynar_t SIMIX_processes_as_dynar(void) {
947 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
948 xbt_swag_foreach(proc, simix_global->process_list) {
949 xbt_dynar_push(res,&proc);
955 void SIMIX_process_on_exit_runall(smx_process_t process) {
956 s_smx_process_exit_fun_t exit_fun;
957 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
958 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
959 while (!xbt_dynar_is_empty(process->on_exit)) {
960 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
961 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
965 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
966 xbt_assert(process, "current process not found: are you in maestro context ?");
968 if (!process->on_exit) {
969 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
972 s_smx_process_exit_fun_t exit_fun = {fun, data};
974 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
978 * \brief Sets the auto-restart status of the process.
979 * If set to 1, the process will be automatically restarted when its host
982 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
983 process->auto_restart = auto_restart;
986 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
987 return SIMIX_process_restart(process, simcall->issuer);
989 /** @brief Restart a process, starting it again from the beginning. */
990 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
991 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_get_name(process->host));
992 //retrieve the arguments of the old process
993 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
994 s_smx_process_arg_t arg;
995 arg.code = process->code;
996 arg.hostname = sg_host_get_name(process->host);
997 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
998 arg.argc = process->argc;
999 arg.data = process->data;
1001 arg.argv = xbt_new(char*,process->argc + 1);
1002 for (i = 0; i < arg.argc; i++) {
1003 arg.argv[i] = xbt_strdup(process->argv[i]);
1005 arg.argv[process->argc] = NULL;
1006 arg.properties = NULL;
1007 arg.auto_restart = process->auto_restart;
1008 //kill the old process
1009 SIMIX_process_kill(process,issuer);
1010 //start the new process
1011 smx_process_t new_process;
1012 if (simix_global->create_process_function) {
1013 new_process = simix_global->create_process_function(
1025 new_process = simcall_process_create(
1040 void SIMIX_segment_index_set(smx_process_t proc, int index){
1041 proc->segment_index = index;