Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
67d2328da53eaced21eb75b3611739244ba47285
[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     xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
281                 !((simdata_task->comm)&&(simdata_task->comm)),
282                 "Got a problem in deciding which action to choose !");
283     simdata->suspended = 1;
284     if(simdata_task->compute) 
285       surf_workstation_resource->extension_public->suspend(simdata_task->compute);
286     else
287       surf_workstation_resource->extension_public->suspend(simdata_task->comm);
288   } else {
289     m_task_t dummy = MSG_TASK_UNINITIALIZED;
290     dummy = MSG_task_create("suspended", 0.0, 0, NULL);
291
292     simdata->suspended = 1;
293     __MSG_task_execute(process,dummy);
294     surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
295     __MSG_wait_for_computation(process,dummy);
296     simdata->suspended = 0;
297
298     MSG_task_destroy(dummy);
299   }
300   return MSG_OK;
301 }
302
303 /** \ingroup m_process_management
304  * \brief Resume a suspended process.
305  *
306  * This functions resume a suspended process by resuming the task on
307  * which it was waiting for the completion.
308  */
309 MSG_error_t MSG_process_resume(m_process_t process)
310 {
311   simdata_process_t simdata = NULL;
312   simdata_task_t simdata_task = NULL;
313   int i;
314
315   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
316   CHECK_HOST();
317
318   simdata = process->simdata;
319
320   if(simdata->blocked) {
321     simdata->suspended = 0; /* He'll wake up by itself */
322     MSG_RETURN(MSG_OK);
323   }
324
325   if(!(simdata->waiting_task)) {
326     xbt_assert0(0,"Process not waiting for anything else. Weird !");
327     return MSG_WARNING;
328   }
329   simdata_task = simdata->waiting_task->simdata;
330
331
332   if(simdata_task->compute) 
333     surf_workstation_resource->extension_public->resume(simdata_task->compute);
334   else 
335     surf_workstation_resource->extension_public->resume(simdata_task->comm);
336
337   MSG_RETURN(MSG_OK);
338 }
339
340 /** \ingroup m_process_management
341  * \brief Returns true if the process is suspended .
342  *
343  * This checks whether a process is suspended or not by inspecting the
344  * task on which it was waiting for the completion.
345  */
346 int MSG_process_isSuspended(m_process_t process)
347 {
348   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
349
350   return (process->simdata->suspended);
351 }
352
353
354
355
356
357 MSG_error_t __MSG_process_block()
358 {
359   m_process_t process = MSG_process_self();
360
361   m_task_t dummy = MSG_TASK_UNINITIALIZED;
362   dummy = MSG_task_create("blocked", 0.0, 0, NULL);
363   
364   process->simdata->blocked=1;
365   __MSG_task_execute(process,dummy);
366   surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
367   __MSG_wait_for_computation(process,dummy);
368   process->simdata->blocked=0;
369
370   if(process->simdata->suspended)
371     MSG_process_suspend(process);
372   
373   MSG_task_destroy(dummy);
374
375   return MSG_OK;
376 }
377
378 MSG_error_t __MSG_process_unblock(m_process_t process)
379 {
380   simdata_process_t simdata = NULL;
381   simdata_task_t simdata_task = NULL;
382   int i;
383
384   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
385   CHECK_HOST();
386
387   simdata = process->simdata;
388   if(!(simdata->waiting_task)) {
389     xbt_assert0(0,"Process not waiting for anything else. Weird !");
390     return MSG_WARNING;
391   }
392   simdata_task = simdata->waiting_task->simdata;
393
394   xbt_assert0(simdata->blocked,"Process not blocked");
395
396   surf_workstation_resource->extension_public->resume(simdata_task->compute);
397
398   MSG_RETURN(MSG_OK);
399 }
400
401 int __MSG_process_isBlocked(m_process_t process)
402 {
403   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
404
405   return (process->simdata->blocked);
406 }