3 /* Copyright (c) 2005 Arnaud Legrand. All rights reserved. */
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. */
9 #include "surf_timer_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(timer, surf,
12 "Logging specific to the SURF timer module");
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;
19 static void timer_free(void *timer)
24 static command_t command_new(void *fun, void* args)
26 command_t command = xbt_new0(s_command_t, 1);
28 command->resource = (surf_resource_t) surf_timer_resource;
29 command->function = fun;
31 xbt_swag_insert(command,command_pending);
35 static void command_free(command_t command)
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);
47 static void parse_timer(void)
51 static void parse_file(const char *file)
55 static void *name_service(const char *name)
61 static const char *get_resource_name(void *resource_id)
67 static int resource_used(void *resource_id)
72 static int action_free(surf_action_t action)
78 static void action_cancel(surf_action_t action)
84 static void action_recycle(surf_action_t action)
90 static void action_change_state(surf_action_t action,
91 e_surf_action_state_t state)
97 static double share_resources(double now)
102 static void update_actions_state(double now, double delta)
107 static void update_resource_state(void *id,
108 tmgr_trace_event_t event_type,
111 command_t command = id;
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);
120 static void set(double date, void *function, void *arg)
122 command_t command = NULL;
124 command = command_new(function, arg);
126 tmgr_history_add_trace(history, empty_trace, date, 0, command);
130 static int get(void **function, void **arg)
132 command_t command = NULL;
134 command = xbt_swag_extract(command_to_run);
136 *function = command->function;
137 *arg = command->args;
144 static void action_suspend(surf_action_t action)
149 static void action_resume(surf_action_t action)
154 static int action_is_suspended(surf_action_t action)
160 static void finalize(void)
162 tmgr_trace_free(empty_trace);
165 xbt_swag_free(command_pending);
166 xbt_swag_free(command_to_run);
168 xbt_swag_free(surf_timer_resource->common_public->states.ready_action_set);
169 xbt_swag_free(surf_timer_resource->common_public->states.
171 xbt_swag_free(surf_timer_resource->common_public->states.
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);
178 free(surf_timer_resource);
179 surf_timer_resource = NULL;
182 static void surf_timer_resource_init_internal(void)
184 s_surf_action_t action;
186 surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
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);
192 surf_timer_resource->extension_public =
193 xbt_new0(s_surf_timer_resource_extension_public_t, 1);
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));
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 =
213 surf_timer_resource->common_public->action_set_data = surf_action_set_data;
214 surf_timer_resource->common_public->name = "TIMER";
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;
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;
228 surf_timer_resource->extension_public->set = set;
229 surf_timer_resource->extension_public->get = get;
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));
237 empty_trace = tmgr_empty_trace_new();
239 xbt_assert0(maxmin_system, "surf_init has to be called first!");
242 void surf_timer_resource_init(const char *filename)
244 if (surf_timer_resource)
246 surf_timer_resource_init_internal();
247 xbt_dynar_push(resource_list, &surf_timer_resource);