X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/b59658a063489a0f89b971818530597ce329a3ce..f4d1afaaa1e4fee55a98707443c05bdbc9abb42c:/src/surf/surf_timer.c diff --git a/src/surf/surf_timer.c b/src/surf/surf_timer.c index 32f0257cab..243162eb9a 100644 --- a/src/surf/surf_timer.c +++ b/src/surf/surf_timer.c @@ -5,47 +5,44 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#include "xbt/ex.h" #include "surf_timer_private.h" -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(timer, surf, - "Logging specific to the SURF timer module"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_timer, surf, + "Logging specific to SURF (timer)"); surf_timer_resource_t surf_timer_resource = NULL; static tmgr_trace_t empty_trace = NULL; -static xbt_dict_t command_set = NULL; +static xbt_swag_t command_pending = NULL; +static xbt_swag_t command_to_run = NULL; +static xbt_heap_t timer_heap = NULL; static void timer_free(void *timer) { free(timer); } -static command_t command_new(void *fun, void* args) +static command_t command_new(void *fun, void *args) { command_t command = xbt_new0(s_command_t, 1); command->resource = (surf_resource_t) surf_timer_resource; - command->fun = fun; + command->function = fun; command->args = args; -/* command->name = name; */ -/* command->power_scale = power_scale; */ -/* xbt_assert0(command->power_scale>0,"Power has to be >0"); */ -/* command->power_current = power_initial; */ -/* if (power_trace) */ -/* command->power_event = */ -/* tmgr_history_add_trace(history, power_trace, 0.0, 0, command); */ - -/* command->state_current = state_initial; */ -/* if (state_trace) */ -/* command->state_event = */ -/* tmgr_history_add_trace(history, state_trace, 0.0, 0, command); */ - -/* command->constraint = */ -/* lmm_constraint_new(maxmin_system, command, */ -/* command->power_current * command->power_scale); */ + xbt_swag_insert(command, command_pending); + return command; +} -/* xbt_dict_set(command_set, name, command, command_free); */ +static void command_free(command_t command) +{ + free(command); - return command; + if (xbt_swag_belongs(command, command_to_run)) { + xbt_swag_remove(command, command_to_run); + } else if (xbt_swag_belongs(command, command_pending)) { + xbt_swag_remove(command, command_pending); + } + return; } static void parse_timer(void) @@ -73,10 +70,10 @@ static int resource_used(void *resource_id) return 1; } -static void action_free(surf_action_t action) +static int action_free(surf_action_t action) { DIE_IMPOSSIBLE; - return; + return 1; } static void action_cancel(surf_action_t action) @@ -100,11 +97,19 @@ static void action_change_state(surf_action_t action, static double share_resources(double now) { - return -1.0; + if (xbt_heap_size(timer_heap)) + return (xbt_heap_maxkey(timer_heap)); + else + return -1.0; } static void update_actions_state(double now, double delta) { + if (xbt_heap_size(timer_heap)) { + if (xbt_heap_maxkey(timer_heap) <= now + delta) { + xbt_heap_pop(timer_heap); + } + } return; } @@ -114,7 +119,9 @@ static void update_resource_state(void *id, { command_t command = id; - // Move this command to the list of commands to execute + /* Move this command to the list of commands to execute */ + xbt_swag_remove(command, command_pending); + xbt_swag_insert(command, command_to_run); return; } @@ -126,13 +133,22 @@ static void set(double date, void *function, void *arg) command = command_new(function, arg); tmgr_history_add_trace(history, empty_trace, date, 0, command); - + xbt_heap_push(timer_heap, NULL, date); } static int get(void **function, void **arg) { - return 0; + command_t command = NULL; + + command = xbt_swag_extract(command_to_run); + if (command) { + *function = command->function; + *arg = command->args; + return 1; + } else { + return 0; + } } static void action_suspend(surf_action_t action) @@ -153,14 +169,23 @@ static int action_is_suspended(surf_action_t action) static void finalize(void) { - xbt_dict_free(&command_set); + xbt_heap_free(timer_heap); + timer_heap = NULL; + + tmgr_trace_free(empty_trace); + empty_trace = NULL; - xbt_swag_free(surf_timer_resource->common_public->states.ready_action_set); + xbt_swag_free(command_pending); + xbt_swag_free(command_to_run); + + xbt_swag_free(surf_timer_resource->common_public->states. + ready_action_set); xbt_swag_free(surf_timer_resource->common_public->states. running_action_set); xbt_swag_free(surf_timer_resource->common_public->states. failed_action_set); - xbt_swag_free(surf_timer_resource->common_public->states.done_action_set); + xbt_swag_free(surf_timer_resource->common_public->states. + done_action_set); free(surf_timer_resource->common_public); free(surf_timer_resource->common_private); free(surf_timer_resource->extension_public); @@ -177,7 +202,8 @@ static void surf_timer_resource_init_internal(void) surf_timer_resource->common_private = xbt_new0(s_surf_resource_private_t, 1); - surf_timer_resource->common_public = xbt_new0(s_surf_resource_public_t, 1); + surf_timer_resource->common_public = + xbt_new0(s_surf_resource_public_t, 1); surf_timer_resource->extension_public = xbt_new0(s_surf_timer_resource_extension_public_t, 1); @@ -192,7 +218,8 @@ static void surf_timer_resource_init_internal(void) xbt_swag_new(xbt_swag_offset(action, state_hookup)); surf_timer_resource->common_public->name_service = name_service; - surf_timer_resource->common_public->get_resource_name = get_resource_name; + surf_timer_resource->common_public->get_resource_name = + get_resource_name; surf_timer_resource->common_public->action_get_state = surf_action_get_state; surf_timer_resource->common_public->action_free = action_free; @@ -200,7 +227,8 @@ static void surf_timer_resource_init_internal(void) surf_timer_resource->common_public->action_recycle = action_recycle; surf_timer_resource->common_public->action_change_state = action_change_state; - surf_timer_resource->common_public->action_set_data = surf_action_set_data; + surf_timer_resource->common_public->action_set_data = + surf_action_set_data; surf_timer_resource->common_public->name = "TIMER"; surf_timer_resource->common_private->resource_used = resource_used; @@ -218,8 +246,16 @@ static void surf_timer_resource_init_internal(void) surf_timer_resource->extension_public->set = set; surf_timer_resource->extension_public->get = get; - command_set = xbt_dict_new(); + { + s_command_t var; + command_pending = + xbt_swag_new(xbt_swag_offset(var, command_set_hookup)); + command_to_run = + xbt_swag_new(xbt_swag_offset(var, command_set_hookup)); + } + empty_trace = tmgr_empty_trace_new(); + timer_heap = xbt_heap_new(8, NULL); xbt_assert0(maxmin_system, "surf_init has to be called first!"); }