Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove useless dependency
[simgrid.git] / src / surf / surf_timer.c
index 9f4d72d..3a7463b 100644 (file)
@@ -8,24 +8,28 @@
 #include "xbt/ex.h"
 #include "surf_timer_private.h"
 
-surf_timer_resource_t surf_timer_resource = NULL;
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_timer, surf,
+                               "Logging specific to SURF (timer)");
+
+surf_timer_model_t surf_timer_model = NULL;
 static tmgr_trace_t empty_trace = 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, voidargs)
+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->model = (surf_model_t) surf_timer_model;
   command->function = fun;
   command->args = args;
-  xbt_swag_insert(command,command_pending);
+  xbt_swag_insert(command, command_pending);
   return command;
 }
 
@@ -33,10 +37,10 @@ static void command_free(command_t command)
 {
   free(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);
+  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;
 }
@@ -93,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;
 }
 
@@ -108,8 +120,8 @@ static void update_resource_state(void *id,
   command_t command = id;
 
   /* Move this command to the list of commands to execute */
-  xbt_swag_remove(command,command_pending);
-  xbt_swag_insert(command,command_to_run);
+  xbt_swag_remove(command, command_pending);
+  xbt_swag_insert(command, command_to_run);
 
   return;
 }
@@ -120,7 +132,8 @@ static void set(double date, void *function, void *arg)
 
   command = command_new(function, arg);
 
-  tmgr_history_add_trace(history, empty_trace, date, 0, command);  
+  tmgr_history_add_trace(history, empty_trace, date, 0, command);
+  xbt_heap_push(timer_heap, NULL, date);
 }
 
 
@@ -129,7 +142,7 @@ static int get(void **function, void **arg)
   command_t command = NULL;
 
   command = xbt_swag_extract(command_to_run);
-  if(command) {
+  if (command) {
     *function = command->function;
     *arg = command->args;
     return 1;
@@ -156,90 +169,99 @@ static int action_is_suspended(surf_action_t action)
 
 static void finalize(void)
 {
+  xbt_heap_free(timer_heap);
+  timer_heap = NULL;
+
   tmgr_trace_free(empty_trace);
   empty_trace = NULL;
 
   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.
+  xbt_swag_free(surf_timer_model->common_public->states.
+               ready_action_set);
+  xbt_swag_free(surf_timer_model->common_public->states.
                running_action_set);
-  xbt_swag_free(surf_timer_resource->common_public->states.
+  xbt_swag_free(surf_timer_model->common_public->states.
                failed_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);
-
-  free(surf_timer_resource);
-  surf_timer_resource = NULL;
+  xbt_swag_free(surf_timer_model->common_public->states.
+               done_action_set);
+  free(surf_timer_model->common_public);
+  free(surf_timer_model->common_private);
+  free(surf_timer_model->extension_public);
+
+  free(surf_timer_model);
+  surf_timer_model = NULL;
 }
 
-static void surf_timer_resource_init_internal(void)
+static void surf_timer_model_init_internal(void)
 {
   s_surf_action_t action;
 
-  surf_timer_resource = xbt_new0(s_surf_timer_resource_t, 1);
+  surf_timer_model = xbt_new0(s_surf_timer_model_t, 1);
 
-  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_model->common_private =
+      xbt_new0(s_surf_model_private_t, 1);
+  surf_timer_model->common_public =
+      xbt_new0(s_surf_model_public_t, 1);
 
-  surf_timer_resource->extension_public =
-      xbt_new0(s_surf_timer_resource_extension_public_t, 1);
+  surf_timer_model->extension_public =
+      xbt_new0(s_surf_timer_model_extension_public_t, 1);
 
-  surf_timer_resource->common_public->states.ready_action_set =
+  surf_timer_model->common_public->states.ready_action_set =
       xbt_swag_new(xbt_swag_offset(action, state_hookup));
-  surf_timer_resource->common_public->states.running_action_set =
+  surf_timer_model->common_public->states.running_action_set =
       xbt_swag_new(xbt_swag_offset(action, state_hookup));
-  surf_timer_resource->common_public->states.failed_action_set =
+  surf_timer_model->common_public->states.failed_action_set =
       xbt_swag_new(xbt_swag_offset(action, state_hookup));
-  surf_timer_resource->common_public->states.done_action_set =
+  surf_timer_model->common_public->states.done_action_set =
       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->action_get_state =
+  surf_timer_model->common_public->name_service = name_service;
+  surf_timer_model->common_public->get_resource_name =
+      get_resource_name;
+  surf_timer_model->common_public->action_get_state =
       surf_action_get_state;
-  surf_timer_resource->common_public->action_free = action_free;
-  surf_timer_resource->common_public->action_cancel = action_cancel;
-  surf_timer_resource->common_public->action_recycle = action_recycle;
-  surf_timer_resource->common_public->action_change_state =
+  surf_timer_model->common_public->action_free = action_free;
+  surf_timer_model->common_public->action_cancel = action_cancel;
+  surf_timer_model->common_public->action_recycle = action_recycle;
+  surf_timer_model->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->name = "TIMER";
+  surf_timer_model->common_public->action_set_data =
+      surf_action_set_data;
+  surf_timer_model->common_public->name = "TIMER";
 
-  surf_timer_resource->common_private->resource_used = resource_used;
-  surf_timer_resource->common_private->share_resources = share_resources;
-  surf_timer_resource->common_private->update_actions_state =
+  surf_timer_model->common_private->resource_used = resource_used;
+  surf_timer_model->common_private->share_resources = share_resources;
+  surf_timer_model->common_private->update_actions_state =
       update_actions_state;
-  surf_timer_resource->common_private->update_resource_state =
+  surf_timer_model->common_private->update_resource_state =
       update_resource_state;
-  surf_timer_resource->common_private->finalize = finalize;
+  surf_timer_model->common_private->finalize = finalize;
 
-  surf_timer_resource->common_public->suspend = action_suspend;
-  surf_timer_resource->common_public->resume = action_resume;
-  surf_timer_resource->common_public->is_suspended = action_is_suspended;
+  surf_timer_model->common_public->suspend = action_suspend;
+  surf_timer_model->common_public->resume = action_resume;
+  surf_timer_model->common_public->is_suspended = action_is_suspended;
 
-  surf_timer_resource->extension_public->set = set;
-  surf_timer_resource->extension_public->get = get;
+  surf_timer_model->extension_public->set = set;
+  surf_timer_model->extension_public->get = get;
 
   {
     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));
+    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();
-
-  xbt_assert0(maxmin_system, "surf_init has to be called first!");
+  timer_heap = xbt_heap_new(8, NULL);
 }
 
-void surf_timer_resource_init(const char *filename)
+void surf_timer_model_init(const char *filename)
 {
-  if (surf_timer_resource)
+  if (surf_timer_model)
     return;
-  surf_timer_resource_init_internal();
-  xbt_dynar_push(resource_list, &surf_timer_resource);
+  surf_timer_model_init_internal();
+  xbt_dynar_push(model_list, &surf_timer_model);
 }