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 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;
16 static void timer_free(void *timer)
21 static command_t command_new(void *fun, void* args)
23 command_t command = xbt_new0(s_command_t, 1);
25 command->resource = (surf_resource_t) surf_timer_resource;
26 command->function = fun;
28 xbt_swag_insert(command,command_pending);
32 static void command_free(command_t command)
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);
44 static void parse_timer(void)
48 static void parse_file(const char *file)
52 static void *name_service(const char *name)
58 static const char *get_resource_name(void *resource_id)
64 static int resource_used(void *resource_id)
69 static int action_free(surf_action_t action)
75 static void action_cancel(surf_action_t action)
81 static void action_recycle(surf_action_t action)
87 static void action_change_state(surf_action_t action,
88 e_surf_action_state_t state)
94 static double share_resources(double now)
99 static void update_actions_state(double now, double delta)
104 static void update_resource_state(void *id,
105 tmgr_trace_event_t event_type,
108 command_t command = id;
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);
117 static void set(double date, void *function, void *arg)
119 command_t command = NULL;
121 command = command_new(function, arg);
123 tmgr_history_add_trace(history, empty_trace, date, 0, command);
127 static int get(void **function, void **arg)
129 command_t command = NULL;
131 command = xbt_swag_extract(command_to_run);
133 *function = command->function;
134 *arg = command->args;
141 static void action_suspend(surf_action_t action)
146 static void action_resume(surf_action_t action)
151 static int action_is_suspended(surf_action_t action)
157 static void finalize(void)
159 tmgr_trace_free(empty_trace);
162 xbt_swag_free(command_pending);
163 xbt_swag_free(command_to_run);
165 xbt_swag_free(surf_timer_resource->common_public->states.ready_action_set);
166 xbt_swag_free(surf_timer_resource->common_public->states.
168 xbt_swag_free(surf_timer_resource->common_public->states.
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);
175 free(surf_timer_resource);
176 surf_timer_resource = NULL;
179 static void surf_timer_resource_init_internal(void)
181 s_surf_action_t action;
183 surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
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);
189 surf_timer_resource->extension_public =
190 xbt_new0(s_surf_timer_resource_extension_public_t, 1);
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));
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 =
210 surf_timer_resource->common_public->action_set_data = surf_action_set_data;
211 surf_timer_resource->common_public->name = "TIMER";
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;
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;
225 surf_timer_resource->extension_public->set = set;
226 surf_timer_resource->extension_public->get = get;
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));
234 empty_trace = tmgr_empty_trace_new();
236 xbt_assert0(maxmin_system, "surf_init has to be called first!");
239 void surf_timer_resource_init(const char *filename)
241 if (surf_timer_resource)
243 surf_timer_resource_init_internal();
244 xbt_dynar_push(resource_list, &surf_timer_resource);