Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleaner use of the refcount introduced by Henri.
[simgrid.git] / src / simix / smx_action.c
index 2abb7c5..ec9ae9c 100644 (file)
@@ -11,7 +11,7 @@
 #include "xbt/ex.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
-                               "Logging specific to SIMIX (action)");
+                                "Logging specific to SIMIX (action)");
 
 /************************************* Actions *********************************/
 /** \brief Creates a new SIMIX action to communicate two hosts.
@@ -25,26 +25,28 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
  *     \return A new SIMIX action
  * */
 smx_action_t SIMIX_action_communicate(smx_host_t sender,
-                                     smx_host_t receiver, char *name,
-                                     double size, double rate)
+                                      smx_host_t receiver, const char *name,
+                                      double size, double rate)
 {
+  smx_action_t act;
+  smx_simdata_action_t simdata;
+
   /* check if the host is active */
-  if (surf_workstation_resource->extension_public->
-      get_state(sender->simdata->host) != SURF_CPU_ON) {
-    THROW1(network_error, 0,
-          "Host %s failed, you cannot call this function", sender->name);
+  if (surf_workstation_model->
+      extension_public->get_state(sender->simdata->host) != SURF_CPU_ON) {
+    THROW1(network_error, 0, "Host %s failed, you cannot call this function",
+           sender->name);
   }
-  if (surf_workstation_resource->extension_public->
-      get_state(receiver->simdata->host) != SURF_CPU_ON) {
-    THROW1(network_error, 0,
-          "Host %s failed, you cannot call this function",
-          receiver->name);
+  if (surf_workstation_model->
+      extension_public->get_state(receiver->simdata->host) != SURF_CPU_ON) {
+    THROW1(network_error, 0, "Host %s failed, you cannot call this function",
+           receiver->name);
   }
 
   /* alloc structures */
-  smx_action_t act = xbt_new0(s_smx_action_t, 1);
+  act = xbt_new0(s_smx_action_t, 1);
   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
-  smx_simdata_action_t simdata = act->simdata;
+  simdata = act->simdata;
   act->cond_list = xbt_fifo_new();
 
   /* initialize them */
@@ -52,12 +54,13 @@ smx_action_t SIMIX_action_communicate(smx_host_t sender,
   simdata->source = sender;
 
 
-  simdata->surf_action = surf_workstation_resource->extension_public->
-      communicate(sender->simdata->host,
-                 receiver->simdata->host, size, rate);
-  surf_workstation_resource->common_public->action_set_data(simdata->
-                                                           surf_action,
-                                                           act);
+  simdata->surf_action =
+    surf_workstation_model->extension_public->communicate(sender->simdata->
+                                                          host,
+                                                          receiver->simdata->
+                                                          host, size, rate);
+  surf_workstation_model->common_public->action_set_data(simdata->surf_action,
+                                                         act);
 
   DEBUG1("Create communicate action %p", act);
   return act;
@@ -71,20 +74,23 @@ smx_action_t SIMIX_action_communicate(smx_host_t sender,
  *     \param amount Task amount (in bytes)
  *     \return A new SIMIX action
  * */
-smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
-                                 double amount)
+smx_action_t SIMIX_action_execute(smx_host_t host, const char *name,
+                                  double amount)
 {
+  smx_action_t act;
+  smx_simdata_action_t simdata;
+
   /* check if the host is active */
-  if (surf_workstation_resource->extension_public->
-      get_state(host->simdata->host) != SURF_CPU_ON) {
+  if (surf_workstation_model->
+      extension_public->get_state(host->simdata->host) != SURF_CPU_ON) {
     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
-          host->name);
+           host->name);
   }
 
   /* alloc structures */
-  smx_action_t act = xbt_new0(s_smx_action_t, 1);
+  act = xbt_new0(s_smx_action_t, 1);
   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
-  smx_simdata_action_t simdata = act->simdata;
+  simdata = act->simdata;
   act->cond_list = xbt_fifo_new();
 
   /* initialize them */
@@ -92,12 +98,12 @@ smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
   act->name = xbt_strdup(name);
 
   /* set communication */
-  simdata->surf_action = surf_workstation_resource->extension_public->
-      execute(host->simdata->host, amount);
+  simdata->surf_action =
+    surf_workstation_model->extension_public->execute(host->simdata->host,
+                                                      amount);
 
-  surf_workstation_resource->common_public->action_set_data(simdata->
-                                                           surf_action,
-                                                           act);
+  surf_workstation_model->common_public->action_set_data(simdata->surf_action,
+                                                         act);
 
   DEBUG1("Create execute action %p", act);
   return act;
@@ -113,30 +119,32 @@ smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
 {
   char name[] = "sleep";
+  smx_simdata_action_t simdata;
+  smx_action_t act;
 
   /* check if the host is active */
-  if (surf_workstation_resource->extension_public->
-      get_state(host->simdata->host) != SURF_CPU_ON) {
+  if (surf_workstation_model->
+      extension_public->get_state(host->simdata->host) != SURF_CPU_ON) {
     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
-          host->name);
+           host->name);
   }
 
   /* alloc structures */
-  smx_action_t act = xbt_new0(s_smx_action_t, 1);
+  act = xbt_new0(s_smx_action_t, 1);
   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
-  smx_simdata_action_t simdata = act->simdata;
+  simdata = act->simdata;
   act->cond_list = xbt_fifo_new();
 
   /* initialize them */
   simdata->source = host;
   act->name = xbt_strdup(name);
 
-  simdata->surf_action = surf_workstation_resource->extension_public->
-      sleep(host->simdata->host, duration);
+  simdata->surf_action =
+    surf_workstation_model->extension_public->sleep(host->simdata->host,
+                                                    duration);
 
-  surf_workstation_resource->common_public->action_set_data(simdata->
-                                                           surf_action,
-                                                           act);
+  surf_workstation_model->common_public->action_set_data(simdata->surf_action,
+                                                         act);
 
   DEBUG1("Create sleep action %p", act);
   return act;
@@ -154,9 +162,9 @@ void SIMIX_action_cancel(smx_action_t action)
 
   DEBUG1("Cancel action %p", action);
   if (action->simdata->surf_action) {
-    surf_workstation_resource->common_public->action_cancel(action->
-                                                           simdata->
-                                                           surf_action);
+    surf_workstation_model->common_public->action_cancel(action->
+                                                         simdata->
+                                                         surf_action);
   }
   return;
 }
@@ -171,10 +179,10 @@ void SIMIX_action_cancel(smx_action_t action)
 void SIMIX_action_set_priority(smx_action_t action, double priority)
 {
   xbt_assert0((action != NULL)
-             && (action->simdata != NULL), "Invalid parameter");
+              && (action->simdata != NULL), "Invalid parameter");
 
-  surf_workstation_resource->common_public->
-      set_priority(action->simdata->surf_action, priority);
+  surf_workstation_model->common_public->set_priority(action->simdata->
+                                                      surf_action, priority);
   return;
 }
 
@@ -184,13 +192,18 @@ void SIMIX_action_set_priority(smx_action_t action, double priority)
  *     Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it. 
  *     \param action The SIMIX action
  */
-void SIMIX_action_destroy(smx_action_t action)
+int SIMIX_action_destroy(smx_action_t action)
 {
+  XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
   xbt_assert0((action != NULL), "Invalid parameter");
 
+  action->refcount--;
+  if (action->refcount > 0)
+    return 0;
+
   xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
-             "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
-             xbt_fifo_size(action->cond_list));
+              "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
+              xbt_fifo_size(action->cond_list));
 
   DEBUG1("Destroy action %p", action);
   if (action->name)
@@ -199,11 +212,40 @@ void SIMIX_action_destroy(smx_action_t action)
   xbt_fifo_free(action->cond_list);
 
   if (action->simdata->surf_action)
-    action->simdata->surf_action->resource_type->common_public->
-       action_free(action->simdata->surf_action);
+    action->simdata->surf_action->model_type->
+      common_public->action_free(action->simdata->surf_action);
 
   xbt_free(action->simdata);
   xbt_free(action);
+  return 1;
+}
+
+/**
+ *     \brief Increase refcount of anan action
+ *
+ *     \param action The SIMIX action
+ */
+void SIMIX_action_use(smx_action_t action)
+{
+  XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
+  xbt_assert0((action != NULL), "Invalid parameter");
+
+  action->refcount++;
+
+  return;
+}
+
+/**
+ *     \brief Decrease refcount of anan action
+ *
+ *     \param action The SIMIX action
+ */
+void SIMIX_action_release(smx_action_t action)
+{
+  xbt_assert0((action != NULL), "Invalid parameter");
+
+  action->refcount--;
+
   return;
 }
 
