Logo AND Algorithmique Numérique Distribuée

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