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 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
42 m_process_t process = xbt_new0(s_m_process_t,1);
43 m_process_t self = NULL;
46 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
51 simdata->waiting_task = NULL;
54 simdata->context = xbt_context_new(code, simdata->argc, simdata->argv);
56 if((self=msg_global->current_process)) {
57 simdata->PPID = MSG_process_get_PID(self);
61 simdata->last_errno=MSG_OK;
64 /* Process structure */
65 process->name = xbt_strdup(name);
66 process->simdata = simdata;
69 xbt_fifo_push(host->simdata->process_list, process);
71 /* /////////////// FIX du current_process !!! ////////////// */
72 self = msg_global->current_process;
73 xbt_context_start(process->simdata->context);
74 msg_global->current_process = self;
78 /** \ingroup m_process_management
79 * \brief Migrates an agent to another location.
81 * This functions checks whether \a process and \a host are valid pointers
82 and change the value of the #m_host_t on which \a process is running.
84 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
86 simdata_process_t simdata = NULL;
90 xbt_assert0(((process) && (process->simdata)
91 && (host)), "Invalid parameters");
92 simdata = process->simdata;
94 xbt_fifo_remove(simdata->host->simdata->process_list,process);
96 xbt_fifo_push(host->simdata->process_list,process);
101 /** \ingroup m_process_management
102 * \brief Return the user data of a #m_process_t.
104 * This functions checks whether \a process is a valid pointer or not
105 and return the user data associated to \a process if it is possible.
107 void *MSG_process_get_data(m_process_t process)
109 xbt_assert0((process != NULL), "Invalid parameters");
111 return (process->data);
114 /** \ingroup m_process_management
115 * \brief Set the user data of a #m_process_t.
117 * This functions checks whether \a process is a valid pointer or not
118 and set the user data associated to \a process if it is possible.
120 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
122 xbt_assert0((process != NULL), "Invalid parameters");
123 xbt_assert0((process->data == NULL), "Data already set");
125 process->data = data;
130 /** \ingroup m_process_management
131 * \brief Return the location on which an agent is running.
133 * This functions checks whether \a process is a valid pointer or not
134 and return the m_host_t corresponding to the location on which \a
137 m_host_t MSG_process_get_host(m_process_t process)
139 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
141 return (((simdata_process_t) process->simdata)->host);
144 /** \ingroup m_process_management
146 * \brief Return a #m_process_t given its PID.
148 * This functions search in the list of all the created m_process_t for a m_process_t
149 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
150 Note that the PID are uniq in the whole simulation, not only on a given host.
152 m_process_t MSG_process_from_PID(int PID)
154 xbt_fifo_item_t i = NULL;
155 m_process_t process = NULL;
157 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
158 if(MSG_process_get_PID(process) == PID) return process;
163 /** \ingroup m_process_management
164 * \brief Returns the process ID of \a process.
166 * This functions checks whether \a process is a valid pointer or not
169 int MSG_process_get_PID(m_process_t process)
171 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
173 return (((simdata_process_t) process->simdata)->PID);
177 /** \ingroup m_process_management
178 * \brief Returns the process ID of the parent of \a process.
180 * This functions checks whether \a process is a valid pointer or not
181 and return its PID. Returns -1 if the agent has not been created by
184 int MSG_process_get_PPID(m_process_t process)
186 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
188 return (((simdata_process_t) process->simdata)->PPID);
191 /** \ingroup m_process_management
192 * \brief Return the name of an agent.
194 * This functions checks whether \a process is a valid pointer or not
197 const char *MSG_process_get_name(m_process_t process)
199 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
201 return (process->name);
204 /** \ingroup m_process_management
205 * \brief Return the PID of the current agent.
207 * This functions returns the PID of the currently running #m_process_t.
209 int MSG_process_self_PID(void)
211 return (MSG_process_get_PID(MSG_process_self()));
214 /** \ingroup m_process_management
215 * \brief Return the PPID of the current agent.
217 * This functions returns the PID of the parent of the currently
218 * running #m_process_t.
220 int MSG_process_self_PPID(void)
222 return (MSG_process_get_PPID(MSG_process_self()));
225 /** \ingroup m_process_management
226 * \brief Return the current agent.
228 * This functions returns the currently running #m_process_t.
230 m_process_t MSG_process_self(void)
232 return msg_global->current_process;
235 /** \ingroup m_process_management
236 * \brief Suspend the process.
238 * This functions suspend the process by suspending the task on which
239 * it was waiting for the completion.
241 MSG_error_t MSG_process_suspend(m_process_t process)
243 simdata_process_t simdata = NULL;
244 simdata_task_t simdata_task = NULL;
247 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
249 if(process!=MSG_process_self()) {
250 simdata = process->simdata;
252 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
254 simdata_task = simdata->waiting_task->simdata;
256 xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
257 !((simdata_task->comm)&&(simdata_task->comm)),
258 "Got a problem in deciding which action to choose !");
259 if(simdata_task->compute)
260 surf_workstation_resource->extension_public->suspend(simdata_task->compute);
262 surf_workstation_resource->extension_public->suspend(simdata_task->comm);
265 m_task_t dummy = MSG_TASK_UNINITIALIZED;
266 dummy = MSG_task_create("suspended", 0.01, 0, NULL);
268 __MSG_task_execute(process,dummy);
269 surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
270 __MSG_wait_for_computation(process,dummy);
272 MSG_task_destroy(dummy);
277 /** \ingroup m_process_management
278 * \brief Resume a suspended process.
280 * This functions resume a suspended process by resuming the task on
281 * which it was waiting for the completion.
283 MSG_error_t MSG_process_resume(m_process_t process)
285 simdata_process_t simdata = NULL;
286 simdata_task_t simdata_task = NULL;
289 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
292 simdata = process->simdata;
293 if(!(simdata->waiting_task)) {
294 xbt_assert0(0,"Process not waiting for anything else. Weird !");
297 simdata_task = simdata->waiting_task->simdata;
299 if(simdata_task->compute)
300 surf_workstation_resource->extension_public->resume(simdata_task->compute);
302 surf_workstation_resource->extension_public->resume(simdata_task->comm);
307 /** \ingroup m_process_management
308 * \brief Returns true if the process is suspended .
310 * This checks whether a process is suspended or not by inspecting the
311 * task on which it was waiting for the completion.
313 int MSG_process_isSuspended(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");
321 simdata = process->simdata;
322 if(!(simdata->waiting_task)) {
323 xbt_assert0(0,"Process not waiting for anything else. Weird !");
327 simdata_task = simdata->waiting_task->simdata;
329 if(simdata_task->compute)
330 return surf_workstation_resource->extension_public->is_suspended(simdata_task->compute);
332 return surf_workstation_resource->extension_public->is_suspended(simdata_task->comm);