Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No need to rebuild the parser if you're not a maintainer.
[simgrid.git] / src / msg / m_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/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(m_process, msg,
12                                 "Logging specific to MSG (process)");
13
14 /******************************** Process ************************************/
15 /** \ingroup m_process_management
16  * \brief Creates and runs a new #m_process_t.
17  *
18  * Does exactly the same as #MSG_process_create_with_arguments but without 
19    providing standard arguments (\a argc, \a argv).
20  * \sa MSG_process_create_with_arguments
21  */
22 m_process_t MSG_process_create(const char *name,
23                                m_process_code_t code, void *data,
24                                m_host_t host)
25 {
26   return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
27 }
28
29 static void MSG_process_cleanup(void *arg)
30 {
31   xbt_fifo_remove(msg_global->process_list, arg);
32   xbt_fifo_remove(msg_global->process_to_run, arg);
33   xbt_fifo_remove(((m_process_t) arg)->simdata->host->simdata->process_list, arg);
34   xbt_free(((m_process_t) arg)->name);
35   xbt_free(((m_process_t) arg)->simdata);
36   xbt_free(arg);
37 }
38
39 /** \ingroup m_process_management
40  * \brief Creates and runs a new #m_process_t.
41
42  * A constructor for #m_process_t taking four arguments and returning the 
43  * corresponding object. The structure (and the corresponding thread) is
44  * created, and put in the list of ready process.
45  * \param name a name for the object. It is for user-level information
46    and can be NULL.
47  * \param code is a function describing the behavior of the agent. It
48    should then only use functions described in \ref
49    m_process_management (to create a new #m_process_t for example),
50    in \ref m_host_management (only the read-only functions i.e. whose
51    name contains the word get), in \ref m_task_management (to create
52    or destroy some #m_task_t for example) and in \ref
53    msg_gos_functions (to handle file transfers and task processing).
54  * \param data a pointer to any data may want to attach to the new
55    object.  It is for user-level information and can be NULL. It can
56    be retrieved with the function \ref MSG_process_get_data.
57  * \param host the location where the new agent is executed.
58  * \param argc first argument passed to \a code
59  * \param argv second argument passed to \a code
60  * \see m_process_t
61  * \return The new corresponding object.
62  */
63 m_process_t MSG_process_create_with_arguments(const char *name,
64                                               m_process_code_t code, void *data,
65                                               m_host_t host, int argc, char **argv)
66 {
67   simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
68   m_process_t process = xbt_new0(s_m_process_t,1);
69   m_process_t self = NULL;
70   static int PID = 1;
71
72   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
73   /* Simulator Data */
74
75   simdata->PID = PID++;
76   simdata->host = host;
77   simdata->waiting_task = NULL;
78   simdata->argc = argc;
79   simdata->argv = argv;
80   simdata->context = xbt_context_new(code, NULL, NULL, 
81                                      MSG_process_cleanup, process, 
82                                      simdata->argc, simdata->argv);
83
84   if((self=msg_global->current_process)) {
85     simdata->PPID = MSG_process_get_PID(self);
86   } else {
87     simdata->PPID = -1;
88   }
89   simdata->last_errno=MSG_OK;
90
91
92   /* Process structure */
93   process->name = xbt_strdup(name);
94   process->simdata = simdata;
95   process->data = data;
96
97   xbt_fifo_push(host->simdata->process_list, process);
98
99   /* /////////////// FIX du current_process !!! ////////////// */
100   self = msg_global->current_process;
101   xbt_context_start(process->simdata->context);
102   msg_global->current_process = self;
103
104   xbt_fifo_push(msg_global->process_list, process);
105   xbt_fifo_push(msg_global->process_to_run, process);
106
107   return process;
108 }
109
110 /** \ingroup m_process_management
111  * \param process poor victim
112  *
113  * This function simply kills a \a process... scarry isn't it ? :)
114  */
115 void MSG_process_kill(m_process_t process)
116 {
117   xbt_fifo_remove(msg_global->process_list,process);
118   xbt_context_free(process->simdata->context);
119   MSG_process_cleanup(process);
120 }
121
122 /** \ingroup m_process_management
123  * \brief Migrates an agent to another location.
124  *
125  * This functions checks whether \a process and \a host are valid pointers
126    and change the value of the #m_host_t on which \a process is running.
127  */
128 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
129 {
130   simdata_process_t simdata = NULL;
131
132   /* Sanity check */
133
134   xbt_assert0(((process) && (process->simdata)
135           && (host)), "Invalid parameters");
136   simdata = process->simdata;
137
138   xbt_fifo_remove(simdata->host->simdata->process_list,process);
139   simdata->host = host;
140   xbt_fifo_push(host->simdata->process_list,process);
141
142   return MSG_OK;
143 }
144
145 /** \ingroup m_process_management
146  * \brief Return the user data of a #m_process_t.
147  *
148  * This functions checks whether \a process is a valid pointer or not 
149    and return the user data associated to \a process if it is possible.
150  */
151 void *MSG_process_get_data(m_process_t process)
152 {
153   xbt_assert0((process != NULL), "Invalid parameters");
154
155   return (process->data);
156 }
157
158 /** \ingroup m_process_management
159  * \brief Set the user data of a #m_process_t.
160  *
161  * This functions checks whether \a process is a valid pointer or not 
162    and set the user data associated to \a process if it is possible.
163  */
164 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
165 {
166   xbt_assert0((process != NULL), "Invalid parameters");
167   xbt_assert0((process->data == NULL), "Data already set");
168   
169   process->data = data;
170    
171   return MSG_OK;
172 }
173
174 /** \ingroup m_process_management
175  * \brief Return the location on which an agent is running.
176  *
177  * This functions checks whether \a process is a valid pointer or not 
178    and return the m_host_t corresponding to the location on which \a 
179    process is running.
180  */
181 m_host_t MSG_process_get_host(m_process_t process)
182 {
183   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
184
185   return (((simdata_process_t) process->simdata)->host);
186 }
187
188 /** \ingroup m_process_management
189  *
190  * \brief Return a #m_process_t given its PID.
191  *
192  * This functions search in the list of all the created m_process_t for a m_process_t 
193    whose PID is equal to \a PID. If no host is found, \c NULL is returned. 
194    Note that the PID are uniq in the whole simulation, not only on a given host.
195  */
196 m_process_t MSG_process_from_PID(int PID)
197 {
198   xbt_fifo_item_t i = NULL;
199   m_process_t process = NULL;
200
201   xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
202     if(MSG_process_get_PID(process) == PID) return process;
203   }
204   return NULL;
205 }
206
207 /** \ingroup m_process_management
208  * \brief Returns the process ID of \a process.
209  *
210  * This functions checks whether \a process is a valid pointer or not 
211    and return its PID.
212  */
213 int MSG_process_get_PID(m_process_t process)
214 {
215   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
216
217   return (((simdata_process_t) process->simdata)->PID);
218 }
219
220 /** \ingroup m_process_management
221  * \brief Returns the process ID of the parent of \a process.
222  *
223  * This functions checks whether \a process is a valid pointer or not 
224    and return its PID. Returns -1 if the agent has not been created by 
225    another agent.
226  */
227 int MSG_process_get_PPID(m_process_t process)
228 {
229   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
230
231   return (((simdata_process_t) process->simdata)->PPID);
232 }
233
234 /** \ingroup m_process_management
235  * \brief Return the name of an agent.
236  *
237  * This functions checks whether \a process is a valid pointer or not 
238    and return its name.
239  */
240 const char *MSG_process_get_name(m_process_t process)
241 {
242   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
243
244   return (process->name);
245 }
246
247 /** \ingroup m_process_management
248  * \brief Return the PID of the current agent.
249  *
250  * This functions returns the PID of the currently running #m_process_t.
251  */
252 int MSG_process_self_PID(void)
253 {
254   return (MSG_process_get_PID(MSG_process_self()));
255 }
256
257 /** \ingroup m_process_management
258  * \brief Return the PPID of the current agent.
259  *
260  * This functions returns the PID of the parent of the currently
261  * running #m_process_t.
262  */
263 int MSG_process_self_PPID(void)
264 {
265   return (MSG_process_get_PPID(MSG_process_self()));
266 }
267
268 /** \ingroup m_process_management
269  * \brief Return the current agent.
270  *
271  * This functions returns the currently running #m_process_t.
272  */
273 m_process_t MSG_process_self(void)
274 {
275   return msg_global->current_process;
276 }
277
278 /** \ingroup m_process_management
279  * \brief Suspend the process.
280  *
281  * This functions suspend the process by suspending the task on which
282  * it was waiting for the completion.
283  */
284 MSG_error_t MSG_process_suspend(m_process_t process)
285 {
286   simdata_process_t simdata = NULL;
287   simdata_task_t simdata_task = NULL;
288   int i;
289
290   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
291
292   if(process!=MSG_process_self()) {
293     simdata = process->simdata;
294     
295     xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
296
297     simdata_task = simdata->waiting_task->simdata;
298
299     simdata->suspended = 1;
300     if(simdata->blocked) return MSG_OK;
301
302     xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
303                 !((simdata_task->compute)&&(simdata_task->comm)),
304                 "Got a problem in deciding which action to choose !");
305     simdata->suspended = 1;
306     if(simdata_task->compute) 
307       surf_workstation_resource->common_public->suspend(simdata_task->compute);
308     else
309       surf_workstation_resource->common_public->suspend(simdata_task->comm);
310   } else {
311     m_task_t dummy = MSG_TASK_UNINITIALIZED;
312     dummy = MSG_task_create("suspended", 0.0, 0, NULL);
313
314     simdata = process->simdata;
315     simdata->suspended = 1;
316     __MSG_task_execute(process,dummy);
317     surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
318     __MSG_wait_for_computation(process,dummy);
319     simdata->suspended = 0;
320
321     MSG_task_destroy(dummy);
322   }
323   return MSG_OK;
324 }
325
326 /** \ingroup m_process_management
327  * \brief Resume a suspended process.
328  *
329  * This functions resume a suspended process by resuming the task on
330  * which it was waiting for the completion.
331  */
332 MSG_error_t MSG_process_resume(m_process_t process)
333 {
334   simdata_process_t simdata = NULL;
335   simdata_task_t simdata_task = NULL;
336   int i;
337
338   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
339   CHECK_HOST();
340
341   simdata = process->simdata;
342
343   if(simdata->blocked) {
344     simdata->suspended = 0; /* He'll wake up by itself */
345     MSG_RETURN(MSG_OK);
346   }
347
348   if(!(simdata->waiting_task)) {
349     xbt_assert0(0,"Process not waiting for anything else. Weird !");
350     return MSG_WARNING;
351   }
352   simdata_task = simdata->waiting_task->simdata;
353
354
355   if(simdata_task->compute) 
356     surf_workstation_resource->common_public->resume(simdata_task->compute);
357   else 
358     surf_workstation_resource->common_public->resume(simdata_task->comm);
359
360   MSG_RETURN(MSG_OK);
361 }
362
363 /** \ingroup m_process_management
364  * \brief Returns true if the process is suspended .
365  *
366  * This checks whether a process is suspended or not by inspecting the
367  * task on which it was waiting for the completion.
368  */
369 int MSG_process_isSuspended(m_process_t process)
370 {
371   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
372
373   return (process->simdata->suspended);
374 }
375
376 MSG_error_t __MSG_process_block(void)
377 {
378   m_process_t process = MSG_process_self();
379
380   m_task_t dummy = MSG_TASK_UNINITIALIZED;
381   dummy = MSG_task_create("blocked", 0.0, 0, NULL);
382   
383   process->simdata->blocked=1;
384   __MSG_task_execute(process,dummy);
385   surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
386   __MSG_wait_for_computation(process,dummy);
387   process->simdata->blocked=0;
388
389   if(process->simdata->suspended)
390     MSG_process_suspend(process);
391   
392   MSG_task_destroy(dummy);
393
394   return MSG_OK;
395 }
396
397 MSG_error_t __MSG_process_unblock(m_process_t process)
398 {
399   simdata_process_t simdata = NULL;
400   simdata_task_t simdata_task = NULL;
401   int i;
402
403   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
404   CHECK_HOST();
405
406   simdata = process->simdata;
407   if(!(simdata->waiting_task)) {
408     xbt_assert0(0,"Process not waiting for anything else. Weird !");
409     return MSG_WARNING;
410   }
411   simdata_task = simdata->waiting_task->simdata;
412
413   xbt_assert0(simdata->blocked,"Process not blocked");
414
415   surf_workstation_resource->common_public->resume(simdata_task->compute);
416
417   MSG_RETURN(MSG_OK);
418 }
419
420 int __MSG_process_isBlocked(m_process_t process)
421 {
422   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
423
424   return (process->simdata->blocked);
425 }