3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "xbt/sysdep.h"
12 #include "msg/mailbox.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
15 "Logging specific to SIMIX (process)");
17 /******************************** Process ************************************/
19 * \brief Creates and runs a new #smx_process_t.
21 * Does exactly the same as #SIMIX_process_create_with_arguments but without
22 providing standard arguments (\a argc, \a argv).
23 * \see SIMIX_process_create_with_arguments
26 void SIMIX_process_cleanup(void *arg)
28 xbt_swag_remove(arg, simix_global->process_to_run);
29 xbt_swag_remove(arg, simix_global->process_list);
30 xbt_swag_remove(arg, ((smx_process_t)arg)->smx_host->process_list);
31 xbt_swag_insert(arg, simix_global->process_to_destroy);
34 /** Garbage collection
36 * Should be called some time to time to free the memory allocated for processes
37 * that have finished (or killed).
39 void SIMIX_process_empty_trash(void)
41 smx_process_t process = NULL;
44 while ((process = xbt_swag_extract(simix_global->process_to_destroy))){
49 for (i = 0; i < process->argc; i++)
51 free(process->argv[i]);
61 * \brief Creates and runs the maestro process
65 void __SIMIX_create_maestro_process()
67 smx_process_t process = NULL;
68 process = xbt_new0(s_smx_process_t, 1);
71 process->name = (char *)"";
73 /*Create the right context type (FIXME: check the return value for success)*/
74 SIMIX_context_create_maestro(&process);
76 /* Set it as the maestro process */
77 simix_global->maestro_process = process;
78 simix_global->current_process = process;
84 * \brief Creates and runs a new #smx_process_t.
86 * 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.
88 * \param name a name for the object. It is for user-level information and can be NULL.
89 * \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.
90 * \param host the location where the new agent is executed.
91 * \param argc first argument passed to \a code
92 * \param argv second argument passed to \a code
93 * \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.
95 * \return The new corresponding object.
97 smx_process_t SIMIX_process_create(const char *name,
98 xbt_main_func_t code, void *data,
99 const char *hostname, int argc,
100 char **argv, xbt_dict_t properties)
102 smx_process_t process = NULL;
103 smx_process_t self = NULL;
104 smx_host_t host = SIMIX_host_get_by_name(hostname);
106 DEBUG2("Start process %s on host %s", name, hostname);
108 if (!SIMIX_host_get_state(host)) {
109 WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
112 process = xbt_new0(s_smx_process_t, 1);
114 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
117 process->name = xbt_strdup(name);
118 process->smx_host = host;
119 process->argc = argc;
120 process->argv = argv;
121 process->mutex = NULL;
122 process->cond = NULL;
124 /*Create the right context type (FIXME: check the return value for success)*/
125 SIMIX_context_new(&process, code);
127 process->data = data;
128 process->cleanup_func = simix_global->cleanup_process_function;
129 process->cleanup_arg = process;
132 process->properties = properties;
134 /* Add the process to it's host process list */
135 xbt_swag_insert(process, host->process_list);
137 /* fix current_process, about which SIMIX_context_start mocks around */
138 self = simix_global->current_process;
139 SIMIX_context_start(process);
140 simix_global->current_process = self;
142 /* Now insert it in the global process list and in the process to run list */
143 xbt_swag_insert(process, simix_global->process_list);
144 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
145 xbt_swag_insert(process, simix_global->process_to_run);
151 * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
153 * Warning: this should only be used in libsimgrid4java, since it create
154 * a context with no code, which leads to segfaults in plain libsimgrid
156 void SIMIX_jprocess_create(const char *name, smx_host_t host,
158 void *jprocess, void *jenv, smx_process_t * res)
160 smx_process_t process = xbt_new0(s_smx_process_t, 1);
161 smx_process_t self = NULL;
163 /* HACK: We need this trick because when we xbt_context_new() do
164 syncronization stuff, the s_process field in the m_process needs
165 to have a valid value, and we call xbt_context_new() before
166 returning, of course, ie, before providing a right value to the
167 caller (Java_simgrid_msg_Msg_processCreate) have time to store it
168 in place. This way, we initialize the m_process->simdata->s_process
171 All this would be much simpler if the synchronization stuff would be done
172 in the JAVA world, I think.
176 DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
177 name, host, data, jprocess, jenv);
178 xbt_assert0(host, "Invalid parameters");
181 process->name = xbt_strdup(name);
182 process->smx_host = host;
184 process->argv = NULL;
185 process->mutex = NULL;
186 process->cond = NULL;
187 SIMIX_context_new(&process, jprocess);
188 process->data = data;
190 /* Add the process to it's host process list */
191 xbt_swag_insert(&process, host->process_list);
193 /* fix current_process, about which xbt_context_start mocks around */
194 self = simix_global->current_process;
195 SIMIX_context_start(process);
196 simix_global->current_process = self;
198 /* Now insert it in the global process list and in the process to run list */
199 xbt_swag_insert(process, simix_global->process_list);
200 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
201 xbt_swag_insert(process, simix_global->process_to_run);
204 /** \brief Kill a SIMIX process
206 * This function simply kills a \a process... scarry isn't it ? :).
207 * \param process poor victim
210 void SIMIX_process_kill(smx_process_t process)
212 DEBUG2("Killing process %s on %s", process->name,
213 process->smx_host->name);
215 /* Cleanup if we were waiting for something */
217 xbt_swag_remove(process, process->mutex->sleeping);
220 xbt_swag_remove(process, process->cond->sleeping);
222 DEBUG2("%p here! killing %p", simix_global->current_process, process);
223 SIMIX_context_kill(process);
225 if (process == SIMIX_process_self()) {
226 /* I just killed myself */
227 SIMIX_context_yield();
232 * \brief Return the user data of a #smx_process_t.
234 * 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.
235 * \param process SIMIX process
236 * \return A void pointer to the user data
238 void *SIMIX_process_get_data(smx_process_t process)
240 xbt_assert0((process != NULL), "Invalid parameters");
241 return (process->data);
245 * \brief Set the user data of a #m_process_t.
247 * 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.
248 * \param process SIMIX process
249 * \param data User data
251 void SIMIX_process_set_data(smx_process_t process, void *data)
253 xbt_assert0((process != NULL), "Invalid parameters");
255 process->data = data;
260 * \brief Return the location on which an agent is running.
262 * 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.
263 * \param process SIMIX process
266 smx_host_t SIMIX_process_get_host(smx_process_t process)
268 xbt_assert0((process != NULL), "Invalid parameters");
269 return (process->smx_host);
273 * \brief Return the name of an agent.
275 * This functions checks whether \a process is a valid pointer or not and return its name.
276 * \param process SIMIX process
277 * \return The process name
279 const char *SIMIX_process_get_name(smx_process_t process)
281 xbt_assert0((process != NULL), "Invalid parameters");
282 return (process->name);
286 * \brief Changes the name of an agent.
288 * This functions checks whether \a process is a valid pointer or not and return its name.
289 * \param process SIMIX process
290 * \param name The new process name
292 void SIMIX_process_set_name(smx_process_t process, char *name)
294 xbt_assert0((process != NULL), "Invalid parameters");
295 process->name = name;
298 /** \ingroup m_process_management
299 * \brief Return the properties
301 * This functions returns the properties associated with this process
303 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
305 return process->properties;
309 * \brief Return the current agent.
311 * This functions returns the currently running #smx_process_t.
312 * \return The SIMIX process
314 smx_process_t SIMIX_process_self(void)
316 return simix_global ? simix_global->current_process : NULL;
320 * \brief Suspend the process.
322 * This functions suspend the process by suspending the action on
323 * which it was waiting for the completion.
325 * \param process SIMIX process
327 void SIMIX_process_suspend(smx_process_t process)
329 xbt_assert0(process, "Invalid parameters");
331 if (process != SIMIX_process_self()) {
333 if (process->mutex) {
334 /* process blocked on a mutex, only set suspend=1 */
335 process->suspended = 1;
336 } else if (process->cond) {
337 /* process blocked cond, suspend all actions */
339 /* temporaries variables */
344 process->suspended = 1;
346 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
347 surf_workstation_model->suspend(act->surf_action);
350 process->suspended = 1;
353 /* process executing, I can create an action and suspend it */
356 char name[] = "dummy";
357 process->suspended = 1;
359 cond = SIMIX_cond_init();
360 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
361 surf_workstation_model->suspend(dummy->surf_action);
362 SIMIX_register_action_to_condition(dummy, cond);
363 __SIMIX_cond_wait(cond);
364 SIMIX_unregister_action_to_condition(dummy, cond);
365 SIMIX_action_destroy(dummy);
366 SIMIX_cond_destroy(cond);
372 * \brief Resume a suspended process.
374 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
375 * \param process SIMIX process
377 void SIMIX_process_resume(smx_process_t process)
379 xbt_assert0((process != NULL), "Invalid parameters");
382 if (process == SIMIX_process_self())
385 if (process->mutex) {
386 DEBUG0("Resume process blocked on a mutex");
387 process->suspended = 0; /* It'll wake up by itself when mutex releases */
389 } else if (process->cond) {
390 /* temporaries variables */
394 DEBUG0("Resume process blocked on a conditional");
395 process->suspended = 0;
397 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
398 surf_workstation_model->resume(act->surf_action);
400 SIMIX_cond_signal(c);
403 process->suspended = 0;
404 xbt_swag_insert(process, simix_global->process_to_run);
409 * \brief Migrates an agent to another location.
411 * This function changes the value of the host on which \a process is running.
413 void SIMIX_process_change_host(smx_process_t process, char *source, char *dest)
415 xbt_assert0((process != NULL), "Invalid parameters");
416 smx_host_t h1 = SIMIX_host_get_by_name(source);
417 smx_host_t h2 = SIMIX_host_get_by_name(dest);
418 process->smx_host = h2;
419 xbt_swag_remove(process, h1->process_list);
420 xbt_swag_insert(process, h2->process_list);
424 * \brief Returns true if the process is suspended .
426 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
427 * \param process SIMIX process
428 * \return 1, if the process is suspended, else 0.
430 int SIMIX_process_is_suspended(smx_process_t process)
432 xbt_assert0((process != NULL), "Invalid parameters");
434 return (process->suspended);
438 * \brief Returns the amount of SIMIX processes in the system
440 * Maestro internal process is not counted, only user code processes are
442 int SIMIX_process_count()
444 return xbt_swag_size(simix_global->process_list);