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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
13 "Logging specific to SIMIX (process)");
15 /******************************** Process ************************************/
17 * \brief Creates and runs a new #smx_process_t.
19 * Does exactly the same as #SIMIX_process_create_with_arguments but without
20 providing standard arguments (\a argc, \a argv).
21 * \see SIMIX_process_create_with_arguments
25 void SIMIX_process_cleanup(void *arg)
27 xbt_swag_remove(arg, simix_global->process_list);
28 xbt_swag_remove(arg, simix_global->process_to_run);
30 ((smx_process_t) arg)->simdata->smx_host->simdata->
32 free(((smx_process_t) arg)->name);
33 ((smx_process_t) arg)->name = NULL;
35 free(((smx_process_t) arg)->simdata);
36 ((smx_process_t) arg)->simdata = NULL;
41 * \brief Creates and runs a new #smx_process_t.
43 * 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.
45 * \param name a name for the object. It is for user-level information and can be NULL.
46 * \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.
47 * \param host the location where the new agent is executed.
48 * \param argc first argument passed to \a code
49 * \param argv second argument passed to \a code
50 * \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.
52 * \return The new corresponding object.
54 smx_process_t SIMIX_process_create(const char *name,
55 xbt_main_func_t code, void *data,
56 const char *hostname, int argc,
59 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
60 smx_process_t process = xbt_new0(s_smx_process_t, 1);
61 smx_process_t self = NULL;
62 smx_host_t host = SIMIX_host_get_by_name(hostname);
64 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
67 simdata->smx_host = host;
68 simdata->mutex = NULL;
72 simdata->context = xbt_context_new(name,code, NULL, NULL,
74 cleanup_process_function, process,
75 simdata->argc, simdata->argv);
77 /* Process structure */
78 process->name = xbt_strdup(name);
79 process->simdata = simdata;
82 xbt_swag_insert(process, host->simdata->process_list);
84 /* fix current_process, about which xbt_context_start mocks around */
85 self = simix_global->current_process;
86 xbt_context_start(process->simdata->context);
87 simix_global->current_process = self;
89 xbt_swag_insert(process, simix_global->process_list);
90 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
91 xbt_swag_insert(process, simix_global->process_to_run);
97 * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
99 * Warning: this should only be used in libsimgrid4java, since it create
100 * a context with no code, which leads to segfaults in plain libsimgrid
102 void SIMIX_jprocess_create(const char *name, smx_host_t host,
104 void *jprocess, void *jenv, smx_process_t * res)
106 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
107 smx_process_t process = xbt_new0(s_smx_process_t, 1);
108 smx_process_t self = NULL;
110 /* HACK: We need this trick because when we xbt_context_new() do
111 syncronization stuff, the s_process field in the m_process needs
112 to have a valid value, and we call xbt_context_new() before
113 returning, of course, ie, before providing a right value to the
114 caller (Java_simgrid_msg_Msg_processCreate) have time to store it
115 in place. This way, we initialize the m_process->simdata->s_process
118 All this would be much simpler if the synchronization stuff would be done
119 in the JAVA world, I think.
125 DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
126 name, host, data, jprocess, jenv);
127 xbt_assert0(host, "Invalid parameters");
129 simdata->smx_host = host;
130 simdata->mutex = NULL;
131 simdata->cond = NULL;
133 simdata->argv = NULL;
135 simdata->context = xbt_context_new(name,NULL, NULL, NULL,
137 cleanup_process_function, process,
138 /* argc/argv */ 0, NULL);
140 /* Process structure */
141 process->name = xbt_strdup(name);
142 process->simdata = simdata;
143 process->data = data;
144 SIMIX_process_set_jprocess(process, jprocess);
145 SIMIX_process_set_jenv(process, jenv);
147 xbt_swag_insert(process, host->simdata->process_list);
149 /* fix current_process, about which xbt_context_start mocks around */
150 self = simix_global->current_process;
151 xbt_context_start(process->simdata->context);
152 simix_global->current_process = self;
154 xbt_swag_insert(process, simix_global->process_list);
155 DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
156 xbt_swag_insert(process, simix_global->process_to_run);
161 /** \brief Kill a SIMIX process
163 * This function simply kills a \a process... scarry isn't it ? :).
164 * \param process poor victim
167 void SIMIX_process_kill(smx_process_t process)
169 smx_simdata_process_t p_simdata = process->simdata;
171 DEBUG2("Killing process %s on %s", process->name,
172 p_simdata->smx_host->name);
174 /* Cleanup if we were waiting for something */
175 if (p_simdata->mutex)
176 xbt_swag_remove(process, p_simdata->mutex->sleeping);
179 xbt_swag_remove(process, p_simdata->cond->sleeping);
181 xbt_swag_remove(process, simix_global->process_to_run);
182 xbt_swag_remove(process, simix_global->process_list);
183 DEBUG2("%p here! killing %p", simix_global->current_process, process);
184 xbt_context_kill(process->simdata->context);
186 if (process == SIMIX_process_self()) {
187 /* I just killed myself */
193 * \brief Return the user data of a #smx_process_t.
195 * 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.
196 * \param process SIMIX process
197 * \return A void pointer to the user data
199 void *SIMIX_process_get_data(smx_process_t process)
201 xbt_assert0((process != NULL), "Invalid parameters");
203 return (process->data);
207 * \brief Set the user data of a #m_process_t.
209 * 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.
210 * \param process SIMIX process
211 * \param data User data
213 void SIMIX_process_set_data(smx_process_t process, void *data)
215 xbt_assert0((process != NULL), "Invalid parameters");
216 //xbt_assert0((process->data == NULL), "Data already set");
218 process->data = data;
224 * \brief Return the location on which an agent is running.
226 * 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.
227 * \param process SIMIX process
230 smx_host_t SIMIX_process_get_host(smx_process_t process)
232 xbt_assert0(((process != NULL)
233 && (process->simdata)), "Invalid parameters");
235 return (process->simdata->smx_host);
239 * \brief Return the name of an agent.
241 * This functions checks whether \a process is a valid pointer or not and return its name.
242 * \param process SIMIX process
243 * \return The process name
245 const char *SIMIX_process_get_name(smx_process_t process)
247 xbt_assert0(((process != NULL)
248 && (process->simdata)), "Invalid parameters");
250 return (process->name);
254 * \brief Return the current agent.
256 * This functions returns the currently running #smx_process_t.
257 * \return The SIMIX process
259 smx_process_t SIMIX_process_self(void)
261 return simix_global ? simix_global->current_process : NULL;
265 * \brief Suspend the process.
267 * This functions suspend the process by suspending the action on
268 * which it was waiting for the completion.
270 * \param process SIMIX process
272 void SIMIX_process_suspend(smx_process_t process)
274 smx_simdata_process_t simdata = NULL;
276 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
278 if (process != SIMIX_process_self()) {
279 simdata = process->simdata;
281 if (simdata->mutex) {
282 /* process blocked on a mutex, only set suspend=1 */
283 simdata->suspended = 1;
284 } else if (simdata->cond) {
285 /* process blocked cond, suspend all actions */
287 /* temporaries variables */
292 simdata->suspended = 1;
294 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
295 surf_workstation_model->common_public->suspend(act->simdata->
299 simdata->suspended = 1;
302 /* process executing, I can create an action and suspend it */
305 char name[] = "dummy";
306 process->simdata->suspended = 1;
308 cond = SIMIX_cond_init();
309 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
310 surf_workstation_model->common_public->suspend(dummy->simdata->surf_action);
311 SIMIX_register_action_to_condition(dummy, cond);
312 __SIMIX_cond_wait(cond);
313 //SIMIX_action_destroy(dummy);
314 //SIMIX_cond_destroy(cond);
320 * \brief Resume a suspended process.
322 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
323 * \param process SIMIX process
325 void SIMIX_process_resume(smx_process_t process)
327 smx_simdata_process_t simdata = NULL;
329 xbt_assert0(((process != NULL)
330 && (process->simdata)), "Invalid parameters");
333 if (process == SIMIX_process_self()) {
337 simdata = process->simdata;
338 if (simdata->mutex) {
339 DEBUG0("Resume process blocked on a mutex");
340 simdata->suspended = 0; /* He'll wake up by itself */
342 } else if (simdata->cond) {
343 /* temporaries variables */
347 DEBUG0("Resume process blocked on a conditional");
348 simdata->suspended = 0;
350 xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
351 surf_workstation_model->common_public->resume(act->simdata->surf_action);
353 SIMIX_cond_signal(c);
356 simdata->suspended = 0;
357 xbt_swag_insert(process, simix_global->process_to_run);
363 * \brief Returns true if the process is suspended .
365 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
366 * \param process SIMIX process
367 * \return 1, if the process is suspended, else 0.
369 int SIMIX_process_is_suspended(smx_process_t process)
371 xbt_assert0(((process != NULL)
372 && (process->simdata)), "Invalid parameters");
374 return (process->simdata->suspended);
378 /* Helper functions for jMSG: manipulate the context data without breaking the module separation */
379 #include "xbt/context.h" /* to pass java objects from MSG to the context */
381 void SIMIX_process_set_jprocess(smx_process_t process, void *jp)
383 xbt_context_set_jprocess(process->simdata->context, jp);
386 void *SIMIX_process_get_jprocess(smx_process_t process)
388 return xbt_context_get_jprocess(process->simdata->context);
391 void SIMIX_process_set_jenv(smx_process_t process, void *je)
393 xbt_context_set_jenv(process->simdata->context, je);
396 void *SIMIX_process_get_jenv(smx_process_t process)
398 return xbt_context_get_jenv(process->simdata->context);