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(surf_timer, surf,
12 "Logging specific to SURF (timer)");
14 surf_timer_model_t surf_timer_model = 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 static xbt_heap_t timer_heap = NULL;
20 static void timer_free(void *timer)
25 static command_t command_new(void *fun, void *args)
27 command_t command = xbt_new0(s_command_t, 1);
29 command->model = (surf_model_t) surf_timer_model;
30 command->function = fun;
32 xbt_swag_insert(command, command_pending);
36 static void command_free(command_t command)
40 if (xbt_swag_belongs(command, command_to_run)) {
41 xbt_swag_remove(command, command_to_run);
42 } else if (xbt_swag_belongs(command, command_pending)) {
43 xbt_swag_remove(command, command_pending);
48 static void parse_timer(void)
52 static void parse_file(const char *file)
56 static void *name_service(const char *name)
62 static const char *get_resource_name(void *resource_id)
68 static int resource_used(void *resource_id)
73 static int action_free(surf_action_t action)
79 static void action_cancel(surf_action_t action)
85 static void action_recycle(surf_action_t action)
91 static void action_change_state(surf_action_t action,
92 e_surf_action_state_t state)
98 static double share_resources(double now)
100 if (xbt_heap_size(timer_heap))
101 return (xbt_heap_maxkey(timer_heap));
106 static void update_actions_state(double now, double delta)
108 if (xbt_heap_size(timer_heap)) {
109 if (xbt_heap_maxkey(timer_heap) <= now + delta) {
110 xbt_heap_pop(timer_heap);
116 static void update_resource_state(void *id,
117 tmgr_trace_event_t event_type,
118 double value, double date)
120 command_t command = id;
122 /* Move this command to the list of commands to execute */
123 xbt_swag_remove(command, command_pending);
124 xbt_swag_insert(command, command_to_run);
129 static void set(double date, void *function, void *arg)
131 command_t command = NULL;
133 command = command_new(function, arg);
135 tmgr_history_add_trace(history, empty_trace, date, 0, command);
136 xbt_heap_push(timer_heap, NULL, date);
140 static int get(void **function, void **arg)
142 command_t command = NULL;
144 command = xbt_swag_extract(command_to_run);
146 *function = command->function;
147 *arg = command->args;
154 static void action_suspend(surf_action_t action)
159 static void action_resume(surf_action_t action)
164 static int action_is_suspended(surf_action_t action)
170 static void finalize(void)
172 xbt_heap_free(timer_heap);
175 tmgr_trace_free(empty_trace);
178 xbt_swag_free(command_pending);
179 xbt_swag_free(command_to_run);
181 xbt_swag_free(surf_timer_model->common_public->states.
183 xbt_swag_free(surf_timer_model->common_public->states.
185 xbt_swag_free(surf_timer_model->common_public->states.
187 xbt_swag_free(surf_timer_model->common_public->states.
189 free(surf_timer_model->common_public);
190 free(surf_timer_model->common_private);
191 free(surf_timer_model->extension_public);
193 free(surf_timer_model);
194 surf_timer_model = NULL;
197 static void surf_timer_model_init_internal(void)
199 s_surf_action_t action;
201 surf_timer_model = xbt_new0(s_surf_timer_model_t, 1);
203 surf_timer_model->common_private =
204 xbt_new0(s_surf_model_private_t, 1);
205 surf_timer_model->common_public =
206 xbt_new0(s_surf_model_public_t, 1);
208 surf_timer_model->extension_public =
209 xbt_new0(s_surf_timer_model_extension_public_t, 1);
211 surf_timer_model->common_public->states.ready_action_set =
212 xbt_swag_new(xbt_swag_offset(action, state_hookup));
213 surf_timer_model->common_public->states.running_action_set =
214 xbt_swag_new(xbt_swag_offset(action, state_hookup));
215 surf_timer_model->common_public->states.failed_action_set =
216 xbt_swag_new(xbt_swag_offset(action, state_hookup));
217 surf_timer_model->common_public->states.done_action_set =
218 xbt_swag_new(xbt_swag_offset(action, state_hookup));
220 surf_timer_model->common_public->name_service = name_service;
221 surf_timer_model->common_public->get_resource_name =
223 surf_timer_model->common_public->action_get_state =
224 surf_action_get_state;
225 surf_timer_model->common_public->action_free = action_free;
226 surf_timer_model->common_public->action_cancel = action_cancel;
227 surf_timer_model->common_public->action_recycle = action_recycle;
228 surf_timer_model->common_public->action_change_state =
230 surf_timer_model->common_public->action_set_data =
231 surf_action_set_data;
232 surf_timer_model->common_public->name = "TIMER";
234 surf_timer_model->common_private->resource_used = resource_used;
235 surf_timer_model->common_private->share_resources = share_resources;
236 surf_timer_model->common_private->update_actions_state =
237 update_actions_state;
238 surf_timer_model->common_private->update_resource_state =
239 update_resource_state;
240 surf_timer_model->common_private->finalize = finalize;
242 surf_timer_model->common_public->suspend = action_suspend;
243 surf_timer_model->common_public->resume = action_resume;
244 surf_timer_model->common_public->is_suspended = action_is_suspended;
246 surf_timer_model->extension_public->set = set;
247 surf_timer_model->extension_public->get = get;
252 xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
254 xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
257 empty_trace = tmgr_empty_trace_new();
258 timer_heap = xbt_heap_new(8, NULL);
261 void surf_timer_model_init(const char *filename)
263 if (surf_timer_model)
265 surf_timer_model_init_internal();
266 xbt_dynar_push(model_list, &surf_timer_model);