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 Move a process to the list of process to destroy. *
22 void SIMIX_process_cleanup(void *arg)
24 xbt_swag_remove(arg, simix_global->process_to_run);
25 xbt_swag_remove(arg, simix_global->process_list);
26 xbt_swag_remove(arg, ((smx_process_t)arg)->smx_host->process_list);
27 xbt_swag_insert(arg, simix_global->process_to_destroy);
33 * Should be called some time to time to free the memory allocated for processes
34 * that have finished (or killed).
36 void SIMIX_process_empty_trash(void)
38 smx_process_t process = NULL;
41 while ((process = xbt_swag_extract(simix_global->process_to_destroy))){
46 for (i = 0; i < process->argc; i++)
48 free(process->argv[i]);
58 * \brief Creates and runs the maestro process
62 void __SIMIX_create_maestro_process()
64 smx_process_t process = NULL;
65 process = xbt_new0(s_smx_process_t, 1);
68 process->name = (char *)"";
70 /*Create the right context type (FIXME: check the return value for success)*/
71 SIMIX_context_create_maestro(&process);
73 /* Set it as the maestro process */
74 simix_global->maestro_process = process;
75 simix_global->current_process = process;
81 * \brief Creates and runs a new #smx_process_t.
83 * 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.
85 * \param name a name for the object. It is for user-level information and can be NULL.
86 * \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.
87 * \param host the location where the new agent is executed.
88 * \param argc first argument passed to \a code
89 * \param argv second argument passed to \a code
90 * \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.
92 * \return The new corresponding object.
94 smx_process_t SIMIX_process_create(const char *name,
95 xbt_main_func_t code, void *data,
96 const char *hostname, int argc,
97 char **argv, xbt_dict_t properties)
99 smx_process_t process = NULL;
100 smx_host_t host = SIMIX_host_get_by_name(hostname);
102 DEBUG2("Start process %s on host %s", name, hostname);
104 if (!SIMIX_host_get_state(host)) {
105 WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
108 process = xbt_new0(s_smx_process_t, 1);
110 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
113 process->name = xbt_strdup(name);
114 process->smx_host = host;
115 process->argc = argc;
116 process->argv = argv;
117 process->mutex = NULL;
118 process->cond = NULL;
120 /*Create the right context type (FIXME: check the return value for success)*/
121 SIMIX_context_new(&process, code);
123 process->data = data;
124 process->cleanup_func = simix_global->cleanup_process_function;
125 process->cleanup_arg = process;
128 process->properties = properties;
130 /* Add the process to it's host process list */
131 xbt_swag_insert(process, host->process_list);
133 SIMIX_context_start(process);
135 /* Now insert it in the global process list and in the process to run list */
136 xbt_swag_insert(process, simix_global->process_list);
137 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
138 xbt_swag_insert(process, simix_global->process_to_run);
144 * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
146 * Warning: this should only be used in libsimgrid4java, since it create
147 * a context with no code, which leads to segfaults in plain libsimgrid
149 void SIMIX_jprocess_create(const char *name, smx_host_t host,
151 void *jprocess, void *jenv, smx_process_t * res)
153 smx_process_t process = xbt_new0(s_smx_process_t, 1);
154 smx_process_t self = NULL;
156 /* HACK: We need this trick because when we xbt_context_new() do
157 syncronization stuff, the s_process field in the m_process needs
158 to have a valid value, and we call xbt_context_new() before
159 returning, of course, ie, before providing a right value to the
160 caller (Java_simgrid_msg_Msg_processCreate) have time to store it
161 in place. This way, we initialize the m_process->simdata->s_process
164 All this would be much simpler if the synchronization stuff would be done
165 in the JAVA world, I think.
169 DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
170 name, host, data, jprocess, jenv);
171 xbt_assert0(host, "Invalid parameters");
174 process->name = xbt_strdup(name);
175 process->smx_host = host;
177 process->argv = NULL;
178 process->mutex = NULL;
179 process->cond = NULL;
180 SIMIX_context_new(&process, jprocess);
181 process->data = data;
183 /* Add the process to it's host process list */
184 xbt_swag_insert(&process, host->process_list);
186 /* fix current_process, about which xbt_context_start mocks around */
187 self = simix_global->current_process;
188 SIMIX_context_start(process);
189 simix_global->current_process = self;
191 /* Now insert it in the global process list and in the process to run list */
192 xbt_swag_insert(process, simix_global->process_list);
193 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
194 xbt_swag_insert(process, simix_global->process_to_run);
197 /** \brief Kill a SIMIX process
199 * This function simply kills a \a process... scarry isn't it ? :).
200 * \param process poor victim
203 void SIMIX_process_kill(smx_process_t process)
205 DEBUG2("Killing process %s on %s", process->name, process->smx_host->name);
207 /* Cleanup if we were waiting for something */
209 xbt_swag_remove(process, process->mutex->sleeping);
212 xbt_swag_remove(process, process->cond->sleeping);
214 DEBUG2("%p here! killing %p", simix_global->current_process, process);
216 /*FIXME: If we are killing the running process we might call stop directly */
217 process->iwannadie = 1;
218 __SIMIX_process_schedule(process);
220 if (process == SIMIX_process_self()) {
221 /* I just killed myself */
222 SIMIX_context_yield();
227 * \brief Return the user data of a #smx_process_t.
229 * 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.
230 * \param process SIMIX process
231 * \return A void pointer to the user data
233 void *SIMIX_process_get_data(smx_process_t process)
235 xbt_assert0((process != NULL), "Invalid parameters");
236 return (process->data);
240 * \brief Set the user data of a #m_process_t.
242 * 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.
243 * \param process SIMIX process
244 * \param data User data
246 void SIMIX_process_set_data(smx_process_t process, void *data)
248 xbt_assert0((process != NULL), "Invalid parameters");
250 process->data = data;
255 * \brief Return the location on which an agent is running.
257 * 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.
258 * \param process SIMIX process
261 smx_host_t SIMIX_process_get_host(smx_process_t process)
263 xbt_assert0((process != NULL), "Invalid parameters");
264 return (process->smx_host);
268 * \brief Return the name of an agent.
270 * This functions checks whether \a process is a valid pointer or not and return its name.
271 * \param process SIMIX process
272 * \return The process name
274 const char *SIMIX_process_get_name(smx_process_t process)
276 xbt_assert0((process != NULL), "Invalid parameters");
277 return (process->name);
281 * \brief Changes the name of an agent.
283 * This functions checks whether \a process is a valid pointer or not and return its name.
284 * \param process SIMIX process
285 * \param name The new process name
287 void SIMIX_process_set_name(smx_process_t process, char *name)
289 xbt_assert0((process != NULL), "Invalid parameters");
290 process->name = name;
293 /** \ingroup m_process_management
294 * \brief Return the properties
296 * This functions returns the properties associated with this process
298 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
300 return process->properties;
304 * \brief Return the current agent.
306 * This functions returns the currently running #smx_process_t.
307 * \return The SIMIX process
309 smx_process_t SIMIX_process_self(void)
311 return simix_global ? simix_global->current_process : NULL;
315 * \brief Suspend the process.
317 * This functions suspend the process by suspending the action on
318 * which it was waiting for the completion.
320 * \param process SIMIX process
322 void SIMIX_process_suspend(smx_process_t process)
324 xbt_assert0(process, "Invalid parameters");
326 if (process != SIMIX_process_self()) {
328 if (process->mutex) {
329 /* process blocked on a mutex, only set suspend=1 */
330 process->suspended = 1;
331 } else if (process->cond) {
332 /* process blocked cond, suspend all actions */
334 /* temporaries variables */
339 process->suspended = 1;
341 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
342 surf_workstation_model->suspend(act->surf_action);
345 process->suspended = 1;
348 /* process executing, I can create an action and suspend it */
351 char name[] = "dummy";
352 process->suspended = 1;
354 cond = SIMIX_cond_init();
355 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
356 surf_workstation_model->suspend(dummy->surf_action);
357 SIMIX_register_action_to_condition(dummy, cond);
358 __SIMIX_cond_wait(cond);
359 SIMIX_unregister_action_to_condition(dummy, cond);
360 SIMIX_action_destroy(dummy);
361 SIMIX_cond_destroy(cond);
367 * \brief Resume a suspended process.
369 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
370 * \param process SIMIX process
372 void SIMIX_process_resume(smx_process_t process)
374 xbt_assert0((process != NULL), "Invalid parameters");
377 if (process == SIMIX_process_self())
380 if (process->mutex) {
381 DEBUG0("Resume process blocked on a mutex");
382 process->suspended = 0; /* It'll wake up by itself when mutex releases */
384 } else if (process->cond) {
385 /* temporaries variables */
389 DEBUG0("Resume process blocked on a conditional");
390 process->suspended = 0;
392 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
393 surf_workstation_model->resume(act->surf_action);
395 SIMIX_cond_signal(c);
398 process->suspended = 0;
399 xbt_swag_insert(process, simix_global->process_to_run);
404 * \brief Migrates an agent to another location.
406 * This function changes the value of the host on which \a process is running.
408 void SIMIX_process_change_host(smx_process_t process, char *source, char *dest)
410 xbt_assert0((process != NULL), "Invalid parameters");
411 smx_host_t h1 = SIMIX_host_get_by_name(source);
412 smx_host_t h2 = SIMIX_host_get_by_name(dest);
413 process->smx_host = h2;
414 xbt_swag_remove(process, h1->process_list);
415 xbt_swag_insert(process, h2->process_list);
419 * \brief Returns true if the process is suspended .
421 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
422 * \param process SIMIX process
423 * \return 1, if the process is suspended, else 0.
425 int SIMIX_process_is_suspended(smx_process_t process)
427 xbt_assert0((process != NULL), "Invalid parameters");
429 return (process->suspended);
433 * \brief Returns the amount of SIMIX processes in the system
435 * Maestro internal process is not counted, only user code processes are
437 int SIMIX_process_count()
439 return xbt_swag_size(simix_global->process_list);
443 * Calling this function makes the process process to yield. The process
444 * that scheduled it returns from __SIMIX_process_schedule as if nothing
447 * Only the processes can call this function, giving back the control
450 void __SIMIX_process_yield(void)
452 DEBUG1("Yield process '%s'", simix_global->current_process->name);
453 xbt_assert0((simix_global->current_process != simix_global->maestro_process),
454 "You are not supposed to run this function here!");
456 SIMIX_context_yield();
458 if (simix_global->current_process->iwannadie)
459 SIMIX_context_stop(1);
462 void __SIMIX_process_schedule(smx_process_t process)
464 DEBUG1("Scheduling context: '%s'", process->name);
466 /* save the current process */
467 smx_process_t self = simix_global->current_process;
469 /* update the current process */
470 simix_global->current_process = process;
472 /* schedule the context */
473 SIMIX_context_schedule(process);
475 /* restore the current process to the previously saved process */
476 simix_global->current_process = self;