Logo AND Algorithmique Numérique Distribuée

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