Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
678af824d9b05bf2e7073e43c01ea562e4effed9
[simgrid.git] / src / surf / surf_timer.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 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 "xbt/ex.h"
9 #include "surf_timer_private.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(timer, surf,
12                                 "Logging specific to the SURF timer module");
13
14 surf_timer_resource_t surf_timer_resource = NULL;
15 static tmgr_trace_t empty_trace = NULL;
16 static xbt_swag_t command_pending = NULL;
17 static xbt_swag_t command_to_run = NULL;
18
19 static void timer_free(void *timer)
20 {
21   free(timer);
22 }
23
24 static command_t command_new(void *fun, void* args)
25 {
26   command_t command = xbt_new0(s_command_t, 1);
27
28   command->resource = (surf_resource_t) surf_timer_resource;
29   command->function = fun;
30   command->args = args;
31   xbt_swag_insert(command,command_pending);
32   return command;
33 }
34
35 static void command_free(command_t command)
36 {
37   free(command);
38
39   if(xbt_swag_belongs(command,command_to_run)) {
40     xbt_swag_remove(command,command_to_run);
41   } else if (xbt_swag_belongs(command,command_pending)) {
42     xbt_swag_remove(command,command_pending);
43   }
44   return;
45 }
46
47 static void parse_timer(void)
48 {
49 }
50
51 static void parse_file(const char *file)
52 {
53 }
54
55 static void *name_service(const char *name)
56 {
57   DIE_IMPOSSIBLE;
58   return NULL;
59 }
60
61 static const char *get_resource_name(void *resource_id)
62 {
63   DIE_IMPOSSIBLE;
64   return "";
65 }
66
67 static int resource_used(void *resource_id)
68 {
69   return 1;
70 }
71
72 static int action_free(surf_action_t action)
73 {
74   DIE_IMPOSSIBLE;
75   return 1;
76 }
77
78 static void action_cancel(surf_action_t action)
79 {
80   DIE_IMPOSSIBLE;
81   return;
82 }
83
84 static void action_recycle(surf_action_t action)
85 {
86   DIE_IMPOSSIBLE;
87   return;
88 }
89
90 static void action_change_state(surf_action_t action,
91                                 e_surf_action_state_t state)
92 {
93   DIE_IMPOSSIBLE;
94   return;
95 }
96
97 static double share_resources(double now)
98 {
99   return -1.0;
100 }
101
102 static void update_actions_state(double now, double delta)
103 {
104   return;
105 }
106
107 static void update_resource_state(void *id,
108                                   tmgr_trace_event_t event_type,
109                                   double value)
110 {
111   command_t command = id;
112
113   /* Move this command to the list of commands to execute */
114   xbt_swag_remove(command,command_pending);
115   xbt_swag_insert(command,command_to_run);
116
117   return;
118 }
119
120 static void set(double date, void *function, void *arg)
121 {
122   command_t command = NULL;
123
124   command = command_new(function, arg);
125
126   tmgr_history_add_trace(history, empty_trace, date, 0, command);  
127 }
128
129
130 static int get(void **function, void **arg)
131 {
132   command_t command = NULL;
133
134   command = xbt_swag_extract(command_to_run);
135   if(command) {
136     *function = command->function;
137     *arg = command->args;
138     return 1;
139   } else {
140     return 0;
141   }
142 }
143
144 static void action_suspend(surf_action_t action)
145 {
146   DIE_IMPOSSIBLE;
147 }
148
149 static void action_resume(surf_action_t action)
150 {
151   DIE_IMPOSSIBLE;
152 }
153
154 static int action_is_suspended(surf_action_t action)
155 {
156   DIE_IMPOSSIBLE;
157   return 0;
158 }
159
160 static void finalize(void)
161 {
162   tmgr_trace_free(empty_trace);
163   empty_trace = NULL;
164
165   xbt_swag_free(command_pending);
166   xbt_swag_free(command_to_run);
167
168   xbt_swag_free(surf_timer_resource->common_public->states.ready_action_set);
169   xbt_swag_free(surf_timer_resource->common_public->states.
170                 running_action_set);
171   xbt_swag_free(surf_timer_resource->common_public->states.
172                 failed_action_set);
173   xbt_swag_free(surf_timer_resource->common_public->states.done_action_set);
174   free(surf_timer_resource->common_public);
175   free(surf_timer_resource->common_private);
176   free(surf_timer_resource->extension_public);
177
178   free(surf_timer_resource);
179   surf_timer_resource = NULL;
180 }
181
182 static void surf_timer_resource_init_internal(void)
183 {
184   s_surf_action_t action;
185
186   surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
187
188   surf_timer_resource->common_private =
189       xbt_new0(s_surf_resource_private_t, 1);
190   surf_timer_resource->common_public = xbt_new0(s_surf_resource_public_t, 1);
191
192   surf_timer_resource->extension_public =
193       xbt_new0(s_surf_timer_resource_extension_public_t, 1);
194
195   surf_timer_resource->common_public->states.ready_action_set =
196       xbt_swag_new(xbt_swag_offset(action, state_hookup));
197   surf_timer_resource->common_public->states.running_action_set =
198       xbt_swag_new(xbt_swag_offset(action, state_hookup));
199   surf_timer_resource->common_public->states.failed_action_set =
200       xbt_swag_new(xbt_swag_offset(action, state_hookup));
201   surf_timer_resource->common_public->states.done_action_set =
202       xbt_swag_new(xbt_swag_offset(action, state_hookup));
203
204   surf_timer_resource->common_public->name_service = name_service;
205   surf_timer_resource->common_public->get_resource_name = get_resource_name;
206   surf_timer_resource->common_public->action_get_state =
207       surf_action_get_state;
208   surf_timer_resource->common_public->action_free = action_free;
209   surf_timer_resource->common_public->action_cancel = action_cancel;
210   surf_timer_resource->common_public->action_recycle = action_recycle;
211   surf_timer_resource->common_public->action_change_state =
212       action_change_state;
213   surf_timer_resource->common_public->action_set_data = surf_action_set_data;
214   surf_timer_resource->common_public->name = "TIMER";
215
216   surf_timer_resource->common_private->resource_used = resource_used;
217   surf_timer_resource->common_private->share_resources = share_resources;
218   surf_timer_resource->common_private->update_actions_state =
219       update_actions_state;
220   surf_timer_resource->common_private->update_resource_state =
221       update_resource_state;
222   surf_timer_resource->common_private->finalize = finalize;
223
224   surf_timer_resource->common_public->suspend = action_suspend;
225   surf_timer_resource->common_public->resume = action_resume;
226   surf_timer_resource->common_public->is_suspended = action_is_suspended;
227
228   surf_timer_resource->extension_public->set = set;
229   surf_timer_resource->extension_public->get = get;
230
231   {
232     s_command_t var;
233     command_pending = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
234     command_to_run  = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
235   }
236
237   empty_trace = tmgr_empty_trace_new();
238
239   xbt_assert0(maxmin_system, "surf_init has to be called first!");
240 }
241
242 void surf_timer_resource_init(const char *filename)
243 {
244   if (surf_timer_resource)
245     return;
246   surf_timer_resource_init_internal();
247   xbt_dynar_push(resource_list, &surf_timer_resource);
248 }