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);
29 xbt_swag_remove(arg, ((smx_process_t) arg)->simdata->s_host->simdata->process_list);
30 free(((smx_process_t) arg)->name);
31 ((smx_process_t) arg)->name = NULL;
33 free(((smx_process_t) arg)->simdata);
34 ((smx_process_t) arg)->simdata = NULL;
39 * \brief Creates and runs a new #smx_process_t.
41 * 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.
43 * \param name a name for the object. It is for user-level information and can be NULL.
44 * \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.
45 * \param host the location where the new agent is executed.
46 * \param argc first argument passed to \a code
47 * \param argv second argument passed to \a code
48 * \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.
50 * \return The new corresponding object.
52 smx_process_t SIMIX_process_create(const char *name,
53 xbt_main_func_t code, void *data,
54 const char * hostname, int argc, char **argv,
55 void * clean_process_function)
57 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t,1);
58 smx_process_t process = xbt_new0(s_smx_process_t,1);
59 smx_process_t self = NULL;
60 smx_host_t host = SIMIX_host_get_by_name(hostname);
62 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
65 simdata->s_host = host;
66 simdata->mutex = NULL;
70 if (clean_process_function) {
71 simdata->context = xbt_context_new(code, NULL, NULL,
72 clean_process_function, process,
73 simdata->argc, simdata->argv);
76 simdata->context = xbt_context_new(code, NULL, NULL,
77 SIMIX_process_cleanup, process,
78 simdata->argc, simdata->argv);
81 /* Process structure */
82 process->name = xbt_strdup(name);
83 process->simdata = simdata;
86 xbt_swag_insert(process, host->simdata->process_list);
88 /* fix current_process, about which xbt_context_start mocks around */
89 self = simix_global->current_process;
90 xbt_context_start(process->simdata->context);
91 simix_global->current_process = self;
93 xbt_swag_insert(process,simix_global->process_list);
94 DEBUG2("Inserting %s(%s) in the to_run list",process->name,
96 xbt_swag_insert(process,simix_global->process_to_run);
102 * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
104 * Warning: this should only be used in libsimgrid4java, since it create
105 * a context with no code, which leads to segfaults in plain libsimgrid
107 void SIMIX_jprocess_create(const char *name, smx_host_t host,
109 void *jprocess, void *jenv,
110 void *clean_process_function,
113 smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t,1);
114 smx_process_t process = xbt_new0(s_smx_process_t,1);
115 smx_process_t self = NULL;
117 /* HACK: We need this trick because when we xbt_context_new() do
118 syncronization stuff, the s_process field in the m_process needs
119 to have a valid value, and we call xbt_context_new() before
120 returning, of course, ie, before providing a right value to the
121 caller (Java_simgrid_msg_Msg_processCreate) have time to store it
122 in place. This way, we initialize the m_process->simdata->s_process
125 All this would be much simpler if the synchronization stuff would be done
126 in the JAVA world, I think.
132 DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
133 name,host,data,jprocess,jenv);
134 xbt_assert0(host, "Invalid parameters");
136 simdata->s_host = host;
137 simdata->mutex = NULL;
138 simdata->cond = NULL;
140 simdata->argv = NULL;
142 if (clean_process_function) {
143 simdata->context = xbt_context_new(NULL, NULL, NULL,
144 clean_process_function, process,
145 /* argc/argv*/0,NULL);
147 simdata->context = xbt_context_new(NULL, NULL, NULL,
148 SIMIX_process_cleanup, process,
149 /* argc/argv*/0,NULL);
152 /* Process structure */
153 process->name = xbt_strdup(name);
154 process->simdata = simdata;
155 process->data = data;
156 SIMIX_process_set_jprocess(process,jprocess);
157 SIMIX_process_set_jenv(process,jenv);
159 xbt_swag_insert(process, host->simdata->process_list);
161 /* fix current_process, about which xbt_context_start mocks around */
162 self = simix_global->current_process;
163 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,
169 xbt_swag_insert(process,simix_global->process_to_run);
174 /** \brief Kill a SIMIX process
176 * This function simply kills a \a process... scarry isn't it ? :).
177 * \param process poor victim
180 void SIMIX_process_kill(smx_process_t process)
182 smx_simdata_process_t p_simdata = process->simdata;
184 DEBUG2("Killing process %s on %s",process->name, p_simdata->s_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 xbt_context_kill(process->simdata->context);
197 if(process==SIMIX_process_self()) {
198 /* I just killed myself */
204 * \brief Return the user data of a #smx_process_t.
206 * 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.
207 * \param process SIMIX process
208 * \return A void pointer to the user data
210 void *SIMIX_process_get_data(smx_process_t process)
212 xbt_assert0((process != NULL), "Invalid parameters");
214 return (process->data);
218 * \brief Set the user data of a #m_process_t.
220 * 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.
221 * \param process SIMIX process
222 * \param data User data
224 void SIMIX_process_set_data(smx_process_t process,void *data)
226 xbt_assert0((process != NULL), "Invalid parameters");
227 xbt_assert0((process->data == NULL), "Data already set");
229 process->data = data;
235 * \brief Return the location on which an agent is running.
237 * 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.
238 * \param process SIMIX process
241 smx_host_t SIMIX_process_get_host(smx_process_t process)
243 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
245 return (process->simdata->s_host);
249 * \brief Return the name of an agent.
251 * This functions checks whether \a process is a valid pointer or not and return its name.
252 * \param process SIMIX process
253 * \return The process name
255 const char *SIMIX_process_get_name(smx_process_t process)
257 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
259 return (process->name);
263 * \brief Return the current agent.
265 * This functions returns the currently running #smx_process_t.
266 * \return The SIMIX process
268 smx_process_t SIMIX_process_self(void)
270 return simix_global ? simix_global->current_process : NULL;
274 * \brief Suspend the process.
276 * This functions suspend the process by suspending the action on which it was waiting for the completion.
277 * \param process SIMIX process
279 void SIMIX_process_suspend(smx_process_t process)
281 smx_simdata_process_t simdata = NULL;
283 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
285 if(process!=SIMIX_process_self()) {
286 simdata = process->simdata;
288 if (simdata->mutex) {
289 /* process blocked on a mutex, only set suspend=1 */
290 simdata->suspended = 1;
292 else if (simdata->cond){
293 /* process blocked cond, suspend all actions */
295 /* temporaries variables */
300 simdata->suspended = 1;
302 xbt_fifo_foreach(c->actions,i,act, smx_action_t) {
303 surf_workstation_resource->common_public->suspend(act->simdata->surf_action);
307 simdata->suspended = 1;
311 /* process executing, I can create an action and suspend it */
312 process->simdata->suspended = 1;
315 char name[] = "dummy";
317 cond = SIMIX_cond_init();
318 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
319 surf_workstation_resource->common_public->set_priority(dummy->simdata->surf_action,0.0);
320 SIMIX_register_condition_to_action(dummy,cond);
321 SIMIX_register_action_to_condition(dummy,cond);
322 __SIMIX_cond_wait(cond);
323 //SIMIX_action_destroy(dummy);
324 //SIMIX_cond_destroy(cond);
330 * \brief Resume a suspended process.
332 * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
333 * \param process SIMIX process
335 void SIMIX_process_resume(smx_process_t process)
337 smx_simdata_process_t simdata = NULL;
339 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
342 if(process == SIMIX_process_self()) {
346 simdata = process->simdata;
348 DEBUG0("Resume process blocked on a mutex");
349 simdata->suspended = 0; /* He'll wake up by itself */
352 else if (simdata->cond) {
353 DEBUG0("Resume process blocked on a conditional");
354 /* temporaries variables */
358 simdata->suspended = 0;
360 xbt_fifo_foreach(c->actions,i,act, smx_action_t) {
361 surf_workstation_resource->common_public->resume(act->simdata->surf_action);
366 simdata->suspended = 0;
367 xbt_swag_insert(process,simix_global->process_to_run);
373 * \brief Returns true if the process is suspended .
375 * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
376 * \param process SIMIX process
377 * \return 1, if the process is suspended, else 0.
379 int SIMIX_process_is_suspended(smx_process_t process)
381 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
383 return (process->simdata->suspended);
387 /* Helper functions for jMSG: manipulate the context data without breaking the module separation */
388 #include "xbt/context.h" /* to pass java objects from MSG to the context */
390 void SIMIX_process_set_jprocess(smx_process_t process, void *jp) {
391 xbt_context_set_jprocess(process->simdata->context,jp);
393 void* SIMIX_process_get_jprocess(smx_process_t process) {
394 return xbt_context_get_jprocess(process->simdata->context);
397 void SIMIX_process_set_jmutex(smx_process_t process, void *jm) {
398 xbt_context_set_jmutex(process->simdata->context,jm);
400 void* SIMIX_process_get_jmutex(smx_process_t process) {
401 return xbt_context_get_jmutex(process->simdata->context);
404 void SIMIX_process_set_jcond(smx_process_t process, void *jc) {
405 xbt_context_set_jcond(process->simdata->context,jc);
407 void* SIMIX_process_get_jcond(smx_process_t process) {
408 return xbt_context_get_jcond(process->simdata->context);
411 void SIMIX_process_set_jenv(smx_process_t process, void *je) {
412 xbt_context_set_jenv(process->simdata->context,je);
414 void* SIMIX_process_get_jenv(smx_process_t process) {
415 return xbt_context_get_jenv(process->simdata->context);