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
27 void SIMIX_process_cleanup(void *arg)
29 xbt_swag_remove(arg, simix_global->process_list);
30 xbt_swag_remove(arg, simix_global->process_to_run);
32 ((smx_process_t) arg)->simdata->smx_host->simdata->
34 free(((smx_process_t) arg)->name);
35 ((smx_process_t) arg)->name = NULL;
37 free(((smx_process_t) arg)->simdata);
38 ((smx_process_t) arg)->simdata = NULL;
43 * \brief Creates and runs a new #smx_process_t.
45 * 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.
47 * \param name a name for the object. It is for user-level information and can be NULL.
48 * \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.
49 * \param host the location where the new agent is executed.
50 * \param argc first argument passed to \a code
51 * \param argv second argument passed to \a code
52 * \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.
54 * \return The new corresponding object.
56 smx_process_t SIMIX_process_create(const char *name,
57 xbt_main_func_t code, void *data,
58 const char *hostname, int argc,
59 char **argv, xbt_dict_t properties)
61 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
62 smx_process_t process = xbt_new0(s_smx_process_t, 1);
63 smx_process_t self = NULL;
64 smx_host_t host = SIMIX_host_get_by_name(hostname);
65 /*char alias[MAX_ALIAS_NAME + 1] = {0};
66 msg_mailbox_t mailbox;*/
68 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
71 simdata->smx_host = host;
72 simdata->mutex = NULL;
76 simdata->context = xbt_context_new(name,code, NULL, NULL,
78 cleanup_process_function, process,
79 simdata->argc, simdata->argv);
81 /* Process structure */
82 process->name = xbt_strdup(name);
83 process->simdata = simdata;
87 simdata->properties = properties;
89 xbt_swag_insert(process, host->simdata->process_list);
91 /* fix current_process, about which xbt_context_start mocks around */
92 self = simix_global->current_process;
93 xbt_context_start(process->simdata->context);
94 simix_global->current_process = self;
96 xbt_swag_insert(process, simix_global->process_list);
97 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
98 xbt_swag_insert(process, simix_global->process_to_run);
100 /*sprintf(alias,"%s:%s",hostname,process->name);
102 mailbox = MSG_mailbox_new(alias);
103 MSG_mailbox_set_hostname(mailbox, hostname);*/
108 * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
110 * Warning: this should only be used in libsimgrid4java, since it create
111 * a context with no code, which leads to segfaults in plain libsimgrid
113 void SIMIX_jprocess_create(const char *name, smx_host_t host,
115 void *jprocess, void *jenv, smx_process_t * res)
117 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
118 smx_process_t process = xbt_new0(s_smx_process_t, 1);
119 smx_process_t self = NULL;
121 /* HACK: We need this trick because when we xbt_context_new() do
122 syncronization stuff, the s_process field in the m_process needs
123 to have a valid value, and we call xbt_context_new() before
124 returning, of course, ie, before providing a right value to the
125 caller (Java_simgrid_msg_Msg_processCreate) have time to store it
126 in place. This way, we initialize the m_process->simdata->s_process
129 All this would be much simpler if the synchronization stuff would be done
130 in the JAVA world, I think.
136 DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
137 name, host, data, jprocess, jenv);
138 xbt_assert0(host, "Invalid parameters");
140 simdata->smx_host = host;
141 simdata->mutex = NULL;
142 simdata->cond = NULL;
144 simdata->argv = NULL;
147 simdata->context = xbt_context_new(name,NULL, NULL, jprocess,
149 cleanup_process_function, process,
150 /* argc/argv */ 0, NULL);
152 /* Process structure */
153 process->name = xbt_strdup(name);
154 process->simdata = simdata;
155 process->data = data;
157 xbt_swag_insert(process, host->simdata->process_list);
159 /* fix current_process, about which xbt_context_start mocks around */
160 self = simix_global->current_process;
162 xbt_context_start(process->simdata->context);
164 simix_global->current_process = self;
166 xbt_swag_insert(process, simix_global->process_list);
167 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
168 xbt_swag_insert(process, simix_global->process_to_run);
173 /** \brief Kill a SIMIX process
175 * This function simply kills a \a process... scarry isn't it ? :).
176 * \param process poor victim
179 void SIMIX_process_kill(smx_process_t process)
181 smx_simdata_process_t p_simdata = process->simdata;
183 DEBUG2("Killing process %s on %s", process->name,
184 p_simdata->smx_host->name);
186 /* Cleanup if we were waiting for something */
187 if (p_simdata->mutex)
188 xbt_swag_remove(process, p_simdata->mutex->sleeping);
191 xbt_swag_remove(process, p_simdata->cond->sleeping);
193 xbt_swag_remove(process, simix_global->process_to_run);
194 xbt_swag_remove(process, simix_global->process_list);
195 DEBUG2("%p here! killing %p", simix_global->current_process, process);
196 xbt_context_kill(process->simdata->context);
198 if (process == SIMIX_process_self()) {
199 /* I just killed myself */
205 * \brief Return the user data of a #smx_process_t.
207 * 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.
208 * \param process SIMIX process
209 * \return A void pointer to the user data
211 void *SIMIX_process_get_data(smx_process_t process)
213 xbt_assert0((process != NULL), "Invalid parameters");
215 return (process->data);
219 * \brief Set the user data of a #m_process_t.
221 * 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.
222 * \param process SIMIX process
223 * \param data User data
225 void SIMIX_process_set_data(smx_process_t process, void *data)
227 xbt_assert0((process != NULL), "Invalid parameters");
228 //xbt_assert0((process->data == NULL), "Data already set");
230 process->data = data;
236 * \brief Return the location on which an agent is running.
238 * 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.
239 * \param process SIMIX process
242 smx_host_t SIMIX_process_get_host(smx_process_t process)
244 xbt_assert0(((process != NULL)
245 && (process->simdata)), "Invalid parameters");
247 return (process->simdata->smx_host);
251 * \brief Return the name of an agent.
253 * This functions checks whether \a process is a valid pointer or not and return its name.
254 * \param process SIMIX process
255 * \return The process name
257 const char *SIMIX_process_get_name(smx_process_t process)
259 xbt_assert0(((process != NULL)
260 && (process->simdata)), "Invalid parameters");
262 return (process->name);
265 /** \ingroup m_process_management
266 * \brief Return the properties
268 * This functions returns the properties associated with this process
270 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
272 return process->simdata->properties;
276 * \brief Return the current agent.
278 * This functions returns the currently running #smx_process_t.
279 * \return The SIMIX process
281 smx_process_t SIMIX_process_self(void)
283 return simix_global ? simix_global->current_process : NULL;
287 * \brief Suspend the process.
289 * This functions suspend the process by suspending the action on
290 * which it was waiting for the completion.
292 * \param process SIMIX process
294 void SIMIX_process_suspend(smx_process_t process)
296 smx_simdata_process_t simdata = NULL;
298 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
300 if (process != SIMIX_process_self()) {
301 simdata = process->simdata;
303 if (simdata->mutex) {
304 /* process blocked on a mutex, only set suspend=1 */
305 simdata->suspended = 1;
306 } else if (simdata->cond) {
307 /* process blocked cond, suspend all actions */
309 /* temporaries variables */
314 simdata->suspended = 1;
316 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
317 surf_workstation_model->common_public->suspend(act->simdata->
321 simdata->suspended = 1;
324 /* process executing, I can create an action and suspend it */
327 char name[] = "dummy";
328 process->simdata->suspended = 1;
330 cond = SIMIX_cond_init();
331 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
332 surf_workstation_model->common_public->suspend(dummy->simdata->surf_action);
333 SIMIX_register_action_to_condition(dummy, cond);
334 __SIMIX_cond_wait(cond);
335 SIMIX_unregister_action_to_condition(dummy, cond);
336 SIMIX_action_destroy(dummy);
337 SIMIX_cond_destroy(cond);
343 * \brief Resume a suspended process.
345 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
346 * \param process SIMIX process
348 void SIMIX_process_resume(smx_process_t process)
350 smx_simdata_process_t simdata = NULL;
352 xbt_assert0(((process != NULL)
353 && (process->simdata)), "Invalid parameters");
356 if (process == SIMIX_process_self()) {
360 simdata = process->simdata;
361 if (simdata->mutex) {
362 DEBUG0("Resume process blocked on a mutex");
363 simdata->suspended = 0; /* He'll wake up by itself */
365 } else if (simdata->cond) {
366 /* temporaries variables */
370 DEBUG0("Resume process blocked on a conditional");
371 simdata->suspended = 0;
373 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
374 surf_workstation_model->common_public->resume(act->simdata->surf_action);
376 SIMIX_cond_signal(c);
379 simdata->suspended = 0;
380 xbt_swag_insert(process, simix_global->process_to_run);
386 * \brief Migrates an agent to another location.
388 * This function changes the value of the host on which \a process is running.
390 void SIMIX_process_change_host(smx_process_t process, char *source, char *dest)
392 smx_simdata_process_t p_simdata = process->simdata;
393 smx_host_t h1 = SIMIX_host_get_by_name(source);
394 smx_host_t h2 = SIMIX_host_get_by_name(dest);
395 p_simdata->smx_host = h2;
396 xbt_swag_remove(process, h1->simdata->process_list);
397 xbt_swag_insert(process, h2->simdata->process_list);
401 * \brief Returns true if the process is suspended .
403 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
404 * \param process SIMIX process
405 * \return 1, if the process is suspended, else 0.
407 int SIMIX_process_is_suspended(smx_process_t process)
409 xbt_assert0(((process != NULL)
410 && (process->simdata)), "Invalid parameters");
412 return (process->simdata->suspended);