Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
610b85cc163b4357ece222b2e4837b017ae24f44
[simgrid.git] / src / simix / smx_process.c
1 //*     $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
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. */
7
8 #include "private.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "xbt/dict.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
14                                 "Logging specific to SIMIX (process)");
15
16 /******************************** Process ************************************/
17 /**
18  * \brief Creates and runs a new #smx_process_t.
19  *
20  * Does exactly the same as #SIMIX_process_create_with_arguments but without 
21    providing standard arguments (\a argc, \a argv).
22  * \see SIMIX_process_create_with_arguments
23  */
24
25
26 void SIMIX_process_cleanup(void *arg)
27 {
28   xbt_swag_remove(arg, simix_global->process_list);
29   xbt_swag_remove(arg, simix_global->process_to_run);
30   xbt_swag_remove(arg,
31                   ((smx_process_t) arg)->simdata->smx_host->simdata->
32                   process_list);
33   free(((smx_process_t) arg)->name);
34   ((smx_process_t) arg)->name = NULL;
35
36   free(((smx_process_t) arg)->simdata);
37   ((smx_process_t) arg)->simdata = NULL;
38   free(arg);
39 }
40
41 /** 
42  * \brief Creates and runs a new #smx_process_t.
43  *
44  * 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  *
46  * \param name a name for the object. It is for user-level information and can be NULL.
47 * \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.
48  * \param host the location where the new agent is executed.
49  * \param argc first argument passed to \a code
50  * \param argv second argument passed to \a code
51  * \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  * \see smx_process_t
53  * \return The new corresponding object.
54  */
55 smx_process_t SIMIX_process_create(const char *name,
56                                    xbt_main_func_t code, void *data,
57                                    const char *hostname, int argc,
58                                    char **argv, xbt_dict_t properties)
59 {
60   smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
61   smx_process_t process = xbt_new0(s_smx_process_t, 1);
62   smx_process_t self = NULL;
63   smx_host_t host = SIMIX_host_get_by_name(hostname);
64
65   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
66   /* Simulator Data */
67
68   simdata->smx_host = host;
69   simdata->mutex = NULL;
70   simdata->cond = NULL;
71   simdata->argc = argc;
72   simdata->argv = argv;
73   simdata->context = xbt_context_new(name,code, NULL, NULL,
74                                      simix_global->
75                                      cleanup_process_function, process,
76                                      simdata->argc, simdata->argv);
77
78   /* Process structure */
79   process->name = xbt_strdup(name);
80   process->simdata = simdata;
81   process->data = data;
82
83   /* Add properties*/
84   simdata->properties = properties;
85
86   xbt_swag_insert(process, host->simdata->process_list);
87
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;
92
93   xbt_swag_insert(process, simix_global->process_list);
94   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
95   xbt_swag_insert(process, simix_global->process_to_run);
96
97   return process;
98 }
99 /** 
100  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
101  *
102  * Warning: this should only be used in libsimgrid4java, since it create
103  * a context with no code, which leads to segfaults in plain libsimgrid 
104  */
105 void SIMIX_jprocess_create(const char *name, smx_host_t host,
106                            void *data,
107                            void *jprocess, void *jenv, smx_process_t * res)
108 {
109   smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t, 1);
110   smx_process_t process = xbt_new0(s_smx_process_t, 1);
111   smx_process_t self = NULL;
112
113   /* HACK: We need this trick because when we xbt_context_new() do
114      syncronization stuff, the s_process field in the m_process needs 
115      to have a valid value, and we call xbt_context_new() before 
116      returning, of course, ie, before providing a right value to the 
117      caller (Java_simgrid_msg_Msg_processCreate) have time to store it  
118      in place. This way, we initialize the m_process->simdata->s_process 
119      field ourself ASAP.
120
121      All this would be much simpler if the synchronization stuff would be done
122      in the JAVA world, I think.
123    */
124   *res = process;
125
126
127
128   DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
129          name, host, data, jprocess, jenv);
130   xbt_assert0(host, "Invalid parameters");
131   /* Simulator Data */
132   simdata->smx_host = host;
133   simdata->mutex = NULL;
134   simdata->cond = NULL;
135   simdata->argc = 0;
136   simdata->argv = NULL;
137
138   simdata->context = xbt_context_new(name,NULL, NULL, NULL,
139                                      simix_global->
140                                      cleanup_process_function, process,
141                                      /* argc/argv */ 0, NULL);
142
143   /* Process structure */
144   process->name = xbt_strdup(name);
145   process->simdata = simdata;
146   process->data = data;
147   SIMIX_process_set_jprocess(process, jprocess);
148   SIMIX_process_set_jenv(process, jenv);
149
150   xbt_swag_insert(process, host->simdata->process_list);
151
152   /* fix current_process, about which xbt_context_start mocks around */
153   self = simix_global->current_process;
154   xbt_context_start(process->simdata->context);
155   simix_global->current_process = self;
156
157   xbt_swag_insert(process, simix_global->process_list);
158   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
159   xbt_swag_insert(process, simix_global->process_to_run);
160
161 }
162
163
164 /** \brief Kill a SIMIX process
165  *
166  * This function simply kills a \a process... scarry isn't it ? :).
167  * \param process poor victim
168  *
169  */
170 void SIMIX_process_kill(smx_process_t process)
171 {
172   smx_simdata_process_t p_simdata = process->simdata;
173
174   DEBUG2("Killing process %s on %s", process->name,
175          p_simdata->smx_host->name);
176
177   /* Cleanup if we were waiting for something */
178   if (p_simdata->mutex)
179     xbt_swag_remove(process, p_simdata->mutex->sleeping);
180
181   if (p_simdata->cond)
182     xbt_swag_remove(process, p_simdata->cond->sleeping);
183
184   xbt_swag_remove(process, simix_global->process_to_run);
185   xbt_swag_remove(process, simix_global->process_list);
186   DEBUG2("%p here! killing %p", simix_global->current_process, process);
187   xbt_context_kill(process->simdata->context);
188
189   if (process == SIMIX_process_self()) {
190     /* I just killed myself */
191     xbt_context_yield();
192   }
193 }
194
195 /**
196  * \brief Return the user data of a #smx_process_t.
197  *
198  * 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.
199  * \param process SIMIX process
200  * \return A void pointer to the user data
201  */
202 void *SIMIX_process_get_data(smx_process_t process)
203 {
204   xbt_assert0((process != NULL), "Invalid parameters");
205
206   return (process->data);
207 }
208
209 /**
210  * \brief Set the user data of a #m_process_t.
211  *
212  * 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.
213  * \param process SIMIX process
214  * \param data User data
215  */
216 void SIMIX_process_set_data(smx_process_t process, void *data)
217 {
218   xbt_assert0((process != NULL), "Invalid parameters");
219   //xbt_assert0((process->data == NULL), "Data already set");
220
221   process->data = data;
222
223   return;
224 }
225
226 /**
227  * \brief Return the location on which an agent is running.
228  *
229  * 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.
230  * \param process SIMIX process
231  * \return SIMIX host
232  */
233 smx_host_t SIMIX_process_get_host(smx_process_t process)
234 {
235   xbt_assert0(((process != NULL)
236                && (process->simdata)), "Invalid parameters");
237
238   return (process->simdata->smx_host);
239 }
240
241 /**
242  * \brief Return the name of an agent.
243  *
244  * This functions checks whether \a process is a valid pointer or not and return its name.
245  * \param process SIMIX process
246  * \return The process name
247  */
248 const char *SIMIX_process_get_name(smx_process_t process)
249 {
250   xbt_assert0(((process != NULL)
251                && (process->simdata)), "Invalid parameters");
252
253   return (process->name);
254 }
255
256 /** \ingroup m_process_management
257  * \brief Return the properties
258  *
259  * This functions returns the properties associated with this process
260  */
261 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
262 {
263   return process->simdata->properties;
264 }
265
266 /**
267  * \brief Return the current agent.
268  *
269  * This functions returns the currently running #smx_process_t.
270  * \return The SIMIX process
271  */
272 smx_process_t SIMIX_process_self(void)
273 {
274   return simix_global ? simix_global->current_process : NULL;
275 }
276
277 /**
278  * \brief Suspend the process.
279  *
280  * This functions suspend the process by suspending the action on
281  * which it was waiting for the completion.
282  *
283  * \param process SIMIX process
284  */
285 void SIMIX_process_suspend(smx_process_t process)
286 {
287   smx_simdata_process_t simdata = NULL;
288
289   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
290
291   if (process != SIMIX_process_self()) {
292     simdata = process->simdata;
293
294     if (simdata->mutex) {
295       /* process blocked on a mutex, only set suspend=1 */
296       simdata->suspended = 1;
297     } else if (simdata->cond) {
298       /* process blocked cond, suspend all actions */
299
300       /* temporaries variables */
301       smx_cond_t c;
302       xbt_fifo_item_t i;
303       smx_action_t act;
304
305       simdata->suspended = 1;
306       c = simdata->cond;
307       xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
308         surf_workstation_model->common_public->suspend(act->simdata->
309                                                           surf_action);
310       }
311     } else {
312       simdata->suspended = 1;
313     }
314   } else {
315     /* process executing, I can create an action and suspend it */
316     smx_action_t dummy;
317     smx_cond_t cond;
318     char name[] = "dummy";
319     process->simdata->suspended = 1;
320
321     cond = SIMIX_cond_init();
322     dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
323     surf_workstation_model->common_public->suspend(dummy->simdata->surf_action);
324     SIMIX_register_action_to_condition(dummy, cond);
325     __SIMIX_cond_wait(cond);
326     //SIMIX_action_destroy(dummy);
327     //SIMIX_cond_destroy(cond);
328   }
329   return;
330 }
331
332 /**
333  * \brief Resume a suspended process.
334  *
335  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
336  * \param process SIMIX process
337  */
338 void SIMIX_process_resume(smx_process_t process)
339 {
340   smx_simdata_process_t simdata = NULL;
341
342   xbt_assert0(((process != NULL)
343                && (process->simdata)), "Invalid parameters");
344   SIMIX_CHECK_HOST();
345
346   if (process == SIMIX_process_self()) {
347     return;
348   }
349
350   simdata = process->simdata;
351   if (simdata->mutex) {
352     DEBUG0("Resume process blocked on a mutex");
353     simdata->suspended = 0;     /* He'll wake up by itself */
354     return;
355   } else if (simdata->cond) {
356     /* temporaries variables */
357     smx_cond_t c;
358     xbt_fifo_item_t i;
359     smx_action_t act;
360         DEBUG0("Resume process blocked on a conditional");
361     simdata->suspended = 0;
362     c = simdata->cond;
363     xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
364       surf_workstation_model->common_public->resume(act->simdata->surf_action);
365     }
366     SIMIX_cond_signal(c);
367     return;
368   } else {
369     simdata->suspended = 0;
370     xbt_swag_insert(process, simix_global->process_to_run);
371   }
372
373 }
374
375 /**
376  * \brief Returns true if the process is suspended .
377  *
378  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
379  * \param process SIMIX process
380  * \return 1, if the process is suspended, else 0.
381  */
382 int SIMIX_process_is_suspended(smx_process_t process)
383 {
384   xbt_assert0(((process != NULL)
385                && (process->simdata)), "Invalid parameters");
386
387   return (process->simdata->suspended);
388 }
389
390
391 /* Helper functions for jMSG: manipulate the context data without breaking the module separation */
392 #include "xbt/context.h"        /* to pass java objects from MSG to the context */
393
394 void SIMIX_process_set_jprocess(smx_process_t process, void *jp)
395 {
396   xbt_context_set_jprocess(process->simdata->context, jp);
397 }
398
399 void *SIMIX_process_get_jprocess(smx_process_t process)
400 {
401   return xbt_context_get_jprocess(process->simdata->context);
402 }
403
404 void SIMIX_process_set_jenv(smx_process_t process, void *je)
405 {
406   xbt_context_set_jenv(process->simdata->context, je);
407 }
408
409 void *SIMIX_process_get_jenv(smx_process_t process)
410 {
411   return xbt_context_get_jenv(process->simdata->context);
412 }