From c394d513c7c5cc4348ff885de7161f03da7a6f11 Mon Sep 17 00:00:00 2001 From: alegrand Date: Mon, 6 Aug 2007 14:22:28 +0000 Subject: [PATCH 1/1] Clean the links between action and conditions. Now there are two functions: * SIMIX_register_action_to_condition * SIMIX_unregister_action_to_condition It creates and destroys links in both directions and should always be called together (i.e. "surround" a wait). git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3988 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/include/simix/simix.h | 3 +-- src/simix/private.h | 3 +++ src/simix/smx_action.c | 49 +++++++++++++++++++++++++++++++++++++-- src/simix/smx_global.c | 4 ++-- src/simix/smx_synchro.c | 44 ++++++----------------------------- 5 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/include/simix/simix.h b/src/include/simix/simix.h index bb011dc9c9..ec89b6fe73 100644 --- a/src/include/simix/simix.h +++ b/src/include/simix/simix.h @@ -118,8 +118,6 @@ XBT_PUBLIC(void) SIMIX_cond_wait(smx_cond_t cond,smx_mutex_t mutex); XBT_PUBLIC(void) SIMIX_cond_wait_timeout(smx_cond_t cond,smx_mutex_t mutex, double max_duration); XBT_PUBLIC(void) SIMIX_cond_broadcast(smx_cond_t cond); XBT_PUBLIC(void) SIMIX_cond_destroy(smx_cond_t cond); -XBT_PUBLIC(void) SIMIX_register_condition_to_action(smx_action_t action, smx_cond_t cond); -XBT_PUBLIC(void) SIMIX_unregister_condition_to_action(smx_action_t action, smx_cond_t cond); XBT_PUBLIC(xbt_fifo_t) SIMIX_cond_get_actions(smx_cond_t cond); @@ -132,6 +130,7 @@ XBT_PUBLIC(void) SIMIX_action_cancel(smx_action_t action); XBT_PUBLIC(void) SIMIX_action_set_priority(smx_action_t action, double priority); XBT_PUBLIC(void) SIMIX_action_destroy(smx_action_t action); XBT_PUBLIC(void) SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond); +XBT_PUBLIC(void) SIMIX_unregister_action_to_condition(smx_action_t action, smx_cond_t cond); XBT_PUBLIC(double) SIMIX_action_get_remains(smx_action_t action); XBT_PUBLIC(e_surf_action_state_t) SIMIX_action_get_state(smx_action_t action); diff --git a/src/simix/private.h b/src/simix/private.h index a575278b73..e2d7d04a7e 100644 --- a/src/simix/private.h +++ b/src/simix/private.h @@ -120,4 +120,7 @@ void __SIMIX_host_destroy(smx_host_t host); void __SIMIX_cond_wait(smx_cond_t cond); +void __SIMIX_cond_display_actions(smx_cond_t cond); +void __SIMIX_action_display_conditions(smx_action_t action); + #endif diff --git a/src/simix/smx_action.c b/src/simix/smx_action.c index 31caeab5fe..2abb7c5eaf 100644 --- a/src/simix/smx_action.c +++ b/src/simix/smx_action.c @@ -186,7 +186,6 @@ void SIMIX_action_set_priority(smx_action_t action, double priority) */ void SIMIX_action_destroy(smx_action_t action) { - xbt_assert0((action != NULL), "Invalid parameter"); xbt_assert1((xbt_fifo_size(action->cond_list) == 0), @@ -220,8 +219,34 @@ void SIMIX_register_action_to_condition(smx_action_t action, { xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters"); - DEBUG2("Register action %p to condtion %p", action, cond); + DEBUG2("Register action %p to cond %p", action, cond); + __SIMIX_cond_display_actions(cond); xbt_fifo_push(cond->actions, action); + __SIMIX_cond_display_actions(cond); + DEBUG2("Register condition %p to action %p", cond, action); + __SIMIX_action_display_conditions(action); + xbt_fifo_push(action->cond_list, cond); + __SIMIX_action_display_conditions(action); +} + +/** + * \brief Unset an action to a condition. + * + * Destroys the "links" from the condition to this action. + * \param action SIMIX action + * \param cond SIMIX cond + */ +void SIMIX_unregister_action_to_condition(smx_action_t action, + smx_cond_t cond) +{ + xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters"); + + __SIMIX_cond_display_actions(cond); + xbt_fifo_remove_all(cond->actions, action); + __SIMIX_cond_display_actions(cond); + __SIMIX_action_display_conditions(action); + xbt_fifo_remove_all(action->cond_list, cond); + __SIMIX_action_display_conditions(action); } /** @@ -273,3 +298,23 @@ e_surf_action_state_t SIMIX_action_get_state(smx_action_t action) action_get_state(action->simdata->surf_action); } + +void __SIMIX_cond_display_actions(smx_cond_t cond) +{ + xbt_fifo_item_t item = NULL; + smx_action_t action = NULL; + + DEBUG1("Actions for condition %p", cond); + xbt_fifo_foreach(cond->actions, item, action, smx_action_t) + DEBUG1("\t %p", action); +} + +void __SIMIX_action_display_conditions(smx_action_t action) +{ + xbt_fifo_item_t item = NULL; + smx_cond_t cond = NULL; + + DEBUG1("Conditions for action %p", action); + xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t) + DEBUG1("\t %p", cond); +} diff --git a/src/simix/smx_global.c b/src/simix/smx_global.c index ff96a3dbb5..97526640b1 100644 --- a/src/simix/smx_global.c +++ b/src/simix/smx_global.c @@ -225,7 +225,7 @@ void __SIMIX_main(void) } SIMIX_cond_broadcast(cond); /* remove conditional from action */ - xbt_fifo_remove(smx_action->cond_list, cond); + SIMIX_unregister_action_to_condition(smx_action, cond); } } @@ -240,7 +240,7 @@ void __SIMIX_main(void) } SIMIX_cond_broadcast(cond); /* remove conditional from action */ - xbt_fifo_remove(smx_action->cond_list, cond); + SIMIX_unregister_action_to_condition(smx_action, cond); } } } diff --git a/src/simix/smx_synchro.c b/src/simix/smx_synchro.c index 0139dee630..699cf8870f 100644 --- a/src/simix/smx_synchro.c +++ b/src/simix/smx_synchro.c @@ -185,9 +185,8 @@ void SIMIX_cond_wait(smx_cond_t cond, smx_mutex_t mutex) if (xbt_fifo_size(cond->actions) == 0) { act_sleep = SIMIX_action_sleep(SIMIX_host_self(), -1); SIMIX_register_action_to_condition(act_sleep, cond); - SIMIX_register_condition_to_action(act_sleep, cond); __SIMIX_cond_wait(cond); - xbt_fifo_pop(act_sleep->cond_list); + SIMIX_unregister_action_to_condition(act_sleep, cond); SIMIX_action_destroy(act_sleep); } else { __SIMIX_cond_wait(cond); @@ -243,9 +242,8 @@ void SIMIX_cond_wait_timeout(smx_cond_t cond, smx_mutex_t mutex, if (max_duration >= 0) { act_sleep = SIMIX_action_sleep(SIMIX_host_self(), max_duration); SIMIX_register_action_to_condition(act_sleep, cond); - SIMIX_register_condition_to_action(act_sleep, cond); __SIMIX_cond_wait(cond); - xbt_fifo_remove(act_sleep->cond_list, cond); + SIMIX_unregister_action_to_condition(act_sleep, cond); if (SIMIX_action_get_state(act_sleep) == SURF_ACTION_DONE) { SIMIX_action_destroy(act_sleep); THROW0(timeout_error, 0, "Condition timeout"); @@ -302,43 +300,15 @@ void SIMIX_cond_destroy(smx_cond_t cond) "Cannot destroy conditional since someone is still using it"); xbt_swag_free(cond->sleeping); + DEBUG1("%d actions registered", xbt_fifo_size(cond->actions)); + __SIMIX_cond_display_actions(cond); xbt_fifo_foreach(cond->actions, item, action, smx_action_t) { - SIMIX_unregister_condition_to_action(action, cond); + SIMIX_unregister_action_to_condition(action, cond); } + __SIMIX_cond_display_actions(cond); + xbt_fifo_free(cond->actions); xbt_free(cond); return; } } - -/** - * \brief Set a condition to an action - * - * Creates the "link" between an action and a condition. You have to call this function when you create an action and want to wait its ending. - * \param action SIMIX action - * \param cond SIMIX cond - */ -void SIMIX_register_condition_to_action(smx_action_t action, - smx_cond_t cond) -{ - xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters"); - - DEBUG2("Register condition %p to action %p", cond, action); - xbt_fifo_push(action->cond_list, cond); -} - -/** - * \brief Unset a condition to an action - * - * Destroys the "link" between an action and a condition. - * \param action SIMIX action - * \param cond SIMIX cond - */ -void SIMIX_unregister_condition_to_action(smx_action_t action, - smx_cond_t cond) -{ - xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters"); - - while (xbt_fifo_remove(action->cond_list, cond)) { - } -} -- 2.20.1