Logo AND Algorithmique Numérique Distribuée

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