3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
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. */
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(m_process, msg,
12 "Logging specific to MSG (process)");
14 /******************************** Process ************************************/
15 /** \ingroup m_process_management
16 * \brief Creates and runs a new #m_process_t.
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
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.
35 * \return The new corresponding object.
37 m_process_t MSG_process_create(const char *name,
38 m_process_code_t code, void *data,
41 return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
44 static void MSG_process_cleanup(void *arg)
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);
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)
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;
64 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
69 simdata->waiting_task = NULL;
72 simdata->context = xbt_context_new(code, NULL, NULL,
73 MSG_process_cleanup, process,
74 simdata->argc, simdata->argv);
76 if((self=msg_global->current_process)) {
77 simdata->PPID = MSG_process_get_PID(self);
81 simdata->last_errno=MSG_OK;
84 /* Process structure */
85 process->name = xbt_strdup(name);
86 process->simdata = simdata;
89 xbt_fifo_push(host->simdata->process_list, process);
91 /* /////////////// FIX du current_process !!! ////////////// */
92 self = msg_global->current_process;
93 xbt_context_start(process->simdata->context);
94 msg_global->current_process = self;
96 xbt_fifo_push(msg_global->process_list, process);
97 xbt_fifo_push(msg_global->process_to_run, process);
102 /** \ingroup m_process_management
103 * \brief Migrates an agent to another location.
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.
108 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
110 simdata_process_t simdata = NULL;
114 xbt_assert0(((process) && (process->simdata)
115 && (host)), "Invalid parameters");
116 simdata = process->simdata;
118 xbt_fifo_remove(simdata->host->simdata->process_list,process);
119 simdata->host = host;
120 xbt_fifo_push(host->simdata->process_list,process);
125 /** \ingroup m_process_management
126 * \brief Return the user data of a #m_process_t.
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.
131 void *MSG_process_get_data(m_process_t process)
133 xbt_assert0((process != NULL), "Invalid parameters");
135 return (process->data);
138 /** \ingroup m_process_management
139 * \brief Set the user data of a #m_process_t.
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.
144 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
146 xbt_assert0((process != NULL), "Invalid parameters");
147 xbt_assert0((process->data == NULL), "Data already set");
149 process->data = data;
154 /** \ingroup m_process_management
155 * \brief Return the location on which an agent is running.
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
161 m_host_t MSG_process_get_host(m_process_t process)
163 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
165 return (((simdata_process_t) process->simdata)->host);
168 /** \ingroup m_process_management
170 * \brief Return a #m_process_t given its PID.
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.
176 m_process_t MSG_process_from_PID(int PID)
178 xbt_fifo_item_t i = NULL;
179 m_process_t process = NULL;
181 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
182 if(MSG_process_get_PID(process) == PID) return process;
187 /** \ingroup m_process_management
188 * \brief Returns the process ID of \a process.
190 * This functions checks whether \a process is a valid pointer or not
193 int MSG_process_get_PID(m_process_t process)
195 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
197 return (((simdata_process_t) process->simdata)->PID);
201 /** \ingroup m_process_management
202 * \brief Returns the process ID of the parent of \a process.
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
208 int MSG_process_get_PPID(m_process_t process)
210 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
212 return (((simdata_process_t) process->simdata)->PPID);
215 /** \ingroup m_process_management
216 * \brief Return the name of an agent.
218 * This functions checks whether \a process is a valid pointer or not
221 const char *MSG_process_get_name(m_process_t process)
223 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
225 return (process->name);
228 /** \ingroup m_process_management
229 * \brief Return the PID of the current agent.
231 * This functions returns the PID of the currently running #m_process_t.
233 int MSG_process_self_PID(void)
235 return (MSG_process_get_PID(MSG_process_self()));
238 /** \ingroup m_process_management
239 * \brief Return the PPID of the current agent.
241 * This functions returns the PID of the parent of the currently
242 * running #m_process_t.
244 int MSG_process_self_PPID(void)
246 return (MSG_process_get_PPID(MSG_process_self()));
249 /** \ingroup m_process_management
250 * \brief Return the current agent.
252 * This functions returns the currently running #m_process_t.
254 m_process_t MSG_process_self(void)
256 return msg_global->current_process;
259 /** \ingroup m_process_management
260 * \brief Suspend the process.
262 * This functions suspend the process by suspending the task on which
263 * it was waiting for the completion.
265 MSG_error_t MSG_process_suspend(m_process_t process)
267 simdata_process_t simdata = NULL;
268 simdata_task_t simdata_task = NULL;
271 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
273 if(process!=MSG_process_self()) {
274 simdata = process->simdata;
276 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
278 simdata_task = simdata->waiting_task->simdata;
280 simdata->suspended = 1;
281 if(simdata->blocked) return MSG_OK;
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);
290 surf_workstation_resource->extension_public->suspend(simdata_task->comm);
292 m_task_t dummy = MSG_TASK_UNINITIALIZED;
293 dummy = MSG_task_create("suspended", 0.0, 0, NULL);
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;
302 MSG_task_destroy(dummy);
307 /** \ingroup m_process_management
308 * \brief Resume a suspended process.
310 * This functions resume a suspended process by resuming the task on
311 * which it was waiting for the completion.
313 MSG_error_t MSG_process_resume(m_process_t process)
315 simdata_process_t simdata = NULL;
316 simdata_task_t simdata_task = NULL;
319 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
322 simdata = process->simdata;
324 if(simdata->blocked) {
325 simdata->suspended = 0; /* He'll wake up by itself */
329 if(!(simdata->waiting_task)) {
330 xbt_assert0(0,"Process not waiting for anything else. Weird !");
333 simdata_task = simdata->waiting_task->simdata;
336 if(simdata_task->compute)
337 surf_workstation_resource->extension_public->resume(simdata_task->compute);
339 surf_workstation_resource->extension_public->resume(simdata_task->comm);
344 /** \ingroup m_process_management
345 * \brief Returns true if the process is suspended .
347 * This checks whether a process is suspended or not by inspecting the
348 * task on which it was waiting for the completion.
350 int MSG_process_isSuspended(m_process_t process)
352 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
354 return (process->simdata->suspended);
361 MSG_error_t __MSG_process_block(void)
363 m_process_t process = MSG_process_self();
365 m_task_t dummy = MSG_TASK_UNINITIALIZED;
366 dummy = MSG_task_create("blocked", 0.0, 0, NULL);
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;
374 if(process->simdata->suspended)
375 MSG_process_suspend(process);
377 MSG_task_destroy(dummy);
382 MSG_error_t __MSG_process_unblock(m_process_t process)
384 simdata_process_t simdata = NULL;
385 simdata_task_t simdata_task = NULL;
388 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
391 simdata = process->simdata;
392 if(!(simdata->waiting_task)) {
393 xbt_assert0(0,"Process not waiting for anything else. Weird !");
396 simdata_task = simdata->waiting_task->simdata;
398 xbt_assert0(simdata->blocked,"Process not blocked");
400 surf_workstation_resource->extension_public->resume(simdata_task->compute);
405 int __MSG_process_isBlocked(m_process_t process)
407 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
409 return (process->simdata->blocked);