Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moving public parts to public places and generating things to the right places
[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   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 }