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. */
8 #include "surf_timer_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(timer, surf,
11 "Logging specific to the SURF timer module");
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;
18 static void timer_free(void *timer)
23 static command_t command_new(void *fun, void* args)
25 command_t command = xbt_new0(s_command_t, 1);
27 command->resource = (surf_resource_t) surf_timer_resource;
28 command->function = fun;
30 xbt_swag_insert(command,command_pending);
34 static void command_free(command_t command)
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);
46 static void parse_timer(void)
50 static void parse_file(const char *file)
54 static void *name_service(const char *name)
60 static const char *get_resource_name(void *resource_id)
66 static int resource_used(void *resource_id)
71 static int action_free(surf_action_t action)
77 static void action_cancel(surf_action_t action)
83 static void action_recycle(surf_action_t action)
89 static void action_change_state(surf_action_t action,
90 e_surf_action_state_t state)
96 static double share_resources(double now)
101 static void update_actions_state(double now, double delta)
106 static void update_resource_state(void *id,
107 tmgr_trace_event_t event_type,
110 command_t command = id;
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);
119 static void set(double date, void *function, void *arg)
121 command_t command = NULL;
123 command = command_new(function, arg);
125 tmgr_history_add_trace(history, empty_trace, date, 0, command);
129 static int get(void **function, void **arg)
131 command_t command = NULL;
133 command = xbt_swag_extract(command_to_run);
135 *function = command->function;
136 *arg = command->args;
143 static void action_suspend(surf_action_t action)
148 static void action_resume(surf_action_t action)
153 static int action_is_suspended(surf_action_t action)
159 static void finalize(void)
161 xbt_swag_free(command_pending);
162 xbt_swag_free(command_to_run);
164 xbt_swag_free(surf_timer_resource->common_public->states.ready_action_set);
165 xbt_swag_free(surf_timer_resource->common_public->states.
167 xbt_swag_free(surf_timer_resource->common_public->states.
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);
174 free(surf_timer_resource);
175 surf_timer_resource = NULL;
178 static void surf_timer_resource_init_internal(void)
180 s_surf_action_t action;
182 surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
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);
188 surf_timer_resource->extension_public =
189 xbt_new0(s_surf_timer_resource_extension_public_t, 1);
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));
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 =
209 surf_timer_resource->common_public->action_set_data = surf_action_set_data;
210 surf_timer_resource->common_public->name = "TIMER";
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;
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;
224 surf_timer_resource->extension_public->set = set;
225 surf_timer_resource->extension_public->get = get;
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));
233 empty_trace = tmgr_empty_trace_new();
235 xbt_assert0(maxmin_system, "surf_init has to be called first!");
238 void surf_timer_resource_init(const char *filename)
240 if (surf_timer_resource)
242 surf_timer_resource_init_internal();
243 xbt_dynar_push(resource_list, &surf_timer_resource);