Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename some struct fields in private datatype to explicit whether they are smx object...
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_process, simix,
13                                 "Logging specific to SIMIX (process)");
14
15 /******************************** Process ************************************/
16 /**
17  * \brief Creates and runs a new #smx_process_t.
18  *
19  * Does exactly the same as #SIMIX_process_create_with_arguments but without 
20    providing standard arguments (\a argc, \a argv).
21  * \see SIMIX_process_create_with_arguments
22  */
23
24
25 void SIMIX_process_cleanup(void *arg)
26 {
27   xbt_swag_remove(arg, simix_global->process_list);
28   xbt_swag_remove(arg, simix_global->process_to_run);
29   xbt_swag_remove(arg, ((smx_process_t) arg)->simdata->s_host->simdata->process_list);
30   free(((smx_process_t) arg)->name);
31   ((smx_process_t) arg)->name = NULL;
32
33   free(((smx_process_t) arg)->simdata);
34   ((smx_process_t) arg)->simdata = NULL;
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                                    smx_process_code_t code, void *data,
54                                    const char * hostname, int argc, char **argv, 
55                                    void * clean_process_function)
56 {
57   smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t,1);
58   smx_process_t process = xbt_new0(s_smx_process_t,1);
59   smx_process_t self = NULL;
60   smx_host_t host = SIMIX_host_get_by_name(hostname);
61
62   xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
63   /* Simulator Data */
64
65   simdata->s_host = host;
66         simdata->mutex = NULL;
67         simdata->cond = NULL;
68   simdata->argc = argc;
69   simdata->argv = argv;
70         if (clean_process_function) {
71         simdata->context = xbt_context_new(code, NULL, NULL, 
72                                              clean_process_function, process, 
73                                              simdata->argc, simdata->argv);
74         }
75         else {
76         simdata->context = xbt_context_new(code, NULL, NULL, 
77                                              SIMIX_process_cleanup, process, 
78                                              simdata->argc, simdata->argv);
79         }
80
81   /* Process structure */
82   process->name = xbt_strdup(name);
83   process->simdata = simdata;
84   process->data = data;
85
86   xbt_swag_insert(process, host->simdata->process_list);
87
88   /* *************** FIX du current_process !!! *************** */
89   self = simix_global->current_process;
90   xbt_context_start(process->simdata->context);
91   simix_global->current_process = self;
92
93   xbt_swag_insert(process,simix_global->process_list);
94   DEBUG2("Inserting %s(%s) in the to_run list",process->name,
95          host->name);
96   xbt_swag_insert(process,simix_global->process_to_run);
97
98   return process;
99 }
100
101 /** 
102  * \brief Creates and runs a new #smx_process_t hosting a JAVA thread
103  *
104  * Warning: this should only be used in libsimgrid4java, since it create
105  * a context with no code, which leads to segfaults in plain libsimgrid 
106  */
107 smx_process_t SIMIX_jprocess_create(const char *name, smx_host_t host,
108                                     void *data,
109                                     void *jprocess, void *jenv)
110 {
111   smx_simdata_process_t simdata = xbt_new0(s_smx_simdata_process_t,1);
112   smx_process_t process = xbt_new0(s_smx_process_t,1);
113   smx_process_t self = NULL;
114
115   DEBUG5("jprocess_create(name=%s,host=%p,data=%p,jproc=%p,jenv=%p)",
116          name,host,data,jprocess,jenv);
117   xbt_assert0(host, "Invalid parameters");
118   /* Simulator Data */
119   simdata->s_host = host;
120   simdata->mutex = NULL;
121   simdata->cond = NULL;
122   simdata->argc = 0;
123   simdata->argv = NULL;
124   
125   simdata->context = xbt_context_new(NULL, NULL, NULL, 
126                                      SIMIX_process_cleanup, process, 
127                                      /* argc/argv*/0,NULL);
128   /* Process structure */
129   process->name = xbt_strdup(name);
130   process->simdata = simdata; 
131   process->data = data;
132   SIMIX_process_set_jprocess(process,jprocess);
133   SIMIX_process_set_jenv(process,jenv);
134
135   xbt_swag_insert(process, host->simdata->process_list);
136
137   /* *************** FIX du current_process !!! *************** */
138   self = simix_global->current_process;
139   xbt_context_start(process->simdata->context);
140   simix_global->current_process = self;
141
142   xbt_swag_insert(process,simix_global->process_list);
143   DEBUG2("Inserting %s(%s) in the to_run list",process->name,
144          host->name);
145   xbt_swag_insert(process,simix_global->process_to_run);
146
147   return process;
148 }
149
150
151 /** \brief Kill a SIMIX process
152  *
153  * This function simply kills a \a process... scarry isn't it ? :). Removes it from the mutex or condition that it can be blocked.
154  * \param process poor victim
155  *
156  */
157 void SIMIX_process_kill(smx_process_t process)
158 {       
159   smx_simdata_process_t p_simdata = process->simdata;
160
161   DEBUG2("Killing %s on %s",process->name, p_simdata->s_host->name);
162   
163         if (p_simdata->mutex) {
164                 xbt_swag_remove(process,p_simdata->mutex->sleeping);
165         }
166         if (p_simdata->cond) {
167                 xbt_swag_remove(process,p_simdata->cond->sleeping);
168         }
169   xbt_swag_remove(process,simix_global->process_to_run);
170   xbt_swag_remove(process,simix_global->process_list);
171   xbt_context_kill(process->simdata->context);
172
173   if(process==SIMIX_process_self()) {
174     /* I just killed myself */
175     xbt_context_yield();
176   }
177 }
178
179 /**
180  * \brief Return the user data of a #smx_process_t.
181  *
182  * 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.
183  * \param process SIMIX process
184  * \return A void pointer to the user data
185  */
186 void *SIMIX_process_get_data(smx_process_t process)
187 {
188   xbt_assert0((process != NULL), "Invalid parameters");
189
190   return (process->data);
191 }
192
193 /**
194  * \brief Set the user data of a #m_process_t.
195  *
196  * 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.
197  * \param process SIMIX process
198  * \param data User data
199  */
200 void SIMIX_process_set_data(smx_process_t process,void *data)
201 {
202   xbt_assert0((process != NULL), "Invalid parameters");
203   xbt_assert0((process->data == NULL), "Data already set");
204   
205   process->data = data;
206    
207   return ;
208 }
209
210 /**
211  * \brief Return the location on which an agent is running.
212  *
213  * 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.
214  * \param process SIMIX process
215  * \return SIMIX host
216  */
217 smx_host_t SIMIX_process_get_host(smx_process_t process)
218 {
219   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
220
221   return (process->simdata->s_host);
222 }
223
224 /**
225  * \brief Return the name of an agent.
226  *
227  * This functions checks whether \a process is a valid pointer or not and return its name.
228  * \param process SIMIX process
229  * \return The process name
230  */
231 const char *SIMIX_process_get_name(smx_process_t process)
232 {
233   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
234
235   return (process->name);
236 }
237
238 /**
239  * \brief Return the current agent.
240  *
241  * This functions returns the currently running #smx_process_t.
242  * \return The SIMIX process
243  */
244 smx_process_t SIMIX_process_self(void)
245 {
246   return simix_global ? simix_global->current_process : NULL;
247 }
248
249 /**
250  * \brief Suspend the process.
251  *
252  * This functions suspend the process by suspending the action on which it was waiting for the completion.
253  *      \param process SIMIX process
254  */
255 void SIMIX_process_suspend(smx_process_t process)
256 {
257   smx_simdata_process_t simdata = NULL;
258         
259   xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
260
261   if(process!=SIMIX_process_self()) {
262     simdata = process->simdata;
263     
264                 if (simdata->mutex) {
265                         /* process blocked on a mutex, only set suspend=1 */
266                         simdata->suspended = 1;
267                 }
268                 else if (simdata->cond){
269                         /* process blocked cond, suspend all actions */
270
271                         /* temporaries variables */ 
272                         smx_cond_t c;
273                         xbt_fifo_item_t i;
274                         smx_action_t act;
275
276                         simdata->suspended = 1;
277                         c = simdata->cond;
278                         xbt_fifo_foreach(c->actions,i,act, smx_action_t) {
279                                 surf_workstation_resource->common_public->suspend(act->simdata->surf_action);
280                         }
281                 }
282                 else {
283                         simdata->suspended = 1;
284                 }
285   }
286         else {
287                 /* process executing, I can create an action and suspend it */
288                 process->simdata->suspended = 1;
289                 smx_action_t dummy;
290                 smx_cond_t cond;
291                 char name[] = "dummy";
292
293                 cond = SIMIX_cond_init();
294                 dummy = SIMIX_action_execute(SIMIX_process_get_host(process), name, 0);
295                 surf_workstation_resource->common_public->set_priority(dummy->simdata->surf_action,0.0);
296                 SIMIX_register_condition_to_action(dummy,cond);
297                 SIMIX_register_action_to_condition(dummy,cond);
298                 __SIMIX_cond_wait(cond);        
299                 //SIMIX_action_destroy(dummy);
300                 //SIMIX_cond_destroy(cond);
301         }
302   return ;
303 }
304
305 /**
306  * \brief Resume a suspended process.
307  *
308  * This functions resume a suspended process by resuming the task on which it was waiting for the completion.
309  * \param process SIMIX process
310  */
311 void SIMIX_process_resume(smx_process_t process)
312 {
313   smx_simdata_process_t simdata = NULL;
314
315   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
316   SIMIX_CHECK_HOST();
317
318   if(process == SIMIX_process_self()) {
319                 return; 
320   }
321
322   simdata = process->simdata;
323   if(simdata->mutex) {
324                 DEBUG0("Resume process blocked on a mutex");
325     simdata->suspended = 0; /* He'll wake up by itself */
326                 return; 
327   }
328         else if (simdata->cond) {
329                 DEBUG0("Resume process blocked on a conditional");
330                 /* temporaries variables */ 
331                 smx_cond_t c;
332                 xbt_fifo_item_t i;
333                 smx_action_t act;
334                 simdata->suspended = 0;
335                 c = simdata->cond;
336                 xbt_fifo_foreach(c->actions,i,act, smx_action_t) {
337                         surf_workstation_resource->common_public->resume(act->simdata->surf_action);
338                 }
339                 return;
340         }
341         else {
342                 simdata->suspended = 0;
343                 xbt_swag_insert(process,simix_global->process_to_run);
344         }
345
346 }
347
348 /**
349  * \brief Returns true if the process is suspended .
350  *
351  * This checks whether a process is suspended or not by inspecting the task on which it was waiting for the completion.
352  * \param process SIMIX process
353  * \return 1, if the process is suspended, else 0.
354  */
355 int SIMIX_process_is_suspended(smx_process_t process)
356 {
357   xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
358
359   return (process->simdata->suspended);
360 }
361
362
363 /* Helper functions for jMSG: manipulate the context data without breaking the module separation */
364 #include "xbt/context.h" /* to pass java objects from MSG to the context */
365 void SIMIX_process_set_jprocess(smx_process_t process, void *jp) {
366    xbt_context_set_jprocess(process->simdata->context,jp);
367 }
368 void* SIMIX_process_get_jprocess(smx_process_t process) {
369    return xbt_context_get_jprocess(process->simdata->context);
370 }
371
372 void SIMIX_process_set_jmutex(smx_process_t process, void *jm) {
373    xbt_context_set_jmutex(process->simdata->context,jm);
374 }
375 void* SIMIX_process_get_jmutex(smx_process_t process) {
376    return xbt_context_get_jmutex(process->simdata->context);
377 }
378
379 void SIMIX_process_set_jcond(smx_process_t process, void *jc) {
380    xbt_context_set_jcond(process->simdata->context,jc);
381 }
382 void* SIMIX_process_get_jcond(smx_process_t process) {
383    return xbt_context_get_jcond(process->simdata->context);
384 }
385
386 void SIMIX_process_set_jenv(smx_process_t process, void *je) {
387    xbt_context_set_jenv(process->simdata->context,je);
388 }
389 void* SIMIX_process_get_jenv(smx_process_t process) {
390    return xbt_context_get_jenv(process->simdata->context);
391 }
392