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 * Does exactly the same as #MSG_process_create_with_arguments but without
19 providing standard arguments (\a argc, \a argv).
20 * \sa MSG_process_create_with_arguments
22 m_process_t MSG_process_create(const char *name,
23 m_process_code_t code, void *data,
26 return MSG_process_create_with_arguments(name, code, data, host, -1, NULL);
29 static void MSG_process_cleanup(void *arg)
31 xbt_fifo_remove(msg_global->process_list, arg);
32 xbt_fifo_remove(msg_global->process_to_run, arg);
33 xbt_fifo_remove(((m_process_t) arg)->simdata->host->simdata->process_list, arg);
34 xbt_free(((m_process_t) arg)->name);
35 xbt_free(((m_process_t) arg)->simdata);
39 /** \ingroup m_process_management
40 * \brief Creates and runs a new #m_process_t.
42 * A constructor for #m_process_t taking four arguments and returning the
43 * corresponding object. The structure (and the corresponding thread) is
44 * created, and put in the list of ready process.
45 * \param name a name for the object. It is for user-level information
47 * \param code is a function describing the behavior of the agent. It
48 should then only use functions described in \ref
49 m_process_management (to create a new #m_process_t for example),
50 in \ref m_host_management (only the read-only functions i.e. whose
51 name contains the word get), in \ref m_task_management (to create
52 or destroy some #m_task_t for example) and in \ref
53 msg_gos_functions (to handle file transfers and task processing).
54 * \param data a pointer to any data may want to attach to the new
55 object. It is for user-level information and can be NULL. It can
56 be retrieved with the function \ref MSG_process_get_data.
57 * \param host the location where the new agent is executed.
58 * \param argc first argument passed to \a code
59 * \param argv second argument passed to \a code
61 * \return The new corresponding object.
63 m_process_t MSG_process_create_with_arguments(const char *name,
64 m_process_code_t code, void *data,
65 m_host_t host, int argc, char **argv)
67 simdata_process_t simdata = xbt_new0(s_simdata_process_t,1);
68 m_process_t process = xbt_new0(s_m_process_t,1);
69 m_process_t self = NULL;
72 xbt_assert0(((code != NULL) && (host != NULL)), "Invalid parameters");
77 simdata->waiting_task = NULL;
80 simdata->context = xbt_context_new(code, NULL, NULL,
81 MSG_process_cleanup, process,
82 simdata->argc, simdata->argv);
84 if((self=msg_global->current_process)) {
85 simdata->PPID = MSG_process_get_PID(self);
89 simdata->last_errno=MSG_OK;
92 /* Process structure */
93 process->name = xbt_strdup(name);
94 process->simdata = simdata;
97 xbt_fifo_push(host->simdata->process_list, process);
99 /* /////////////// FIX du current_process !!! ////////////// */
100 self = msg_global->current_process;
101 xbt_context_start(process->simdata->context);
102 msg_global->current_process = self;
104 xbt_fifo_push(msg_global->process_list, process);
105 xbt_fifo_push(msg_global->process_to_run, process);
110 /** \ingroup m_process_management
111 * \param process poor victim
113 * This function simply kills a \a process... scarry isn't it ? :)
115 void MSG_process_kill(m_process_t process)
117 xbt_fifo_remove(msg_global->process_list,process);
118 xbt_context_free(process->simdata->context);
119 MSG_process_cleanup(process);
122 /** \ingroup m_process_management
123 * \brief Migrates an agent to another location.
125 * This functions checks whether \a process and \a host are valid pointers
126 and change the value of the #m_host_t on which \a process is running.
128 MSG_error_t MSG_process_change_host(m_process_t process, m_host_t host)
130 simdata_process_t simdata = NULL;
134 xbt_assert0(((process) && (process->simdata)
135 && (host)), "Invalid parameters");
136 simdata = process->simdata;
138 xbt_fifo_remove(simdata->host->simdata->process_list,process);
139 simdata->host = host;
140 xbt_fifo_push(host->simdata->process_list,process);
145 /** \ingroup m_process_management
146 * \brief Return the user data of a #m_process_t.
148 * This functions checks whether \a process is a valid pointer or not
149 and return the user data associated to \a process if it is possible.
151 void *MSG_process_get_data(m_process_t process)
153 xbt_assert0((process != NULL), "Invalid parameters");
155 return (process->data);
158 /** \ingroup m_process_management
159 * \brief Set the user data of a #m_process_t.
161 * This functions checks whether \a process is a valid pointer or not
162 and set the user data associated to \a process if it is possible.
164 MSG_error_t MSG_process_set_data(m_process_t process,void *data)
166 xbt_assert0((process != NULL), "Invalid parameters");
167 xbt_assert0((process->data == NULL), "Data already set");
169 process->data = data;
174 /** \ingroup m_process_management
175 * \brief Return the location on which an agent is running.
177 * This functions checks whether \a process is a valid pointer or not
178 and return the m_host_t corresponding to the location on which \a
181 m_host_t MSG_process_get_host(m_process_t process)
183 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
185 return (((simdata_process_t) process->simdata)->host);
188 /** \ingroup m_process_management
190 * \brief Return a #m_process_t given its PID.
192 * This functions search in the list of all the created m_process_t for a m_process_t
193 whose PID is equal to \a PID. If no host is found, \c NULL is returned.
194 Note that the PID are uniq in the whole simulation, not only on a given host.
196 m_process_t MSG_process_from_PID(int PID)
198 xbt_fifo_item_t i = NULL;
199 m_process_t process = NULL;
201 xbt_fifo_foreach(msg_global->process_list,i,process,m_process_t) {
202 if(MSG_process_get_PID(process) == PID) return process;
207 /** \ingroup m_process_management
208 * \brief Returns the process ID of \a process.
210 * This functions checks whether \a process is a valid pointer or not
213 int MSG_process_get_PID(m_process_t process)
215 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
217 return (((simdata_process_t) process->simdata)->PID);
220 /** \ingroup m_process_management
221 * \brief Returns the process ID of the parent of \a process.
223 * This functions checks whether \a process is a valid pointer or not
224 and return its PID. Returns -1 if the agent has not been created by
227 int MSG_process_get_PPID(m_process_t process)
229 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
231 return (((simdata_process_t) process->simdata)->PPID);
234 /** \ingroup m_process_management
235 * \brief Return the name of an agent.
237 * This functions checks whether \a process is a valid pointer or not
240 const char *MSG_process_get_name(m_process_t process)
242 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
244 return (process->name);
247 /** \ingroup m_process_management
248 * \brief Return the PID of the current agent.
250 * This functions returns the PID of the currently running #m_process_t.
252 int MSG_process_self_PID(void)
254 return (MSG_process_get_PID(MSG_process_self()));
257 /** \ingroup m_process_management
258 * \brief Return the PPID of the current agent.
260 * This functions returns the PID of the parent of the currently
261 * running #m_process_t.
263 int MSG_process_self_PPID(void)
265 return (MSG_process_get_PPID(MSG_process_self()));
268 /** \ingroup m_process_management
269 * \brief Return the current agent.
271 * This functions returns the currently running #m_process_t.
273 m_process_t MSG_process_self(void)
275 return msg_global->current_process;
278 /** \ingroup m_process_management
279 * \brief Suspend the process.
281 * This functions suspend the process by suspending the task on which
282 * it was waiting for the completion.
284 MSG_error_t MSG_process_suspend(m_process_t process)
286 simdata_process_t simdata = NULL;
287 simdata_task_t simdata_task = NULL;
290 xbt_assert0(((process) && (process->simdata)), "Invalid parameters");
292 if(process!=MSG_process_self()) {
293 simdata = process->simdata;
295 xbt_assert0(simdata->waiting_task,"Process not waiting for anything else. Weird !");
297 simdata_task = simdata->waiting_task->simdata;
299 simdata->suspended = 1;
300 if(simdata->blocked) return MSG_OK;
302 xbt_assert0(((simdata_task->compute)||(simdata_task->comm))&&
303 !((simdata_task->compute)&&(simdata_task->comm)),
304 "Got a problem in deciding which action to choose !");
305 simdata->suspended = 1;
306 if(simdata_task->compute)
307 surf_workstation_resource->common_public->suspend(simdata_task->compute);
309 surf_workstation_resource->common_public->suspend(simdata_task->comm);
311 m_task_t dummy = MSG_TASK_UNINITIALIZED;
312 dummy = MSG_task_create("suspended", 0.0, 0, NULL);
314 simdata = process->simdata;
315 simdata->suspended = 1;
316 __MSG_task_execute(process,dummy);
317 surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
318 __MSG_wait_for_computation(process,dummy);
319 simdata->suspended = 0;
321 MSG_task_destroy(dummy);
326 /** \ingroup m_process_management
327 * \brief Resume a suspended process.
329 * This functions resume a suspended process by resuming the task on
330 * which it was waiting for the completion.
332 MSG_error_t MSG_process_resume(m_process_t process)
334 simdata_process_t simdata = NULL;
335 simdata_task_t simdata_task = NULL;
338 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
341 simdata = process->simdata;
343 if(simdata->blocked) {
344 simdata->suspended = 0; /* He'll wake up by itself */
348 if(!(simdata->waiting_task)) {
349 xbt_assert0(0,"Process not waiting for anything else. Weird !");
352 simdata_task = simdata->waiting_task->simdata;
355 if(simdata_task->compute)
356 surf_workstation_resource->common_public->resume(simdata_task->compute);
358 surf_workstation_resource->common_public->resume(simdata_task->comm);
363 /** \ingroup m_process_management
364 * \brief Returns true if the process is suspended .
366 * This checks whether a process is suspended or not by inspecting the
367 * task on which it was waiting for the completion.
369 int MSG_process_isSuspended(m_process_t process)
371 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
373 return (process->simdata->suspended);
376 MSG_error_t __MSG_process_block(void)
378 m_process_t process = MSG_process_self();
380 m_task_t dummy = MSG_TASK_UNINITIALIZED;
381 dummy = MSG_task_create("blocked", 0.0, 0, NULL);
383 process->simdata->blocked=1;
384 __MSG_task_execute(process,dummy);
385 surf_workstation_resource->common_public->suspend(dummy->simdata->compute);
386 __MSG_wait_for_computation(process,dummy);
387 process->simdata->blocked=0;
389 if(process->simdata->suspended)
390 MSG_process_suspend(process);
392 MSG_task_destroy(dummy);
397 MSG_error_t __MSG_process_unblock(m_process_t process)
399 simdata_process_t simdata = NULL;
400 simdata_task_t simdata_task = NULL;
403 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
406 simdata = process->simdata;
407 if(!(simdata->waiting_task)) {
408 xbt_assert0(0,"Process not waiting for anything else. Weird !");
411 simdata_task = simdata->waiting_task->simdata;
413 xbt_assert0(simdata->blocked,"Process not blocked");
415 surf_workstation_resource->common_public->resume(simdata_task->compute);
420 int __MSG_process_isBlocked(m_process_t process)
422 xbt_assert0(((process != NULL) && (process->simdata)), "Invalid parameters");
424 return (process->simdata->blocked);