Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af973f05ec989379bca47ff494827225d2837fe3
[simgrid.git] / src / msg / task.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
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. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(task, msg,
12                                 "Logging specific to MSG (task)");
13
14 static char sprint_buffer[64];
15
16 /********************************* Task **************************************/
17 /** \ingroup m_task_management
18  * \brief Creates a new #m_task_t.
19  *
20  * A constructor for #m_task_t taking four arguments and returning the 
21    corresponding object.
22  * \param name a name for the object. It is for user-level information
23    and can be NULL.
24  * \param compute_duration a value of the processing amount (in Mflop)
25    needed to process this new task. If 0, then it cannot be executed with
26    MSG_task_execute(). This value has to be >=0.
27  * \param message_size a value of the amount of data (in Mb) needed to
28    transfer this new task. If 0, then it cannot be transfered with
29    MSG_task_get() and MSG_task_put(). This value has to be >=0.
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_task_get_data.
33  * \see m_task_t
34  * \return The new corresponding object.
35  */
36 m_task_t MSG_task_create(const char *name, long double compute_duration,
37                          long double message_size, void *data)
38 {
39   simdata_task_t simdata = xbt_new0(s_simdata_task_t,1);
40   m_task_t task = xbt_new0(s_m_task_t,1);
41   
42   /* Task structure */
43   task->name = xbt_strdup(name);
44   task->simdata = simdata;
45   task->data = data;
46
47   /* Simulator Data */
48   simdata->sleeping = xbt_dynar_new(sizeof(m_process_t),NULL);
49   simdata->computation_amount = compute_duration;
50   simdata->message_size = message_size;
51   simdata->using = 1;
52   simdata->sender = NULL;
53
54   return task;
55 }
56
57 /** \ingroup m_task_management
58  * \brief Return the user data of a #m_task_t.
59  *
60  * This functions checks whether \a task is a valid pointer or not and return
61    the user data associated to \a task if it is possible.
62  */
63 void *MSG_task_get_data(m_task_t task)
64 {
65   xbt_assert0((task != NULL), "Invalid parameter");
66
67   return (task->data);
68 }
69
70 /** \ingroup m_task_management
71  * \brief Return the sender of a #m_task_t.
72  *
73  * This functions returns the #m_process_t which sent this task
74  */
75 m_process_t MSG_task_get_sender(m_task_t task)
76 {
77    xbt_assert0(task, "Invalid parameters");
78    return ((simdata_task_t) task->simdata)->sender;
79 }
80
81
82 /** \ingroup m_task_management
83  * \brief Destroy a #m_task_t.
84  *
85  * Destructor for #m_task_t. Note that you should free user data, if any, \b 
86    before calling this function.
87  */
88 MSG_error_t MSG_task_destroy(m_task_t task)
89 {
90   simdata_task_t simdata = NULL;
91   surf_action_t action = NULL;
92   int i;
93
94   xbt_assert0((task != NULL), "Invalid parameter");
95
96   task->simdata->using--;
97   if(task->simdata->using>0) return MSG_OK;
98
99   xbt_assert0((xbt_dynar_length(task->simdata->sleeping)==0), 
100               "Task still used. There is a problem. Cannot destroy it now!");
101
102   if(task->name) xbt_free(task->name);
103
104   xbt_dynar_free(&(task->simdata->sleeping));
105
106   action = task->simdata->compute;
107   if(action) action->resource_type->common_public->action_free(action);
108   action = task->simdata->comm;
109   if(action) action->resource_type->common_public->action_free(action);
110
111
112   xbt_free(task->simdata);
113   xbt_free(task);
114
115   return MSG_OK;
116 }
117
118 /* static MSG_error_t __MSG_task_check(m_task_t task) */
119 /* { */
120 /*   simdata_task_t simdata = NULL; */
121 /*   int warning = 0; */
122
123 /*   if (task == NULL) {                /\* Fatal *\/ */
124 /*     WARNING("Task uninitialized"); */
125 /*     return MSG_FATAL; */
126 /*   } */
127 /*   simdata = task->simdata; */
128
129 /*   if (simdata == NULL) {     /\* Fatal *\/ */
130 /*     WARNING("Simulator Data uninitialized"); */
131 /*     return MSG_FATAL; */
132 /*   } */
133
134 /*   if (simdata->compute == NULL) {    /\* Fatal if execute ... *\/ */
135 /*     WARNING("No duration set for this task"); */
136 /*     warning++; */
137 /*   } */
138
139 /*   if (simdata->message_size == 0) {  /\* Fatal if transfered ... *\/ */
140 /*     WARNING("No message_size set for this task"); */
141 /*     warning++; */
142 /*   } */
143
144 /* /\*    if (task->data == NULL) { *\/ */
145 /* /\*      WARNING("User Data uninitialized"); *\/ */
146 /* /\*      warning++; *\/ */
147 /* /\*    } *\/ */
148
149 /*   if (warning) */
150 /*     return MSG_WARNING; */
151 /*   return MSG_OK; */
152 /* } */
153
154 /* static m_task_t __MSG_task_copy(m_task_t src) */
155 /* { */
156 /*   m_task_t copy = NULL; */
157 /*   simdata_task_t simdata = NULL; */
158
159 /*   __MSG_task_check(src); */
160
161 /*   simdata = src->simdata; */
162 /*   copy = MSG_task_create(src->name, SG_getTaskCost(simdata->compute), */
163 /*                       simdata->message_size, MSG_task_get_data(src)); */
164
165 /*   return (copy); */
166 /* } */
167
168 MSG_error_t __MSG_task_wait_event(m_process_t process, m_task_t task)
169 {
170   int _cursor;
171   m_process_t proc = NULL;
172
173   xbt_assert0(((task != NULL)
174                && (task->simdata != NULL)), "Invalid parameters");
175
176   xbt_dynar_push(task->simdata->sleeping, &process);
177   process->simdata->waiting_task = task;
178   xbt_context_yield();
179   process->simdata->waiting_task = NULL;
180   xbt_dynar_foreach(task->simdata->sleeping,_cursor,proc) {
181     if(proc==process) 
182       xbt_dynar_remove_at(task->simdata->sleeping,_cursor,&proc);
183   }
184
185   return MSG_OK;
186 }