Logo AND Algorithmique Numérique Distribuée

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