Logo AND Algorithmique Numérique Distribuée

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