1 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
8 #include "xbt/sysdep.h"
11 #include "msg/mailbox.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14 "Logging specific to SIMIX (process)");
17 * \brief Move a process to the list of process to destroy. *
19 void SIMIX_process_cleanup(void *arg)
21 xbt_swag_remove(arg, simix_global->process_to_run);
22 xbt_swag_remove(arg, simix_global->process_list);
23 xbt_swag_remove(arg, ((smx_process_t) arg)->smx_host->process_list);
24 xbt_swag_insert(arg, simix_global->process_to_destroy);
30 * Should be called some time to time to free the memory allocated for processes
31 * that have finished (or killed).
33 void SIMIX_process_empty_trash(void)
35 smx_process_t process = NULL;
37 while ((process = xbt_swag_extract(simix_global->process_to_destroy))) {
38 SIMIX_context_free(process->context);
40 /* Free the exception allocated at creation time */
41 if (process->exception)
42 free(process->exception);
43 if (process->properties)
44 xbt_dict_free(&process->properties);
53 * \brief Creates and runs the maestro process
55 void SIMIX_create_maestro_process()
57 smx_process_t process = NULL;
58 process = xbt_new0(s_smx_process_t, 1);
61 process->name = (char *) "";
63 process->exception = xbt_new(ex_ctx_t, 1);
64 XBT_CTX_INITIALIZE(process->exception);
66 /* Create a dummy context for maestro */
67 process->context = SIMIX_context_new(NULL, 0, NULL, NULL, NULL);
69 /* Set it as the maestro process */
70 simix_global->maestro_process = process;
71 simix_global->current_process = process;
77 * \brief Creates and runs a new #smx_process_t.
79 * A constructor for #m_process_t taking four arguments and returning the corresponding object. The structure (and the corresponding thread) is created, and put in the list of ready process.
81 * \param name a name for the object. It is for user-level information and can be NULL.
82 * \param data a pointer to any data one may want to attach to the new object. It is for user-level information and can be NULL. It can be retrieved with the function \ref MSG_process_get_data.
83 * \param host the location where the new agent is executed.
84 * \param argc first argument passed to \a code
85 * \param argv second argument passed to \a code
86 * \param clean_process_function The cleanup function of user process. It will be called when the process finish. This function have to call the SIMIX_process_cleanup.
88 * \return The new corresponding object.
90 smx_process_t SIMIX_process_create(const char *name,
91 xbt_main_func_t code, void *data,
92 const char *hostname, int argc,
93 char **argv, xbt_dict_t properties)
95 smx_process_t process = NULL;
96 smx_host_t host = SIMIX_host_get_by_name(hostname);
98 DEBUG2("Start process %s on host %s", name, hostname);
100 if (!SIMIX_host_get_state(host)) {
101 WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
104 process = xbt_new0(s_smx_process_t, 1);
106 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
109 process->name = xbt_strdup(name);
110 process->smx_host = host;
111 process->mutex = NULL;
112 process->cond = NULL;
113 process->iwannadie = 0;
114 process->data = data;
116 VERB1("Create context %s", process->name);
117 process->context = SIMIX_context_new(code, argc, argv,
118 simix_global->cleanup_process_function,
121 process->exception = xbt_new(ex_ctx_t, 1);
122 XBT_CTX_INITIALIZE(process->exception);
125 process->properties = properties;
127 /* Add the process to it's host process list */
128 xbt_swag_insert(process, host->process_list);
130 DEBUG1("Start context '%s'", process->name);
132 /* Now insert it in the global process list and in the process to run list */
133 xbt_swag_insert(process, simix_global->process_list);
134 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
135 xbt_swag_insert(process, simix_global->process_to_run);
140 /** \brief Kill a SIMIX process
142 * This function simply kills a \a process... scarry isn't it ? :).
143 * \param process poor victim
146 void SIMIX_process_kill(smx_process_t process)
148 DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
150 process->iwannadie = 1;
152 /* If I'm killing myself then stop otherwise schedule the process to kill
153 * Two different behaviors, if I'm killing my self, remove from mutex and condition and stop. Otherwise, first we must schedule the process, wait its ending and after remove it from mutex and condition */
154 if (process == SIMIX_process_self()) {
155 /* Cleanup if we were waiting for something */
157 xbt_swag_remove(process, process->mutex->sleeping);
160 xbt_swag_remove(process, process->cond->sleeping);
161 if (process->waiting_action) {
162 SIMIX_unregister_action_to_condition(process->waiting_action, process->cond);
163 SIMIX_action_destroy(process->waiting_action);
167 xbt_fifo_remove(process->sem->sleeping, process);
169 if (process->waiting_action) {
170 SIMIX_unregister_action_to_semaphore(process->waiting_action, process->sem);
171 SIMIX_action_destroy(process->waiting_action);
175 SIMIX_context_stop(process->context);
178 DEBUG4("%s(%p) here! killing %s(%p)",
179 simix_global->current_process->name,simix_global->current_process,
180 process->name,process);
182 /* Cleanup if it were waiting for something */
183 if (process->mutex) {
184 xbt_swag_remove(process, process->mutex->sleeping);
185 process->mutex = NULL;
189 xbt_swag_remove(process, process->cond->sleeping);
191 if (process->waiting_action) {
192 SIMIX_unregister_action_to_condition(process->waiting_action, process->cond);
193 SIMIX_action_destroy(process->waiting_action);
195 process->cond = NULL;
199 xbt_fifo_remove(process->sem->sleeping, process);
201 if (process->waiting_action) {
202 SIMIX_unregister_action_to_semaphore(process->waiting_action, process->sem);
203 SIMIX_action_destroy(process->waiting_action);
208 /* make sure that the process gets awake soon enough, now that we've set its iwannadie to 1 */
209 process->blocked = 0;
210 process->suspended = 0;
211 xbt_swag_insert(process, simix_global->process_to_run);
216 * \brief Return the user data of a #smx_process_t.
218 * This functions checks whether \a process is a valid pointer or not and return the user data associated to \a process if it is possible.
219 * \param process SIMIX process
220 * \return A void pointer to the user data
222 XBT_INLINE void *SIMIX_process_get_data(smx_process_t process)
224 xbt_assert0((process != NULL), "Invalid parameters");
225 return (process->data);
229 * \brief Set the user data of a #m_process_t.
231 * This functions checks whether \a process is a valid pointer or not and set the user data associated to \a process if it is possible.
232 * \param process SIMIX process
233 * \param data User data
235 XBT_INLINE void SIMIX_process_set_data(smx_process_t process, void *data)
237 xbt_assert0((process != NULL), "Invalid parameters");
239 process->data = data;
244 * \brief Return the location on which an agent is running.
246 * This functions checks whether \a process is a valid pointer or not and return the m_host_t corresponding to the location on which \a process is running.
247 * \param process SIMIX process
250 XBT_INLINE smx_host_t SIMIX_process_get_host(smx_process_t process)
252 xbt_assert0((process != NULL), "Invalid parameters");
253 return (process->smx_host);
257 * \brief Return the name of an agent.
259 * This functions checks whether \a process is a valid pointer or not and return its name.
260 * \param process SIMIX process
261 * \return The process name
263 XBT_INLINE const char *SIMIX_process_get_name(smx_process_t process)
265 xbt_assert0((process != NULL), "Invalid parameters");
266 return (process->name);
270 * \brief Changes the name of an agent.
272 * This functions checks whether \a process is a valid pointer or not and return its name.
273 * \param process SIMIX process
274 * \param name The new process name
276 XBT_INLINE void SIMIX_process_set_name(smx_process_t process, char *name)
278 xbt_assert0((process != NULL), "Invalid parameters");
279 process->name = name;
282 /** \ingroup m_process_management
283 * \brief Return the properties
285 * This functions returns the properties associated with this process
287 XBT_INLINE xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
289 return process->properties;
293 * \brief Return the current agent.
295 * This functions returns the currently running #smx_process_t.
296 * \return The SIMIX process
298 XBT_INLINE smx_process_t SIMIX_process_self(void)
301 return simix_global->current_process;
306 * \brief Suspend the process.
308 * This functions suspend the process by suspending the action on
309 * which it was waiting for the completion.
311 * \param process SIMIX process
313 void SIMIX_process_suspend(smx_process_t process)
315 xbt_assert0(process, "Invalid parameters");
317 if (process != SIMIX_process_self()) {
319 if (process->mutex) {
320 /* process blocked on a mutex or sem, only set suspend=1 */
321 process->suspended = 1;
322 } else if (process->cond) {
323 /* process blocked cond, suspend all actions */
325 /* temporaries variables */
330 process->suspended = 1;
332 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
333 SIMIX_action_suspend(act);
335 } else if (process->sem) {
340 process->suspended = 1;
342 xbt_fifo_foreach(s->actions, i, act, smx_action_t) {
343 SIMIX_action_suspend(act);
346 process->suspended = 1;
349 /* process executing, I can create an action and suspend it */
352 char name[] = "dummy";
353 process->suspended = 1;
355 cond = SIMIX_cond_init();
356 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
357 SIMIX_process_self()->waiting_action = dummy;
358 SIMIX_action_suspend(dummy);
359 SIMIX_register_action_to_condition(dummy, cond);
360 __SIMIX_cond_wait(cond);
361 SIMIX_process_self()->waiting_action = NULL;
362 SIMIX_unregister_action_to_condition(dummy, cond);
363 SIMIX_action_destroy(dummy);
364 SIMIX_cond_destroy(cond);
370 * \brief Resume a suspended process.
372 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
373 * \param process SIMIX process
375 void SIMIX_process_resume(smx_process_t process)
377 xbt_assert0((process != NULL), "Invalid parameters");
380 if (process == SIMIX_process_self())
383 if (process->mutex) {
384 DEBUG0("Resume process blocked on a mutex or semaphore");
385 process->suspended = 0; /* It'll wake up by itself when mutex releases */
387 } else if (process->cond) {
388 /* temporaries variables */
392 DEBUG0("Resume process blocked on a conditional");
393 process->suspended = 0;
395 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
396 SIMIX_action_resume(act);
398 SIMIX_cond_signal(c);
400 } else if (process->sem) {
401 /* temporaries variables */
405 DEBUG0("Resume process blocked on a semaphore");
406 process->suspended = 0;
408 xbt_fifo_foreach(s->actions, i, act, smx_action_t) {
409 SIMIX_action_resume(act);
413 process->suspended = 0;
414 xbt_swag_insert(process, simix_global->process_to_run);
419 * \brief Migrates an agent to another location.
421 * This function changes the value of the host on which \a process is running.
423 void SIMIX_process_change_host(smx_process_t process, char *source,
426 smx_host_t h1 = NULL;
427 smx_host_t h2 = NULL;
428 xbt_assert0((process != NULL), "Invalid parameters");
429 h1 = SIMIX_host_get_by_name(source);
430 h2 = SIMIX_host_get_by_name(dest);
431 process->smx_host = h2;
432 xbt_swag_remove(process, h1->process_list);
433 xbt_swag_insert(process, h2->process_list);
437 * \brief Returns true if the process is suspended .
439 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
440 * \param process SIMIX process
441 * \return 1, if the process is suspended, else 0.
443 XBT_INLINE int SIMIX_process_is_suspended(smx_process_t process)
445 xbt_assert0((process != NULL), "Invalid parameters");
447 return (process->suspended);
451 * \brief Returns the amount of SIMIX processes in the system
453 * Maestro internal process is not counted, only user code processes are
455 XBT_INLINE int SIMIX_process_count()
457 return xbt_swag_size(simix_global->process_list);
461 * Calling this function makes the process process to yield. The process
462 * that scheduled it returns from __SIMIX_process_schedule as if nothing
465 * Only the processes can call this function, giving back the control
468 void SIMIX_process_yield(void)
470 DEBUG1("Yield process '%s'", simix_global->current_process->name);
471 xbt_assert0((simix_global->current_process !=
472 simix_global->maestro_process),
473 "You are not supposed to run this function in maestro context!");
476 /* Go into sleep and return control to maestro */
477 SIMIX_context_suspend(simix_global->current_process->context);
478 /* Ok, maestro returned control to us */
480 if (simix_global->current_process->iwannadie)
481 SIMIX_context_stop(simix_global->current_process->context);
484 void SIMIX_process_schedule(smx_process_t new_process)
486 xbt_assert0(simix_global->current_process == simix_global->maestro_process,
487 "This function can only be called from maestro context");
488 DEBUG1("Scheduling context: '%s'", new_process->name);
490 /* update the current process */
491 simix_global->current_process = new_process;
493 /* schedule the context */
494 SIMIX_context_resume(new_process->context);
495 DEBUG1("Resumed from scheduling context: '%s'", new_process->name);
497 /* restore the current process to the previously saved process */
498 simix_global->current_process = simix_global->maestro_process;
501 /* callback: context fetching */
502 ex_ctx_t *SIMIX_process_get_exception(void)
504 return simix_global->current_process->exception;
507 /* callback: termination */
508 void SIMIX_process_exception_terminate(xbt_ex_t * e)