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);
50 m_process_t MSG_process_create_with_arguments(const char *name,
51 m_process_code_t code, void *data,
52 m_host_t host, int argc, char **argv)
54 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
55 m_process_t process = xbt_new0(s_m_process_t,1);
56 m_process_t self = NULL;
59 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
64 simdata->waiting_task = NULL;
67 simdata->context = xbt_context_new(code, NULL, NULL,
68 MSG_process_cleanup, process,
69 simdata->argc, simdata->argv);
71 if((self=msg_global->current_process)) {
72 simdata->PPID = MSG_process_get_PID(self);
76 simdata->last_errno=MSG_OK;
79 /* Process structure */
80 process->name = xbt_strdup(name);
81 process->simdata = simdata;
84 xbt_fifo_push(host->simdata->process_list, process);
86 /* /////////////// FIX du current_process !!! ////////////// */
87 self = msg_global->current_process;
88 xbt_context_start(process->simdata->context);
89 msg_global->current_process = self;
91 xbt_fifo_push(msg_global->process_list, process);
92 xbt_fifo_push(msg_global->process_to_run, process);
97 /** \ingroup m_process_management
98 * \brief Migrates an agent to another location.
100 * This functions checks whether \a process and \a host are valid pointers
101 and change the value of the #m_host_t on which \a process is running.
103 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
105 simdata_process_t simdata = NULL;
109 xbt_assert0(((process) && (process->simdata)
110 && (host)), "Invalid parameters");
111 simdata = process->simdata;
113 xbt_fifo_remove(simdata->host->simdata->process_list,process);
114 simdata->host = host;
115 xbt_fifo_push(host->simdata->process_list,process);
120 /** \ingroup m_process_management
121 * \brief Return the user data of a #m_process_t.
123 * This functions checks whether \a process is a valid pointer or not
124 and return the user data associated to \a process if it is possible.
126 void *MSG_process_get_data(m_process_t process)
128 xbt_assert0((process != NULL), "Invalid parameters");
130 return (process->data);
133 /** \ingroup m_process_management
134 * \brief Set the user data of a #m_process_t.
136 * This functions checks whether \a process is a valid pointer or not
137 and set the user data associated to \a process if it is possible.
139 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
141 xbt_assert0((process != NULL), "Invalid parameters");
142 xbt_assert0((process->data == NULL), "Data already set");
144 process->data = data;
149 /** \ingroup m_process_management
150 * \brief Return the location on which an agent is running.
152 * This functions checks whether \a process is a valid pointer or not
153 and return the m_host_t corresponding to the location on which \a
156 m_host_t MSG_process_get_host(m_process_t process)
158 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
160 return (((simdata_process_t) process->simdata)->host);
163 /** \ingroup m_process_management
165 * \brief Return a #m_process_t given its PID.
167 * This functions search in the list of all the created m_process_t for a m_process_t
168 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
169 Note that the PID are uniq in the whole simulation, not only on a given host.
171 m_process_t MSG_process_from_PID(int PID)
173 xbt_fifo_item_t i = NULL;
174 m_process_t process = NULL;
176 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
177 if(MSG_process_get_PID(process) == PID) return process;
182 /** \ingroup m_process_management
183 * \brief Returns the process ID of \a process.
185 * This functions checks whether \a process is a valid pointer or not
188 int MSG_process_get_PID(m_process_t process)
190 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
192 return (((simdata_process_t) process->simdata)->PID);
196 /** \ingroup m_process_management
197 * \brief Returns the process ID of the parent of \a process.
199 * This functions checks whether \a process is a valid pointer or not
200 and return its PID. Returns -1 if the agent has not been created by
203 int MSG_process_get_PPID(m_process_t process)
205 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
207 return (((simdata_process_t) process->simdata)->PPID);
210 /** \ingroup m_process_management
211 * \brief Return the name of an agent.
213 * This functions checks whether \a process is a valid pointer or not
216 const char *MSG_process_get_name(m_process_t process)
218 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
220 return (process->name);
223 /** \ingroup m_process_management
224 * \brief Return the PID of the current agent.
226 * This functions returns the PID of the currently running #m_process_t.
228 int MSG_process_self_PID(void)
230 return (MSG_process_get_PID(MSG_process_self()));
233 /** \ingroup m_process_management
234 * \brief Return the PPID of the current agent.
236 * This functions returns the PID of the parent of the currently
237 * running #m_process_t.
239 int MSG_process_self_PPID(void)
241 return (MSG_process_get_PPID(MSG_process_self()));
244 /** \ingroup m_process_management
245 * \brief Return the current agent.
247 * This functions returns the currently running #m_process_t.
249 m_process_t MSG_process_self(void)
251 return msg_global->current_process;
254 /** \ingroup m_process_management
255 * \brief Suspend the process.
257 * This functions suspend the process by suspending the task on which
258 * it was waiting for the completion.
260 MSG_error_t MSG_process_suspend(m_process_t process)
262 simdata_process_t simdata = NULL;
263 simdata_task_t simdata_task = NULL;
266 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
268 if(process!=MSG_process_self()) {
269 simdata = process->simdata;
271 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
273 simdata_task = simdata->waiting_task->simdata;
275 xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
276 !((simdata_task->comm)&&(simdata_task->comm)),
277 "Got a problem in deciding which action to choose !");
278 if(simdata_task->compute)
279 surf_workstation_resource->extension_public->suspend(simdata_task->compute);
281 surf_workstation_resource->extension_public->suspend(simdata_task->comm);
283 m_task_t dummy = MSG_TASK_UNINITIALIZED;
284 dummy = MSG_task_create("suspended", 0.0, 0, NULL);
286 __MSG_task_execute(process,dummy);
287 surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
288 __MSG_wait_for_computation(process,dummy);
290 MSG_task_destroy(dummy);
295 /** \ingroup m_process_management
296 * \brief Resume a suspended process.
298 * This functions resume a suspended process by resuming the task on
299 * which it was waiting for the completion.
301 MSG_error_t MSG_process_resume(m_process_t process)
303 simdata_process_t simdata = NULL;
304 simdata_task_t simdata_task = NULL;
307 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
310 simdata = process->simdata;
311 if(!(simdata->waiting_task)) {
312 xbt_assert0(0,"Process not waiting for anything else. Weird !");
315 simdata_task = simdata->waiting_task->simdata;
317 if(simdata_task->compute)
318 surf_workstation_resource->extension_public->resume(simdata_task->compute);
320 surf_workstation_resource->extension_public->resume(simdata_task->comm);
325 /** \ingroup m_process_management
326 * \brief Returns true if the process is suspended .
328 * This checks whether a process is suspended or not by inspecting the
329 * task on which it was waiting for the completion.
331 int MSG_process_isSuspended(m_process_t process)
333 simdata_process_t simdata = NULL;
334 simdata_task_t simdata_task = NULL;
337 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
339 simdata = process->simdata;
340 if(!(simdata->waiting_task)) {
341 xbt_assert0(0,"Process not waiting for anything else. Weird !");
345 simdata_task = simdata->waiting_task->simdata;
347 if(simdata_task->compute)
348 return surf_workstation_resource->extension_public->is_suspended(simdata_task->compute);
350 return surf_workstation_resource->extension_public->is_suspended(simdata_task->comm);