@@ -214,8 +256,7 @@ void SIMIX_action_destroy(smx_action_t action)
  *     \param action SIMIX action
  *     \param cond SIMIX cond
  */
-void SIMIX_register_action_to_condition(smx_action_t action,
-                                       smx_cond_t cond)
+void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
 {
   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
 
@@ -237,7 +278,7 @@ void SIMIX_register_action_to_condition(smx_action_t action,
  *     \param cond SIMIX cond
  */
 void SIMIX_unregister_action_to_condition(smx_action_t action,
-                                         smx_cond_t cond)
+                                          smx_cond_t cond)
 {
   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
 
@@ -261,32 +302,42 @@ double SIMIX_action_get_remains(smx_action_t action)
   return action->simdata->surf_action->remains;
 }
 
-smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb,
-                                          void **workstation_list,
-                                          double *computation_amount,
-                                          double *communication_amount,
-                                          double amount, double rate)
+smx_action_t SIMIX_action_parallel_execute(char *name, int host_nb,
+                                           smx_host_t * host_list,
+                                           double *computation_amount,
+                                           double *communication_amount,
+                                           double amount, double rate)
 {
+  void **workstation_list = NULL;
+  smx_simdata_action_t simdata;
+  smx_action_t act;
+  int i;
 
   /* alloc structures */
-  smx_action_t act = xbt_new0(s_smx_action_t, 1);
+  act = xbt_new0(s_smx_action_t, 1);
   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
-  smx_simdata_action_t simdata = act->simdata;
+  simdata = act->simdata;
   act->cond_list = xbt_fifo_new();
 
   /* initialize them */
   act->name = xbt_strdup(name);
 
-  /* set communication */
+  /* set action */
+
+  workstation_list = xbt_new0(void *, host_nb);
+  for (i = 0; i < host_nb; i++)
+    workstation_list[i] = host_list[i]->simdata->host;
+
   simdata->surf_action =
-      surf_workstation_resource->extension_public->
-      execute_parallel_task(workstation_nb, workstation_list,
-                           computation_amount, communication_amount,
-                           amount, rate);
+    surf_workstation_model->extension_public->execute_parallel_task(host_nb,
+                                                                    workstation_list,
+                                                                    computation_amount,
+                                                                    communication_amount,
+                                                                    amount,
+                                                                    rate);
 
-  surf_workstation_resource->common_public->action_set_data(simdata->
-                                                           surf_action,
-                                                           act);
+  surf_workstation_model->common_public->action_set_data(simdata->surf_action,
+                                                         act);
 
   return act;
 }
@@ -294,8 +345,9 @@ smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb,
 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
 {
   xbt_assert0((action != NULL), "Invalid parameter");
-  return surf_workstation_resource->common_public->
-      action_get_state(action->simdata->surf_action);
+  return surf_workstation_model->common_public->action_get_state(action->
+                                                                 simdata->
+                                                                 surf_action);
 
 }
 
@@ -306,7 +358,7 @@ void __SIMIX_cond_display_actions(smx_cond_t cond)
 
   DEBUG1("Actions for condition %p", cond);
   xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
-      DEBUG1("\t %p", action);
+    DEBUG2("\t %p [%s]", action, action->name);
 }
 
 void __SIMIX_action_display_conditions(smx_action_t action)
@@ -316,5 +368,5 @@ void __SIMIX_action_display_conditions(smx_action_t action)
 
   DEBUG1("Conditions for action %p", action);
   xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
-      DEBUG1("\t %p", cond);
+    DEBUG1("\t %p", cond);
 }