Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed the pthread context implementation, now it compiles and runs
[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 #include "msg/mailbox.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
15                                 "Logging specific to SIMIX (process)");
16
17 /******************************** Process ************************************/
18 /**
19  * \brief Creates and runs a new #smx_process_t.
20  *
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
24  */
25
26 void SIMIX_process_cleanup(void *arg)
27 {
28   xbt_swag_remove(arg, simix_global->process_to_run);
29   xbt_swag_remove(arg, simix_global->process_list);
30   xbt_swag_remove(arg, ((smx_process_t)arg)->smx_host->process_list);
31   xbt_swag_insert(arg, simix_global->process_to_destroy);
32 }
33
34 /** Garbage collection
35  *
36  * Should be called some time to time to free the memory allocated for processes
37  * that have finished (or killed).
38  */
39 void SIMIX_process_empty_trash(void)
40
41   smx_process_t process = NULL;
42   int i;  
43
44   while ((process = xbt_swag_extract(simix_global->process_to_destroy))){
45     free(process->name);
46     process->name = NULL;
47   
48     if (process->argv) {
49       for (i = 0; i < process->argc; i++)
50         if (process->argv[i])
51           free(process->argv[i]);
52
53       free(process->argv);
54     }
55   
56     free(process);
57   }
58 }
59
60 /**
61  * \brief Creates and runs the maestro process
62  *
63  */
64
65 void __SIMIX_create_maestro_process()
66 {
67   smx_process_t process = NULL;
68   process = xbt_new0(s_smx_process_t, 1);
69
70   /* Process data */
71   process->name = (char *)"";
72
73   /*Create the right context type (FIXME: check the return value for success)*/
74   SIMIX_context_create_maestro(&process);
75
76   /* Set it as the maestro process */
77   simix_global->maestro_process = process;
78   simix_global->current_process = process;
79
80   return;
81 }
82
83 /**
84  * \brief Creates and runs a new #smx_process_t.
85  *
86  * 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.
87  *
88  * \param name a name for the object. It is for user-level information and can be NULL.
89 * \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.
90  * \param host the location where the new agent is executed.
91  * \param argc first argument passed to \a code
92  * \param argv second argument passed to \a code
93  * \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.
94  * \see smx_process_t
95  * \return The new corresponding object.
96  */
97 smx_process_t SIMIX_process_create(const char *name,
98                                    xbt_main_func_t code, void *data,
99                                    const char *hostname, int argc,
100                                    char **argv, xbt_dict_t properties)
101 {
102   smx_process_t process = NULL;
103   smx_process_t self = NULL;
104   smx_host_t host = SIMIX_host_get_by_name(hostname);
105
106   DEBUG2("Start process %s on host %s", name, hostname);
107
108   if (!SIMIX_host_get_state(host)) {
109     WARN2("Cannot launch process '%s' on failed host '%s'", name, hostname);
110     return NULL;
111   }
112   process = xbt_new0(s_smx_process_t, 1);
113
114   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
115
116   /* Process data */
117   process->name = xbt_strdup(name);
118   process->smx_host = host;
119   process->argc = argc;
120   process->argv = argv;
121   process->mutex = NULL;
122   process->cond = NULL;
123
124   /*Create the right context type (FIXME: check the return value for success)*/
125   SIMIX_context_new(&process, code);
126
127   process->data = data;
128   process->cleanup_func = simix_global->cleanup_process_function;
129   process->cleanup_arg = process;
130
131   /* Add properties */
132   process->properties = properties;
133
134   /* Add the process to it's host process list */
135   xbt_swag_insert(process, host->process_list);
136
137   /* fix current_process, about which SIMIX_context_start mocks around */
138   self = simix_global->current_process;
139   SIMIX_context_start(process);
140   simix_global->current_process = self;
141
142   /* Now insert it in the global process list and in the process to run list */
143   xbt_swag_insert(process, simix_global->process_list);
144   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
145   xbt_swag_insert(process, simix_global->process_to_run);
146
147   return process;
148 }
149
150 /**
151  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
152  *
153  * Warning: this should only be used in libsimgrid4java, since it create
154  * a context with no code, which leads to segfaults in plain libsimgrid
155  */
156 void SIMIX_jprocess_create(const char *name, smx_host_t host,
157                            void *data,
158                            void *jprocess, void *jenv, smx_process_t * res)
159 {
160   smx_process_t process = xbt_new0(s_smx_process_t, 1);
161   smx_process_t self = NULL;
162
163   /* HACK: We need this trick because when we xbt_context_new() do
164      syncronization stuff, the s_process field in the m_process needs
165      to have a valid value, and we call xbt_context_new() before
166      returning, of course, ie, before providing a right value to the
167      caller (Java_simgrid_msg_Msg_processCreate) have time to store it
168      in place. This way, we initialize the m_process->simdata->s_process
169      field ourself ASAP.
170
171      All this would be much simpler if the synchronization stuff would be done
172      in the JAVA world, I think.
173    */
174   *res = process;
175
176   DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
177          name, host, data, jprocess, jenv);
178   xbt_assert0(host, "Invalid parameters");
179
180   /* Process data */
181   process->name = xbt_strdup(name);
182   process->smx_host = host;
183   process->argc = 0;
184   process->argv = NULL;
185   process->mutex = NULL;
186   process->cond = NULL;
187   SIMIX_context_new(&process, jprocess);
188   process->data = data;
189
190   /* Add the process to it's host process list */
191   xbt_swag_insert(&process, host->process_list);
192
193   /* fix current_process, about which xbt_context_start mocks around */
194   self = simix_global->current_process;
195   SIMIX_context_start(process);
196   simix_global->current_process = self;
197
198   /* Now insert it in the global process list and in the process to run list */
199   xbt_swag_insert(process, simix_global->process_list);
200   DEBUG2("Inserting %s(%s) in the to_run list", process->name, host->name);
201   xbt_swag_insert(process, simix_global->process_to_run);
202 }
203
204 /** \brief Kill a SIMIX process
205  *
206  * This function simply kills a \a process... scarry isn't it ? :).
207  * \param process poor victim
208  *
209  */
210 void SIMIX_process_kill(smx_process_t process)
211 {
212   DEBUG2("Killing process %s on %s", process->name,
213          process->smx_host->name);
214
215   /* Cleanup if we were waiting for something */
216   if (process->mutex)
217     xbt_swag_remove(process, process->mutex->sleeping);
218
219   if (process->cond)
220     xbt_swag_remove(process, process->cond->sleeping);
221
222   DEBUG2("%p here! killing %p", simix_global->current_process, process);
223   SIMIX_context_kill(process);
224
225   if (process == SIMIX_process_self()) {
226     /* I just killed myself */
227     SIMIX_context_yield();
228   }
229 }
230
231 /**
232  * \brief Return the user data of a #smx_process_t.
233  *
234  * 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.
235  * \param process SIMIX process
236  * \return A void pointer to the user data
237  */
238 void *SIMIX_process_get_data(smx_process_t process)
239 {
240   xbt_assert0((process != NULL), "Invalid parameters");
241   return (process->data);
242 }
243
244 /**
245  * \brief Set the user data of a #m_process_t.
246  *
247  * 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.
248  * \param process SIMIX process
249  * \param data User data
250  */
251 void SIMIX_process_set_data(smx_process_t process, void *data)
252 {
253   xbt_assert0((process != NULL), "Invalid parameters");
254
255   process->data = data;
256   return;
257 }
258
259 /**
260  * \brief Return the location on which an agent is running.
261  *
262  * 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.
263  * \param process SIMIX process
264  * \return SIMIX host
265  */
266 smx_host_t SIMIX_process_get_host(smx_process_t process)
267 {
268   xbt_assert0((process != NULL), "Invalid parameters");
269   return (process->smx_host);
270 }
271
272 /**
273  * \brief Return the name of an agent.
274  *
275  * This functions checks whether \a process is a valid pointer or not and return its name.
276  * \param process SIMIX process
277  * \return The process name
278  */
279 const char *SIMIX_process_get_name(smx_process_t process)
280 {
281   xbt_assert0((process != NULL), "Invalid parameters");
282   return (process->name);
283 }
284
285 /**
286  * \brief Changes the name of an agent.
287  *
288  * This functions checks whether \a process is a valid pointer or not and return its name.
289  * \param process SIMIX process
290  * \param name The new process name
291  */
292 void SIMIX_process_set_name(smx_process_t process, char *name)
293 {
294   xbt_assert0((process != NULL), "Invalid parameters");
295   process->name = name;
296 }
297
298 /** \ingroup m_process_management
299  * \brief Return the properties
300  *
301  * This functions returns the properties associated with this process
302  */
303 xbt_dict_t SIMIX_process_get_properties(smx_process_t process)
304 {
305   return process->properties;
306 }
307
308 /**
309  * \brief Return the current agent.
310  *
311  * This functions returns the currently running #smx_process_t.
312  * \return The SIMIX process
313  */
314 smx_process_t SIMIX_process_self(void)
315 {
316   return simix_global ? simix_global->current_process : NULL;
317 }
318
319 /**
320  * \brief Suspend the process.
321  *
322  * This functions suspend the process by suspending the action on
323  * which it was waiting for the completion.
324  *
325  * \param process SIMIX process
326  */
327 void SIMIX_process_suspend(smx_process_t process)
328 {
329   xbt_assert0(process, "Invalid parameters");
330
331   if (process != SIMIX_process_self()) {
332
333     if (process->mutex) {
334       /* process blocked on a mutex, only set suspend=1 */
335       process->suspended = 1;
336     } else if (process->cond) {
337       /* process blocked cond, suspend all actions */
338
339       /* temporaries variables */
340       smx_cond_t c;
341       xbt_fifo_item_t i;
342       smx_action_t act;
343
344       process->suspended = 1;
345       c = process->cond;
346       xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
347          surf_workstation_model->suspend(act->surf_action);
348       }
349     } else {
350       process->suspended = 1;
351     }
352   } else {
353     /* process executing, I can create an action and suspend it */
354     smx_action_t dummy;
355     smx_cond_t cond;
356     char name[] = "dummy";
357     process->suspended = 1;
358
359     cond = SIMIX_cond_init();
360     dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
361     surf_workstation_model->suspend(dummy->surf_action);
362     SIMIX_register_action_to_condition(dummy, cond);
363     __SIMIX_cond_wait(cond);
364     SIMIX_unregister_action_to_condition(dummy, cond);
365     SIMIX_action_destroy(dummy);
366     SIMIX_cond_destroy(cond);
367   }
368   return;
369 }
370
371 /**
372  * \brief Resume a suspended process.
373  *
374  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
375  * \param process SIMIX process
376  */
377 void SIMIX_process_resume(smx_process_t process)
378 {
379   xbt_assert0((process != NULL), "Invalid parameters");
380   SIMIX_CHECK_HOST();
381
382   if (process == SIMIX_process_self())
383     return;
384
385   if (process->mutex) {
386     DEBUG0("Resume process blocked on a mutex");
387     process->suspended = 0;     /* It'll wake up by itself when mutex releases */
388     return;
389   } else if (process->cond) {
390     /* temporaries variables */
391     smx_cond_t c;
392     xbt_fifo_item_t i;
393     smx_action_t act;
394     DEBUG0("Resume process blocked on a conditional");
395     process->suspended = 0;
396     c = process->cond;
397     xbt_fifo_foreach(c->actions, i, act, smx_action_t) {
398       surf_workstation_model->resume(act->surf_action);
399     }
400     SIMIX_cond_signal(c);
401     return;
402   } else {
403     process->suspended = 0;
404     xbt_swag_insert(process, simix_global->process_to_run);
405   }
406 }
407
408 /**
409  * \brief Migrates an agent to another location.
410  *
411  * This function changes the value of the host on which \a process is running.
412  */
413 void SIMIX_process_change_host(smx_process_t process, char *source, char *dest)
414 {
415   xbt_assert0((process != NULL), "Invalid parameters");
416   smx_host_t h1 = SIMIX_host_get_by_name(source);
417   smx_host_t h2 = SIMIX_host_get_by_name(dest);
418   process->smx_host = h2;
419   xbt_swag_remove(process, h1->process_list);
420   xbt_swag_insert(process, h2->process_list);
421 }
422
423 /**
424  * \brief Returns true if the process is suspended .
425  *
426  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
427  * \param process SIMIX process
428  * \return 1, if the process is suspended, else 0.
429  */
430 int SIMIX_process_is_suspended(smx_process_t process)
431 {
432   xbt_assert0((process != NULL), "Invalid parameters");
433
434   return (process->suspended);
435 }
436
437 /**
438  * \brief Returns the amount of SIMIX processes in the system
439  *
440  * Maestro internal process is not counted, only user code processes are
441  */
442 int SIMIX_process_count()
443 {
444   return xbt_swag_size(simix_global->process_list);
445 }