From ed8f133b3bcee22984faa7d3ed48340ba4f9f68c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Christophe=20Thi=C3=A9ry?= Date: Fri, 13 Jan 2012 17:04:03 +0100 Subject: [PATCH] Lua: close the Lua world of a process being destroyed Also make sure MSG_clean is called only once (by maestro). --- include/msg/msg.h | 1 + src/bindings/lua/simgrid_lua.c | 5 ++++- src/msg/msg_global.c | 1 + src/msg/msg_private.h | 1 + src/msg/msg_process.c | 18 ++++++++++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/msg/msg.h b/include/msg/msg.h index f77200a050..ea1e2a881d 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -90,6 +90,7 @@ XBT_PUBLIC(MSG_error_t) MSG_process_migrate(m_process_t process, m_host_t host); XBT_PUBLIC(void *) MSG_process_get_data(m_process_t process); XBT_PUBLIC(MSG_error_t) MSG_process_set_data(m_process_t process, void *data); +XBT_PUBLIC(void) MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup); XBT_PUBLIC(m_host_t) MSG_process_get_host(m_process_t process); XBT_PUBLIC(m_process_t) MSG_process_from_PID(int PID); XBT_PUBLIC(int) MSG_process_get_PID(m_process_t process); diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index 2df56d5690..6877c67d2d 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -185,7 +185,9 @@ static int get_clock(lua_State* L) { */ static int simgrid_gc(lua_State * L) { - MSG_clean(); + if (sglua_is_maestro(L)) { + MSG_clean(); + } return 0; } @@ -317,6 +319,7 @@ int luaopen_simgrid(lua_State *L) /* Initialize the MSG core */ MSG_global_init(&argc, argv); + MSG_process_set_data_cleanup((void_f_pvoid_t) lua_close); XBT_DEBUG("Still %d arguments on command line", argc); // FIXME: update the lua's arg table to reflect the changes from SimGrid } diff --git a/src/msg/msg_global.c b/src/msg/msg_global.c index b10d9c421b..b7d7b6e88e 100644 --- a/src/msg/msg_global.c +++ b/src/msg/msg_global.c @@ -68,6 +68,7 @@ void MSG_global_init(int *argc, char **argv) msg_global->PID = 1; msg_global->sent_msg = 0; msg_global->task_copy_callback = NULL; + msg_global->process_data_cleanup = NULL; /* initialization of the action module */ _MSG_action_init(); diff --git a/src/msg/msg_private.h b/src/msg/msg_private.h index 0b7ff957f3..7b60abf834 100644 --- a/src/msg/msg_private.h +++ b/src/msg/msg_private.h @@ -86,6 +86,7 @@ typedef struct MSG_Global { int session; unsigned long int sent_msg; /* Total amount of messages sent during the simulation */ void (*task_copy_callback) (m_task_t task, m_process_t src, m_process_t dst); + void_f_pvoid_t process_data_cleanup; } s_MSG_Global_t, *MSG_Global_t; /*extern MSG_Global_t msg_global;*/ diff --git a/src/msg/msg_process.c b/src/msg/msg_process.c index 5030416b5e..931ec4e19c 100644 --- a/src/msg/msg_process.c +++ b/src/msg/msg_process.c @@ -35,6 +35,7 @@ void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc) { simdata_process_t msg_proc; + // get the MSG process from the SIMIX process if (smx_proc == SIMIX_process_self()) { /* avoid a SIMIX request if this function is called by the process itself */ msg_proc = SIMIX_process_self_get_data(smx_proc); @@ -49,6 +50,12 @@ void MSG_process_cleanup_from_SIMIX(smx_process_t smx_proc) TRACE_msg_process_end(smx_proc); #endif + // free the data if a function was provided + if (msg_proc->data && msg_global->process_data_cleanup) { + msg_global->process_data_cleanup(msg_proc->data); + } + + // free the MSG process xbt_free(msg_proc); } @@ -263,6 +270,17 @@ MSG_error_t MSG_process_set_data(m_process_t process, void *data) return MSG_OK; } +/** \ingroup m_process_management + * \brief Sets a cleanup function to be called to free the userdata of a + * process when a process is destroyed. + * \param data_cleanup a cleanup function for the userdata of a process, + * or NULL to call no function + */ +XBT_PUBLIC(void) MSG_process_set_data_cleanup(void_f_pvoid_t data_cleanup) { + + msg_global->process_data_cleanup = data_cleanup; +} + /** \ingroup m_process_management * \brief Return the location on which an agent is running. * \param process a process (NULL means the current one) -- 2.20.1