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_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_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 const char *get_resource_name(void *resource_id)
62 static int resource_used(void *resource_id)
67 static void action_change_state(surf_action_t action,
68 e_surf_action_state_t state)
74 static double share_resources(double now)
76 if (xbt_heap_size(timer_heap))
77 return (xbt_heap_maxkey(timer_heap));
82 static void update_actions_state(double now, double delta)
84 if (xbt_heap_size(timer_heap)) {
85 if (xbt_heap_maxkey(timer_heap) <= now + delta) {
86 xbt_heap_pop(timer_heap);
92 static void update_resource_state(void *id,
93 tmgr_trace_event_t event_type,
94 double value, double date)
96 command_t command = id;
98 /* Move this command to the list of commands to execute */
99 xbt_swag_remove(command, command_pending);
100 xbt_swag_insert(command, command_to_run);
105 static void set(double date, void *function, void *arg)
107 command_t command = NULL;
109 command = command_new(function, arg);
111 tmgr_history_add_trace(history, empty_trace, date, 0, command);
112 xbt_heap_push(timer_heap, NULL, date);
116 static int get(void **function, void **arg)
118 command_t command = NULL;
120 command = xbt_swag_extract(command_to_run);
122 *function = command->function;
123 *arg = command->args;
130 static void action_suspend(surf_action_t action)
135 static void action_resume(surf_action_t action)
140 static int action_is_suspended(surf_action_t action)
146 static void finalize(void)
148 xbt_heap_free(timer_heap);
151 tmgr_trace_free(empty_trace);
154 xbt_swag_free(command_pending);
155 xbt_swag_free(command_to_run);
157 surf_model_exit(surf_timer_model);
159 free(surf_timer_model);
160 surf_timer_model = NULL;
163 static void surf_timer_model_init_internal(void)
165 surf_timer_model = xbt_new0(s_surf_model_t, 1);
167 surf_model_init(surf_timer_model);
169 surf_timer_model->get_resource_name = get_resource_name;
170 surf_timer_model->action_get_state = surf_action_get_state;
171 surf_timer_model->action_change_state = action_change_state;
172 surf_timer_model->action_set_data = surf_action_set_data;
173 surf_timer_model->name = "TIMER";
175 surf_timer_model->model_private->resource_used = resource_used;
176 surf_timer_model->model_private->share_resources = share_resources;
177 surf_timer_model->model_private->update_actions_state =
178 update_actions_state;
179 surf_timer_model->model_private->update_resource_state =
180 update_resource_state;
181 surf_timer_model->model_private->finalize = finalize;
183 surf_timer_model->suspend = action_suspend;
184 surf_timer_model->resume = action_resume;
185 surf_timer_model->is_suspended = action_is_suspended;
187 surf_timer_model->extension.timer.set = set;
188 surf_timer_model->extension.timer.get = get;
192 command_pending = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
193 command_to_run = xbt_swag_new(xbt_swag_offset(var, command_set_hookup));
196 empty_trace = tmgr_empty_trace_new();
197 timer_heap = xbt_heap_new(8, NULL);
200 void surf_timer_model_init(const char *filename)
202 if (surf_timer_model)
204 surf_timer_model_init_internal();
205 xbt_dynar_push(model_list, &surf_timer_model);