Logo AND Algorithmique Numérique Distribuée

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