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;
52 simdata->put_host = NULL;
53 simdata->put_channel = -1;
56 simdata->context = xbt_context_new(code, simdata->argc, simdata->argv);
58 if((self=msg_global->current_process)) {
59 simdata->PPID = MSG_process_get_PID(self);
63 simdata->last_errno=MSG_OK;
66 /* Process structure */
67 process->name = xbt_strdup(name);
68 process->simdata = simdata;
71 xbt_fifo_push(host->simdata->process_list, process);
73 /* /////////////// FIX du current_process !!! ////////////// */
74 self = msg_global->current_process;
75 xbt_context_start(process->simdata->context);
76 msg_global->current_process = self;
80 /** \ingroup m_process_management
81 * \brief Migrates an agent to another location.
83 * This functions checks whether \a process and \a host are valid pointers
84 and change the value of the #m_host_t on which \a process is running.
86 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
88 simdata_process_t simdata = NULL;
92 xbt_assert0(((process) && (process->simdata)
93 && (host)), "Invalid parameters");
94 simdata = process->simdata;
96 xbt_fifo_remove(simdata->host->simdata->process_list,process);
98 xbt_fifo_push(host->simdata->process_list,process);
103 /** \ingroup m_process_management
104 * \brief Return the user data of a #m_process_t.
106 * This functions checks whether \a process is a valid pointer or not
107 and return the user data associated to \a process if it is possible.
109 void *MSG_process_get_data(m_process_t process)
111 xbt_assert0((process != NULL), "Invalid parameters");
113 return (process->data);
116 /** \ingroup m_process_management
117 * \brief Set the user data of a #m_process_t.
119 * This functions checks whether \a process is a valid pointer or not
120 and set the user data associated to \a process if it is possible.
122 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
124 xbt_assert0((process != NULL), "Invalid parameters");
125 xbt_assert0((process->data == NULL), "Data already set");
127 process->data = data;
132 /** \ingroup m_process_management
133 * \brief Return the location on which an agent is running.
135 * This functions checks whether \a process is a valid pointer or not
136 and return the m_host_t corresponding to the location on which \a
139 m_host_t MSG_process_get_host(m_process_t process)
141 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
143 return (((simdata_process_t) process->simdata)->host);
146 /** \ingroup m_process_management
148 * \brief Return a #m_process_t given its PID.
150 * This functions search in the list of all the created m_process_t for a m_process_t
151 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
152 Note that the PID are uniq in the whole simulation, not only on a given host.
154 m_process_t MSG_process_from_PID(int PID)
156 xbt_fifo_item_t i = NULL;
157 m_process_t process = NULL;
159 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
160 if(MSG_process_get_PID(process) == PID) return process;
165 /** \ingroup m_process_management
166 * \brief Returns the process ID of \a process.
168 * This functions checks whether \a process is a valid pointer or not
171 int MSG_process_get_PID(m_process_t process)
173 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
175 return (((simdata_process_t) process->simdata)->PID);
179 /** \ingroup m_process_management
180 * \brief Returns the process ID of the parent of \a process.
182 * This functions checks whether \a process is a valid pointer or not
183 and return its PID. Returns -1 if the agent has not been created by
186 int MSG_process_get_PPID(m_process_t process)
188 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
190 return (((simdata_process_t) process->simdata)->PPID);
193 /** \ingroup m_process_management
194 * \brief Return the name of an agent.
196 * This functions checks whether \a process is a valid pointer or not
199 const char *MSG_process_get_name(m_process_t process)
201 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
203 return (process->name);
206 /** \ingroup m_process_management
207 * \brief Return the PID of the current agent.
209 * This functions returns the PID of the currently running #m_process_t.
211 int MSG_process_self_PID(void)
213 return (MSG_process_get_PID(MSG_process_self()));
216 /** \ingroup m_process_management
217 * \brief Return the PPID of the current agent.
219 * This functions returns the PID of the parent of the currently
220 * running #m_process_t.
222 int MSG_process_self_PPID(void)
224 return (MSG_process_get_PPID(MSG_process_self()));
227 /** \ingroup m_process_management
228 * \brief Return the current agent.
230 * This functions returns the currently running #m_process_t.
232 m_process_t MSG_process_self(void)
234 return msg_global->current_process;
237 /** \ingroup m_process_management
238 * \brief Suspend the process.
240 * This functions suspend the process by suspending the task on which
241 * it was waiting for the completion.
243 MSG_error_t MSG_process_suspend(m_process_t process)
245 simdata_process_t simdata = NULL;
246 simdata_task_t simdata_task = NULL;
249 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
251 if(process!=MSG_process_self()) {
252 simdata = process->simdata;
254 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
256 simdata_task = simdata->waiting_task->simdata;
258 xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
259 !((simdata_task->comm)&&(simdata_task->comm)),
260 "Got a problem in deciding which action to choose !");
261 if(simdata_task->compute)
262 surf_workstation_resource->extension_public->suspend(simdata_task->compute);
264 surf_workstation_resource->extension_public->suspend(simdata_task->comm);
267 m_task_t dummy = MSG_TASK_UNINITIALIZED;
268 dummy = MSG_task_create("suspended", 0.01, 0, NULL);
270 __MSG_task_execute(process,dummy);
271 surf_workstation_resource->extension_public->suspend(dummy->simdata->compute);
272 __MSG_wait_for_computation(process,dummy);
274 MSG_task_destroy(dummy);
279 /** \ingroup m_process_management
280 * \brief Resume a suspended process.
282 * This functions resume a suspended process by resuming the task on
283 * which it was waiting for the completion.
285 MSG_error_t MSG_process_resume(m_process_t process)
287 simdata_process_t simdata = NULL;
288 simdata_task_t simdata_task = NULL;
291 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
294 simdata = process->simdata;
295 if(!(simdata->waiting_task)) {
296 xbt_assert0(0,"Process not waiting for anything else. Weird !");
299 simdata_task = simdata->waiting_task->simdata;
301 if(simdata_task->compute)
302 surf_workstation_resource->extension_public->resume(simdata_task->compute);
304 surf_workstation_resource->extension_public->resume(simdata_task->comm);
309 /** \ingroup m_process_management
310 * \brief Returns true if the process is suspended .
312 * This checks whether a process is suspended or not by inspecting the
313 * task on which it was waiting for the completion.
315 int MSG_process_isSuspended(m_process_t process)
317 simdata_process_t simdata = NULL;
318 simdata_task_t simdata_task = NULL;
321 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
323 simdata = process->simdata;
324 if(!(simdata->waiting_task)) {
325 xbt_assert0(0,"Process not waiting for anything else. Weird !");
329 simdata_task = simdata->waiting_task->simdata;
331 if(simdata_task->compute)
332 return surf_workstation_resource->extension_public->is_suspended(simdata_task->compute);
334 return surf_workstation_resource->extension_public->is_suspended(simdata_task->comm);