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 "mc/mc_replay.h"
13 #include "mc/mc_client.h"
16 #include "smpi/private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
20 "Logging specific to SIMIX (process)");
22 unsigned long simix_process_maxpid = 0;
25 * \brief Returns the current agent.
27 * This functions returns the currently running SIMIX process.
29 * \return The SIMIX process
31 XBT_INLINE smx_process_t SIMIX_process_self(void)
33 smx_context_t self_context = SIMIX_context_self();
35 return self_context ? SIMIX_context_get_process(self_context) : NULL;
39 * \brief Returns whether a process has pending asynchronous communications.
40 * \return true if there are asynchronous communications in this process
42 int SIMIX_process_has_pending_comms(smx_process_t process) {
44 return xbt_fifo_size(process->comms) > 0;
48 * \brief Moves a process to the list of processes to destroy.
50 void SIMIX_process_cleanup(smx_process_t process)
52 XBT_DEBUG("Cleanup process %s (%p), waiting synchro %p",
53 process->name, process, process->waiting_synchro);
55 SIMIX_process_on_exit_runall(process);
57 /* Unregister from the kill timer if any */
58 if (process->kill_timer != NULL)
59 SIMIX_timer_remove(process->kill_timer);
61 xbt_os_mutex_acquire(simix_global->mutex);
63 /* cancel non-blocking communications */
64 smx_synchro_t synchro;
65 while ((synchro = xbt_fifo_pop(process->comms))) {
67 /* make sure no one will finish the comm after this process is destroyed,
68 * because src_proc or dst_proc would be an invalid pointer */
69 SIMIX_comm_cancel(synchro);
71 if (synchro->comm.src_proc == process) {
72 XBT_DEBUG("Found an unfinished send comm %p (detached = %d), state %d, src = %p, dst = %p",
73 synchro, synchro->comm.detached, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
74 synchro->comm.src_proc = NULL;
76 /* I'm not supposed to destroy a detached comm from the sender side, */
77 if (!synchro->comm.detached)
78 SIMIX_comm_destroy(synchro);
80 XBT_DEBUG("Don't destroy it since it's a detached comm");
83 else if (synchro->comm.dst_proc == process){
84 XBT_DEBUG("Found an unfinished recv comm %p, state %d, src = %p, dst = %p",
85 synchro, (int)synchro->state, synchro->comm.src_proc, synchro->comm.dst_proc);
86 synchro->comm.dst_proc = NULL;
88 if (synchro->comm.detached && synchro->comm.refcount == 1
89 && synchro->comm.src_proc != NULL) {
90 /* the comm will be freed right now, remove it from the sender */
91 xbt_fifo_remove(synchro->comm.src_proc->comms, synchro);
93 SIMIX_comm_destroy(synchro);
96 xbt_die("Communication synchro %p is in my list but I'm not the sender "
97 "or the receiver", synchro);
101 XBT_DEBUG("%p should not be run anymore",process);
102 xbt_swag_remove(process, simix_global->process_list);
103 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
104 xbt_swag_insert(process, simix_global->process_to_destroy);
105 process->context->iwannadie = 0;
107 xbt_os_mutex_release(simix_global->mutex);
113 * Should be called some time to time to free the memory allocated for processes
114 * that have finished (or killed).
116 void SIMIX_process_empty_trash(void)
118 smx_process_t process = NULL;
120 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
121 XBT_DEBUG("Getting rid of %p",process);
123 SIMIX_context_free(process->context);
125 /* Free the exception allocated at creation time */
126 free(process->running_ctx);
127 xbt_dict_free(&process->properties);
129 xbt_fifo_free(process->comms);
131 xbt_dynar_free(&process->on_exit);
133 xbt_free(process->name);
139 * \brief Creates and runs the maestro process
141 void SIMIX_create_maestro_process()
143 smx_process_t maestro = NULL;
145 /* Create maestro process and intilialize it */
146 maestro = xbt_new0(s_smx_process_t, 1);
147 maestro->pid = simix_process_maxpid++;
149 maestro->name = (char *) "";
150 maestro->running_ctx = xbt_new(xbt_running_ctx_t, 1);
151 XBT_RUNNING_CTX_INITIALIZE(maestro->running_ctx);
152 maestro->context = SIMIX_context_new(NULL, 0, NULL, NULL, maestro);
153 maestro->simcall.issuer = maestro;
154 simix_global->maestro_process = maestro;
158 * \brief Stops a process.
160 * Stops the process, execute all the registered on_exit functions,
161 * register it to the list of the process to restart if needed
162 * and stops its context.
164 void SIMIX_process_stop(smx_process_t arg) {
165 /* execute the on_exit functions */
166 SIMIX_process_on_exit_runall(arg);
167 /* Add the process to the list of process to restart, only if
170 if (arg->auto_restart && !SIMIX_host_get_state(arg->host)) {
171 SIMIX_host_add_auto_restart_process(arg->host,arg->name,arg->code, arg->data,
172 sg_host_name(arg->host),
173 SIMIX_timer_get_date(arg->kill_timer),
174 arg->argc,arg->argv,arg->properties,
177 XBT_DEBUG("Process %s (%s) is dead",arg->name,sg_host_name(arg->host));
178 /* stop the context */
179 SIMIX_context_stop(arg->context);
183 * \brief Same as SIMIX_process_create() but with only one argument (used by timers).
184 * This function frees the argument.
185 * \return the process created
187 smx_process_t SIMIX_process_create_from_wrapper(smx_process_arg_t args) {
189 smx_process_t process = simix_global->create_process_function(
205 void* simcall_HANDLER_process_create(smx_simcall_t simcall,
207 xbt_main_func_t code,
209 const char *hostname,
211 int argc, char **argv,
212 xbt_dict_t properties,
214 return (void*)SIMIX_process_create(name, code, data, hostname,
215 kill_time, argc, argv, properties, auto_restart,
219 * \brief Internal function to create a process.
221 * This function actually creates the process.
222 * It may be called when a SIMCALL_PROCESS_CREATE simcall occurs,
223 * or directly for SIMIX internal purposes. The sure thing is that it's called from maestro context.
225 * \return the process created
227 smx_process_t SIMIX_process_create(
229 xbt_main_func_t code,
231 const char *hostname,
233 int argc, char **argv,
234 xbt_dict_t properties,
236 smx_process_t parent_process)
238 smx_process_t process = NULL;
239 sg_host_t host = sg_host_by_name(hostname);
241 XBT_DEBUG("Start process %s on host '%s'", name, hostname);
243 if (!SIMIX_host_get_state(host)) {
245 XBT_WARN("Cannot launch process '%s' on failed host '%s'", name,
247 for (i = 0; i < argc; i++)
252 process = xbt_new0(s_smx_process_t, 1);
254 xbt_assert(((code != NULL) && (host != NULL)), "Invalid parameters");
256 process->pid = simix_process_maxpid++;
257 process->name = xbt_strdup(name);
258 process->host = host;
259 process->data = data;
260 process->comms = xbt_fifo_new();
261 process->simcall.issuer = process;
262 /* Initiliaze data segment to default value */
263 SIMIX_segment_index_set(process, -1);
265 if (parent_process != NULL) {
266 process->ppid = SIMIX_process_get_PID(parent_process);
267 /* SMPI process have their own data segment and
268 each other inherit from their father */
269 if(smpi_privatize_global_variables){
270 if( parent_process->pid != 0){
271 SIMIX_segment_index_set(process, parent_process->segment_index);
273 SIMIX_segment_index_set(process, process->pid - 1);
280 /* Process data for auto-restart */
281 process->auto_restart = auto_restart;
282 process->code = code;
283 process->argc = argc;
284 process->argv = argv;
287 XBT_VERB("Create context %s", process->name);
288 process->context = SIMIX_context_new(code, argc, argv, simix_global->cleanup_process_function, process);
290 process->running_ctx = xbt_new(xbt_running_ctx_t, 1);
291 XBT_RUNNING_CTX_INITIALIZE(process->running_ctx);
294 MC_ignore_heap(process->running_ctx, sizeof(*process->running_ctx));
298 process->properties = properties;
300 /* Add the process to it's host process list */
301 xbt_swag_insert(process, sg_host_simix(host)->process_list);
303 XBT_DEBUG("Start context '%s'", process->name);
305 /* Now insert it in the global process list and in the process to run list */
306 xbt_swag_insert(process, simix_global->process_list);
307 XBT_DEBUG("Inserting %s(%s) in the to_run list", process->name, sg_host_name(host));
308 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
310 if (kill_time > SIMIX_get_clock() && simix_global->kill_process_function) {
311 XBT_DEBUG("Process %s(%s) will be kill at time %f", process->name,
312 sg_host_name(process->host), kill_time);
313 process->kill_timer = SIMIX_timer_set(kill_time, simix_global->kill_process_function, process);
320 * \brief Executes the processes from simix_global->process_to_run.
322 * The processes of simix_global->process_to_run are run (in parallel if
323 * possible). On exit, simix_global->process_to_run is empty, and
324 * simix_global->process_that_ran contains the list of processes that just ran.
325 * The two lists are swapped so, be careful when using them before and after a
326 * call to this function.
328 void SIMIX_process_runall(void)
330 SIMIX_context_runall();
332 xbt_dynar_t tmp = simix_global->process_that_ran;
333 simix_global->process_that_ran = simix_global->process_to_run;
334 simix_global->process_to_run = tmp;
335 xbt_dynar_reset(simix_global->process_to_run);
338 void simcall_HANDLER_process_kill(smx_simcall_t simcall, smx_process_t process) {
339 SIMIX_process_kill(process, simcall->issuer);
342 * \brief Internal function to kill a SIMIX process.
344 * This function may be called when a SIMCALL_PROCESS_KILL simcall occurs,
345 * or directly for SIMIX internal purposes.
347 * \param process poor victim
348 * \param issuer the process which has sent the PROCESS_KILL. Important to not schedule twice the same process.
350 void SIMIX_process_kill(smx_process_t process, smx_process_t issuer) {
352 XBT_DEBUG("Killing process %s on %s", process->name, sg_host_name(process->host));
354 process->context->iwannadie = 1;
355 process->blocked = 0;
356 process->suspended = 0;
357 process->doexception = 0;
359 /* destroy the blocking synchro if any */
360 if (process->waiting_synchro) {
362 switch (process->waiting_synchro->type) {
364 case SIMIX_SYNC_EXECUTE:
365 case SIMIX_SYNC_PARALLEL_EXECUTE:
366 SIMIX_process_execution_destroy(process->waiting_synchro);
369 case SIMIX_SYNC_COMMUNICATE:
370 xbt_fifo_remove(process->comms, process->waiting_synchro);
371 SIMIX_comm_cancel(process->waiting_synchro);
372 xbt_fifo_remove(process->waiting_synchro->simcalls, &process->simcall);
373 SIMIX_comm_destroy(process->waiting_synchro);
376 case SIMIX_SYNC_SLEEP:
377 SIMIX_process_sleep_destroy(process->waiting_synchro);
380 case SIMIX_SYNC_JOIN:
381 SIMIX_process_sleep_destroy(process->waiting_synchro);
384 case SIMIX_SYNC_SYNCHRO:
385 SIMIX_synchro_stop_waiting(process, &process->simcall);
386 SIMIX_synchro_destroy(process->waiting_synchro);
390 SIMIX_io_destroy(process->waiting_synchro);
395 process->waiting_synchro = NULL;
397 if(!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != issuer) {
398 XBT_DEBUG("Inserting %s in the to_run list", process->name);
399 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
404 /** @brief Ask another process to raise the given exception
406 * @param cat category of exception
407 * @param value value associated to the exception
408 * @param msg string information associated to the exception
410 void SIMIX_process_throw(smx_process_t process, xbt_errcat_t cat, int value, const char *msg) {
411 SMX_EXCEPTION(process, cat, value, msg);
413 if (process->suspended)
414 SIMIX_process_resume(process,SIMIX_process_self());
416 /* cancel the blocking synchro if any */
417 if (process->waiting_synchro) {
419 switch (process->waiting_synchro->type) {
421 case SIMIX_SYNC_EXECUTE:
422 case SIMIX_SYNC_PARALLEL_EXECUTE:
423 SIMIX_process_execution_cancel(process->waiting_synchro);
426 case SIMIX_SYNC_COMMUNICATE:
427 xbt_fifo_remove(process->comms, process->waiting_synchro);
428 SIMIX_comm_cancel(process->waiting_synchro);
431 case SIMIX_SYNC_SLEEP:
432 case SIMIX_SYNC_JOIN:
433 SIMIX_process_sleep_destroy(process->waiting_synchro);
434 if (!xbt_dynar_member(simix_global->process_to_run, &(process)) && process != SIMIX_process_self()) {
435 XBT_DEBUG("Inserting %s in the to_run list", process->name);
436 xbt_dynar_push_as(simix_global->process_to_run, smx_process_t, process);
440 case SIMIX_SYNC_SYNCHRO:
441 SIMIX_synchro_stop_waiting(process, &process->simcall);
445 SIMIX_io_destroy(process->waiting_synchro);
450 process->waiting_synchro = NULL;
454 void simcall_HANDLER_process_killall(smx_simcall_t simcall, int reset_pid) {
455 SIMIX_process_killall(simcall->issuer, reset_pid);
458 * \brief Kills all running processes.
459 * \param issuer this one will not be killed
461 void SIMIX_process_killall(smx_process_t issuer, int reset_pid)
463 smx_process_t p = NULL;
465 while ((p = xbt_swag_extract(simix_global->process_list))) {
467 SIMIX_process_kill(p,issuer);
472 simix_process_maxpid = reset_pid;
474 SIMIX_context_runall();
476 SIMIX_process_empty_trash();
479 void simcall_HANDLER_process_set_host(smx_simcall_t simcall, smx_process_t process, sg_host_t dest)
481 process->new_host = dest;
483 void SIMIX_process_change_host(smx_process_t process,
486 xbt_assert((process != NULL), "Invalid parameters");
487 xbt_swag_remove(process, sg_host_simix(process->host)->process_list);
488 process->host = dest;
489 xbt_swag_insert(process, sg_host_simix(dest)->process_list);
493 void simcall_HANDLER_process_suspend(smx_simcall_t simcall, smx_process_t process)
495 smx_synchro_t sync_suspend =
496 SIMIX_process_suspend(process, simcall->issuer);
498 if (process != simcall->issuer) {
499 SIMIX_simcall_answer(simcall);
501 xbt_fifo_push(sync_suspend->simcalls, simcall);
502 process->waiting_synchro = sync_suspend;
503 SIMIX_host_execution_suspend(process->waiting_synchro);
505 /* If we are suspending ourselves, then just do not finish the simcall now */
508 smx_synchro_t SIMIX_process_suspend(smx_process_t process, smx_process_t issuer)
510 xbt_assert((process != NULL), "Invalid parameters");
512 if (process->suspended) {
513 XBT_DEBUG("Process '%s' is already suspended", process->name);
517 process->suspended = 1;
519 /* If we are suspending another process, and it is waiting on a sync,
520 suspend its synchronization. */
521 if (process != issuer) {
523 if (process->waiting_synchro) {
525 switch (process->waiting_synchro->type) {
527 case SIMIX_SYNC_EXECUTE:
528 case SIMIX_SYNC_PARALLEL_EXECUTE:
529 SIMIX_host_execution_suspend(process->waiting_synchro);
532 case SIMIX_SYNC_COMMUNICATE:
533 SIMIX_comm_suspend(process->waiting_synchro);
536 case SIMIX_SYNC_SLEEP:
537 SIMIX_process_sleep_suspend(process->waiting_synchro);
540 case SIMIX_SYNC_SYNCHRO:
541 /* Suspension is delayed to when the process is rescheduled. */
545 xbt_die("Internal error in SIMIX_process_suspend: unexpected synchronization type %d",
546 (int)process->waiting_synchro->type);
550 /* Suspension is delayed to when the process is rescheduled. */
554 /* FIXME: computation size is zero. Is it okay that bound is zero ? */
555 return SIMIX_process_execute(process, "suspend", 0.0, 1.0, 0.0, 0);
559 void simcall_HANDLER_process_resume(smx_simcall_t simcall, smx_process_t process){
560 SIMIX_process_resume(process, simcall->issuer);
563 void SIMIX_process_resume(smx_process_t process, smx_process_t issuer)
565 XBT_IN("process = %p, issuer = %p", process, issuer);
567 if(process->context->iwannadie) {
568 XBT_VERB("Ignoring request to suspend a process that is currently dying.");
572 if(!process->suspended) return;
573 process->suspended = 0;
575 /* If we are resuming another process, resume the synchronization it was waiting for
576 if any. Otherwise add it to the list of process to run in the next round. */
577 if (process != issuer) {
579 if (process->waiting_synchro) {
581 switch (process->waiting_synchro->type) {
583 case SIMIX_SYNC_EXECUTE:
584 case SIMIX_SYNC_PARALLEL_EXECUTE:
585 SIMIX_host_execution_resume(process->waiting_synchro);
588 case SIMIX_SYNC_COMMUNICATE:
589 SIMIX_comm_resume(process->waiting_synchro);
592 case SIMIX_SYNC_SLEEP:
593 SIMIX_process_sleep_resume(process->waiting_synchro);
596 case SIMIX_SYNC_SYNCHRO:
597 /* I cannot resume it now. This is delayed to when the process is rescheduled at
598 * the end of the synchro. */
602 xbt_die("Internal error in SIMIX_process_resume: unexpected synchronization type %d",
603 (int)process->waiting_synchro->type);
606 } else XBT_WARN("Strange. Process %p is trying to resume himself.", issuer);
611 int SIMIX_process_get_maxpid(void) {
612 return simix_process_maxpid;
615 int SIMIX_process_count(void)
617 return xbt_swag_size(simix_global->process_list);
620 int SIMIX_process_get_PID(smx_process_t self){
627 int SIMIX_process_get_PPID(smx_process_t self){
634 void* SIMIX_process_self_get_data(smx_process_t self)
636 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
641 return SIMIX_process_get_data(self);
644 void SIMIX_process_self_set_data(smx_process_t self, void *data)
646 xbt_assert(self == SIMIX_process_self(), "This is not the current process");
648 SIMIX_process_set_data(self, data);
651 void* SIMIX_process_get_data(smx_process_t process)
653 return process->data;
656 void SIMIX_process_set_data(smx_process_t process, void *data)
658 process->data = data;
661 sg_host_t SIMIX_process_get_host(smx_process_t process)
663 return process->host;
666 xbt_main_func_t SIMIX_process_get_code(void){
667 return SIMIX_process_self()->code;
670 /* needs to be public and without simcall because it is called
671 by exceptions and logging events */
672 const char* SIMIX_process_self_get_name(void) {
674 smx_process_t process = SIMIX_process_self();
675 if (process == NULL || process == simix_global->maestro_process)
678 return SIMIX_process_get_name(process);
681 const char* SIMIX_process_get_name(smx_process_t process)
683 return process->name;
686 smx_process_t SIMIX_process_get_by_name(const char* name)
690 xbt_swag_foreach(proc, simix_global->process_list)
692 if(!strcmp(name, proc->name))
698 int SIMIX_process_is_suspended(smx_process_t process)
700 return process->suspended;
703 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
705 return process->properties;
708 void simcall_HANDLER_process_join(smx_simcall_t simcall, smx_process_t process, double timeout)
710 smx_synchro_t sync = SIMIX_process_join(simcall->issuer, process, timeout);
711 xbt_fifo_push(sync->simcalls, simcall);
712 simcall->issuer->waiting_synchro = sync;
715 static int SIMIX_process_join_finish(smx_process_exit_status_t status, smx_synchro_t sync){
716 if (sync->sleep.surf_sleep) {
717 surf_action_cancel(sync->sleep.surf_sleep);
719 smx_simcall_t simcall;
720 while ((simcall = xbt_fifo_shift(sync->simcalls))) {
721 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
722 simcall->issuer->waiting_synchro = NULL;
723 if (simcall->issuer->suspended) {
724 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
725 simcall->issuer->suspended = 0;
726 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
728 SIMIX_simcall_answer(simcall);
731 surf_action_unref(sync->sleep.surf_sleep);
732 sync->sleep.surf_sleep = NULL;
734 xbt_mallocator_release(simix_global->synchro_mallocator, sync);
738 smx_synchro_t SIMIX_process_join(smx_process_t issuer, smx_process_t process, double timeout)
740 smx_synchro_t res = SIMIX_process_sleep(issuer, timeout);
741 res->type = SIMIX_SYNC_JOIN;
742 SIMIX_process_on_exit(process, (int_f_pvoid_pvoid_t)SIMIX_process_join_finish, res);
746 void simcall_HANDLER_process_sleep(smx_simcall_t simcall, double duration)
748 if (MC_is_active() || MC_record_replay_is_active()) {
749 MC_process_clock_add(simcall->issuer, duration);
750 simcall_process_sleep__set__result(simcall, SIMIX_DONE);
751 SIMIX_simcall_answer(simcall);
754 smx_synchro_t sync = SIMIX_process_sleep(simcall->issuer, duration);
755 xbt_fifo_push(sync->simcalls, simcall);
756 simcall->issuer->waiting_synchro = sync;
759 smx_synchro_t SIMIX_process_sleep(smx_process_t process, double duration)
761 smx_synchro_t synchro;
762 sg_host_t host = process->host;
764 /* check if the host is active */
765 if (surf_host_get_state(surf_host_resource_priv(host)) != SURF_RESOURCE_ON) {
766 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
770 synchro = xbt_mallocator_get(simix_global->synchro_mallocator);
771 synchro->type = SIMIX_SYNC_SLEEP;
772 synchro->name = NULL;
773 synchro->category = NULL;
775 synchro->sleep.host = host;
776 synchro->sleep.surf_sleep = surf_host_sleep(host, duration);
778 surf_action_set_data(synchro->sleep.surf_sleep, synchro);
779 XBT_DEBUG("Create sleep synchronization %p", synchro);
784 void SIMIX_post_process_sleep(smx_synchro_t synchro)
786 smx_simcall_t simcall;
788 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
790 while ((simcall = xbt_fifo_shift(synchro->simcalls))) {
792 switch(surf_action_get_state(synchro->sleep.surf_sleep)){
793 case SURF_ACTION_FAILED:
794 simcall->issuer->context->iwannadie = 1;
795 //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
796 state = SIMIX_SRC_HOST_FAILURE;
799 case SURF_ACTION_DONE:
807 if (surf_host_get_state(surf_host_resource_priv(simcall->issuer->host)) != SURF_RESOURCE_ON) {
808 simcall->issuer->context->iwannadie = 1;
810 simcall_process_sleep__set__result(simcall, state);
811 simcall->issuer->waiting_synchro = NULL;
812 if (simcall->issuer->suspended) {
813 XBT_DEBUG("Wait! This process is suspended and can't wake up now.");
814 simcall->issuer->suspended = 0;
815 simcall_HANDLER_process_suspend(simcall, simcall->issuer);
817 SIMIX_simcall_answer(simcall);
821 SIMIX_process_sleep_destroy(synchro);
824 void SIMIX_process_sleep_destroy(smx_synchro_t synchro)
826 XBT_DEBUG("Destroy synchro %p", synchro);
827 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP || synchro->type == SIMIX_SYNC_JOIN);
829 if (synchro->sleep.surf_sleep) {
830 surf_action_unref(synchro->sleep.surf_sleep);
831 synchro->sleep.surf_sleep = NULL;
833 if (synchro->type == SIMIX_SYNC_SLEEP)
834 xbt_mallocator_release(simix_global->synchro_mallocator, synchro);
837 void SIMIX_process_sleep_suspend(smx_synchro_t synchro)
839 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
840 surf_action_suspend(synchro->sleep.surf_sleep);
843 void SIMIX_process_sleep_resume(smx_synchro_t synchro)
845 XBT_DEBUG("Synchro state is %d on process_sleep_resume.", synchro->state);
846 xbt_assert(synchro->type == SIMIX_SYNC_SLEEP);
847 surf_action_resume(synchro->sleep.surf_sleep);
851 * \brief Calling this function makes the process to yield.
853 * Only the current process can call this function, giving back the control to
856 * \param self the current process
858 void SIMIX_process_yield(smx_process_t self)
860 XBT_DEBUG("Yield process '%s'", self->name);
862 /* Go into sleep and return control to maestro */
863 SIMIX_context_suspend(self->context);
865 /* Ok, maestro returned control to us */
866 XBT_DEBUG("Control returned to me: '%s'", self->name);
868 if (self->new_host) {
869 SIMIX_process_change_host(self, self->new_host);
870 self->new_host = NULL;
873 if (self->context->iwannadie){
874 XBT_DEBUG("I wanna die!");
875 SIMIX_process_stop(self);
878 if (self->suspended) {
879 XBT_DEBUG("Hey! I'm suspended.");
880 xbt_assert(!self->doexception, "Gasp! This exception may be lost by subsequent calls.");
882 SIMIX_process_suspend(self, self);
885 if (self->doexception) {
886 XBT_DEBUG("Wait, maestro left me an exception");
887 self->doexception = 0;
891 if(SMPI_switch_data_segment && self->segment_index != -1){
892 SMPI_switch_data_segment(self->segment_index);
896 /* callback: context fetching */
897 xbt_running_ctx_t *SIMIX_process_get_running_context(void)
899 return SIMIX_process_self()->running_ctx;
902 /* callback: termination */
903 void SIMIX_process_exception_terminate(xbt_ex_t * e)
909 smx_context_t SIMIX_process_get_context(smx_process_t p) {
913 void SIMIX_process_set_context(smx_process_t p,smx_context_t c) {
918 * \brief Returns the list of processes to run.
920 xbt_dynar_t SIMIX_process_get_runnable(void)
922 return simix_global->process_to_run;
926 * \brief Returns the process from PID.
928 smx_process_t SIMIX_process_from_PID(int PID)
931 xbt_swag_foreach(proc, simix_global->process_list)
939 /** @brief returns a dynar containg all currently existing processes */
940 xbt_dynar_t SIMIX_processes_as_dynar(void) {
942 xbt_dynar_t res = xbt_dynar_new(sizeof(smx_process_t),NULL);
943 xbt_swag_foreach(proc, simix_global->process_list) {
944 xbt_dynar_push(res,&proc);
950 void SIMIX_process_on_exit_runall(smx_process_t process) {
951 s_smx_process_exit_fun_t exit_fun;
952 smx_process_exit_status_t exit_status = (process->context->iwannadie) ?
953 SMX_EXIT_FAILURE : SMX_EXIT_SUCCESS;
954 while (!xbt_dynar_is_empty(process->on_exit)) {
955 exit_fun = xbt_dynar_pop_as(process->on_exit,s_smx_process_exit_fun_t);
956 (exit_fun.fun)((void*)exit_status, exit_fun.arg);
960 void SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_pvoid_t fun, void *data) {
961 xbt_assert(process, "current process not found: are you in maestro context ?");
963 if (!process->on_exit) {
964 process->on_exit = xbt_dynar_new(sizeof(s_smx_process_exit_fun_t), NULL);
967 s_smx_process_exit_fun_t exit_fun = {fun, data};
969 xbt_dynar_push_as(process->on_exit,s_smx_process_exit_fun_t,exit_fun);
973 * \brief Sets the auto-restart status of the process.
974 * If set to 1, the process will be automatically restarted when its host
977 void SIMIX_process_auto_restart_set(smx_process_t process, int auto_restart) {
978 process->auto_restart = auto_restart;
981 smx_process_t simcall_HANDLER_process_restart(smx_simcall_t simcall, smx_process_t process) {
982 return SIMIX_process_restart(process, simcall->issuer);
984 /** @brief Restart a process, starting it again from the beginning. */
985 smx_process_t SIMIX_process_restart(smx_process_t process, smx_process_t issuer) {
986 XBT_DEBUG("Restarting process %s on %s", process->name, sg_host_name(process->host));
987 //retrieve the arguments of the old process
988 //FIXME: Factorize this with SIMIX_host_add_auto_restart_process ?
989 s_smx_process_arg_t arg;
990 arg.code = process->code;
991 arg.hostname = sg_host_name(process->host);
992 arg.kill_time = SIMIX_timer_get_date(process->kill_timer);
993 arg.argc = process->argc;
994 arg.data = process->data;
996 arg.argv = xbt_new(char*,process->argc + 1);
997 for (i = 0; i < arg.argc; i++) {
998 arg.argv[i] = xbt_strdup(process->argv[i]);
1000 arg.argv[process->argc] = NULL;
1001 arg.properties = NULL;
1002 arg.auto_restart = process->auto_restart;
1003 //kill the old process
1004 SIMIX_process_kill(process,issuer);
1005 //start the new process
1006 smx_process_t new_process;
1007 if (simix_global->create_process_function) {
1008 new_process = simix_global->create_process_function(
1020 new_process = simcall_process_create(
1035 void SIMIX_segment_index_set(smx_process_t proc, int index){
1036 proc->segment_index = index;