Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
indent -i2 -kr. Aaaah, I feel much better now. Sorry, my screen is
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
13                                 "Logging specific to SIMIX (process)");
14
15 /******************************** Process ************************************/
16 /**
17  * \brief Creates and runs a new #smx_process_t.
18  *
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
22  */
23
24
25 void SIMIX_process_cleanup(void *arg)
26 {
27   xbt_swag_remove(arg, simix_global->process_list);
28   xbt_swag_remove(arg, simix_global->process_to_run);
29   xbt_swag_remove(arg,
30                   ((smx_process_t) arg)->simdata->s_host->simdata->
31                   process_list);
32   free(((smx_process_t) arg)->name);
33   ((smx_process_t) arg)->name = NULL;
34
35   free(((smx_process_t) arg)->simdata);
36   ((smx_process_t) arg)->simdata = NULL;
37   free(arg);
38 }
39
40 /** 
41  * \brief Creates and runs a new #smx_process_t.
42  *
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.
44  *
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. 
51  * \see smx_process_t
52  * \return The new corresponding object.
53  */
54 smx_process_t SIMIX_process_create(const char *name,
55                                    xbt_main_func_t code, void *data,
56                                    const char *hostname, int argc,
57                                    char **argv)
58 {
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);
63
64   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
65   /* Simulator Data */
66
67   simdata->s_host = host;
68   simdata->mutex = NULL;
69   simdata->cond = NULL;
70   simdata->argc = argc;
71   simdata->argv = argv;
72   simdata->context = xbt_context_new(code, NULL, NULL,
73                                      simix_global->
74                                      cleanup_process_function, process,
75                                      simdata->argc, simdata->argv);
76
77   /* Process structure */
78   process->name = xbt_strdup(name);
79   process->simdata = simdata;
80   process->data = data;
81
82   xbt_swag_insert(process, host->simdata->process_list);
83
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;
88
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);
92
93   return process;
94 }
95
96 /** 
97  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
98  *
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 
101  */
102 void SIMIX_jprocess_create(const char *name, smx_host_t host,
103                            void *data,
104                            void *jprocess, void *jenv, smx_process_t * res)
105 {
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;
109
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 
116      field ourself ASAP.
117
118      All this would be much simpler if the synchronization stuff would be done
119      in the JAVA world, I think.
120    */
121   *res = process;
122
123
124
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");
128   /* Simulator Data */
129   simdata->s_host = host;
130   simdata->mutex = NULL;
131   simdata->cond = NULL;
132   simdata->argc = 0;
133   simdata->argv = NULL;
134
135   simdata->context = xbt_context_new(NULL, NULL, NULL,
136                                      simix_global->
137                                      cleanup_process_function, process,
138                                      /* argc/argv */ 0, NULL);
139
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);
146
147   xbt_swag_insert(process, host->simdata->process_list);
148
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;
153
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);
157
158 }
159
160
161 /** \brief Kill a SIMIX process
162  *
163  * This function simply kills a \a process... scarry isn't it ? :).
164  * \param process poor victim
165  *
166  */
167 void SIMIX_process_kill(smx_process_t process)
168 {
169   smx_simdata_process_t p_simdata = process->simdata;
170
171   DEBUG2("Killing process %s on %s", process->name,
172          p_simdata->s_host->name);
173
174   /* Cleanup if we were waiting for something */
175   DEBUG0("Here!");
176   if (p_simdata->mutex)
177     xbt_swag_remove(process, p_simdata->mutex->sleeping);
178
179   DEBUG0("Here!");
180   if (p_simdata->cond)
181     xbt_swag_remove(process, p_simdata->cond->sleeping);
182
183   DEBUG0("Here!");
184   xbt_swag_remove(process, simix_global->process_to_run);
185   DEBUG0("Here!");
186   xbt_swag_remove(process, simix_global->process_list);
187   DEBUG2("%p here! killing %p", simix_global->current_process, process);
188   xbt_context_kill(process->simdata->context);
189
190   DEBUG0("Here!");
191   if (process == SIMIX_process_self()) {
192     /* I just killed myself */
193     DEBUG0("Here!");
194     xbt_context_yield();
195   }
196   DEBUG0("Here!");
197 }
198
199 /**
200  * \brief Return the user data of a #smx_process_t.
201  *
202  * 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.
203  * \param process SIMIX process
204  * \return A void pointer to the user data
205  */
206 void *SIMIX_process_get_data(smx_process_t process)
207 {
208   xbt_assert0((process != NULL), "Invalid parameters");
209
210   return (process->data);
211 }
212
213 /**
214  * \brief Set the user data of a #m_process_t.
215  *
216  * 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.
217  * \param process SIMIX process
218  * \param data User data
219  */
220 void SIMIX_process_set_data(smx_process_t process, void *data)
221 {
222   xbt_assert0((process != NULL), "Invalid parameters");
223   //xbt_assert0((process->data == NULL), "Data already set");
224
225   process->data = data;
226
227   return;
228 }
229
230 /**
231  * \brief Return the location on which an agent is running.
232  *
233  * 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.
234  * \param process SIMIX process
235  * \return SIMIX host
236  */
237 smx_host_t SIMIX_process_get_host(smx_process_t process)
238 {
239   xbt_assert0(((process != NULL)
240                && (process->simdata)), "Invalid parameters");
241
242   return (process->simdata->s_host);
243 }
244
245 /**
246  * \brief Return the name of an agent.
247  *
248  * This functions checks whether \a process is a valid pointer or not and return its name.
249  * \param process SIMIX process
250  * \return The process name
251  */
252 const char *SIMIX_process_get_name(smx_process_t process)
253 {
254   xbt_assert0(((process != NULL)
255                && (process->simdata)), "Invalid parameters");
256
257   return (process->name);
258 }
259
260 /**
261  * \brief Return the current agent.
262  *
263  * This functions returns the currently running #smx_process_t.
264  * \return The SIMIX process
265  */
266 smx_process_t SIMIX_process_self(void)
267 {
268   return simix_global ? simix_global->current_process : NULL;
269 }
270
271 /**
272  * \brief Suspend the process.
273  *
274  * This functions suspend the process by suspending the action on which it was waiting for the completion.
275  *      \param process SIMIX process
276  */
277 void SIMIX_process_suspend(smx_process_t process)
278 {
279   smx_simdata_process_t simdata = NULL;
280
281   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
282
283   if (process != SIMIX_process_self()) {
284     simdata = process->simdata;
285
286     if (simdata->mutex) {
287       /* process blocked on a mutex, only set suspend=1 */
288       simdata->suspended = 1;
289     } else if (simdata->cond) {
290       /* process blocked cond, suspend all actions */
291
292       /* temporaries variables */
293       smx_cond_t c;
294       xbt_fifo_item_t i;
295       smx_action_t act;
296
297       simdata->suspended = 1;
298       c = simdata->cond;
299       xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
300         surf_workstation_resource->common_public->suspend(act->simdata->
301                                                           surf_action);
302       }
303     } else {
304       simdata->suspended = 1;
305     }
306   } else {
307     /* process executing, I can create an action and suspend it */
308     process->simdata->suspended = 1;
309     smx_action_t dummy;
310     smx_cond_t cond;
311     char name[] = "dummy";
312
313     cond = SIMIX_cond_init();
314     dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
315     surf_workstation_resource->common_public->set_priority(dummy->simdata->
316                                                            surf_action,
317                                                            0.0);
318     SIMIX_register_condition_to_action(dummy, cond);
319     SIMIX_register_action_to_condition(dummy, cond);
320     __SIMIX_cond_wait(cond);
321     //SIMIX_action_destroy(dummy);
322     //SIMIX_cond_destroy(cond);
323   }
324   return;
325 }
326
327 /**
328  * \brief Resume a suspended process.
329  *
330  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
331  * \param process SIMIX process
332  */
333 void SIMIX_process_resume(smx_process_t process)
334 {
335   smx_simdata_process_t simdata = NULL;
336
337   xbt_assert0(((process != NULL)
338                && (process->simdata)), "Invalid parameters");
339   SIMIX_CHECK_HOST();
340
341   if (process == SIMIX_process_self()) {
342     return;
343   }
344
345   simdata = process->simdata;
346   if (simdata->mutex) {
347     DEBUG0("Resume process blocked on a mutex");
348     simdata->suspended = 0;     /* He'll wake up by itself */
349     return;
350   } else if (simdata->cond) {
351     DEBUG0("Resume process blocked on a conditional");
352     /* temporaries variables */
353     smx_cond_t c;
354     xbt_fifo_item_t i;
355     smx_action_t act;
356     simdata->suspended = 0;
357     c = simdata->cond;
358     xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
359       surf_workstation_resource->common_public->resume(act->simdata->
360                                                        surf_action);
361     }
362     return;
363   } else {
364     simdata->suspended = 0;
365     xbt_swag_insert(process, simix_global->process_to_run);
366   }
367
368 }
369
370 /**
371  * \brief Returns true if the process is suspended .
372  *
373  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
374  * \param process SIMIX process
375  * \return 1, if the process is suspended, else 0.
376  */
377 int SIMIX_process_is_suspended(smx_process_t process)
378 {
379   xbt_assert0(((process != NULL)
380                && (process->simdata)), "Invalid parameters");
381
382   return (process->simdata->suspended);
383 }
384
385
386 /* Helper functions for jMSG: manipulate the context data without breaking the module separation */
387 #include "xbt/context.h"        /* to pass java objects from MSG to the context */
388
389 void SIMIX_process_set_jprocess(smx_process_t process, void *jp)
390 {
391   xbt_context_set_jprocess(process->simdata->context, jp);
392 }
393
394 void *SIMIX_process_get_jprocess(smx_process_t process)
395 {
396   return xbt_context_get_jprocess(process->simdata->context);
397 }
398
399 void SIMIX_process_set_jmutex(smx_process_t process, void *jm)
400 {
401   xbt_context_set_jmutex(process->simdata->context, jm);
402 }
403
404 void *SIMIX_process_get_jmutex(smx_process_t process)
405 {
406   return xbt_context_get_jmutex(process->simdata->context);
407 }
408
409 void SIMIX_process_set_jcond(smx_process_t process, void *jc)
410 {
411   xbt_context_set_jcond(process->simdata->context, jc);
412 }
413
414 void *SIMIX_process_get_jcond(smx_process_t process)
415 {
416   return xbt_context_get_jcond(process->simdata->context);
417 }
418
419 void SIMIX_process_set_jenv(smx_process_t process, void *je)
420 {
421   xbt_context_set_jenv(process->simdata->context, je);
422 }
423
424 void *SIMIX_process_get_jenv(smx_process_t process)
425 {
426   return xbt_context_get_jenv(process->simdata->context);
427 }