From 32965191d7fcfb85c4f0da9f118826b70f7d32ba Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Mon, 13 Mar 2017 01:22:21 +0100 Subject: [PATCH] MSG_process_ref/unref: allow to fiddle with the process refcounting This is useful to avoid that a process gets deleted before we're done with it. This is what happened in the process-join example, where we were joining the process after its end. So we have to increase its refcount while it's still alive to make sure that it does not get destroyed too early. This would be automated in C++ with RAII but we are not there yet. --- ChangeLog | 1 + examples/msg/process-join/process-join.c | 4 +++- examples/msg/process-join/process-join.tesh | 2 +- include/simgrid/msg.h | 2 ++ src/msg/msg_process.cpp | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index a3f35c5741..037b902301 100644 --- a/ChangeLog +++ b/ChangeLog @@ -13,6 +13,7 @@ SimGrid (3.15) UNRELEASED; urgency=low - New: MSG_process_yield(). Stop and yield to other processes. - New: MSG_process_daemon(). Daemon processes are automatically killed when the last non-daemon process terminates + - New: MSG_process_ref/unref(). Fiddle with the process refcounting. - Renamed MSG_energy_plugin_init() -> MSG_host_energy_plugin_init() to make room for the upcoming network energy plugin. diff --git a/examples/msg/process-join/process-join.c b/examples/msg/process-join/process-join.c index 282c342ca4..08000a5f27 100644 --- a/examples/msg/process-join/process-join.c +++ b/examples/msg/process-join/process-join.c @@ -37,10 +37,12 @@ static int master(int argc, char* argv[]) XBT_INFO("Start slave"); process = MSG_process_create("slave from master", slave, NULL, MSG_host_self()); + MSG_process_ref(process); // We have to take that ref because the process will stop before we join it XBT_INFO("Waiting 4"); MSG_process_sleep(4); - XBT_INFO("Join the slave (timeout 1)"); + XBT_INFO("Join the slave after its end (timeout 1)"); MSG_process_join(process, 1); + MSG_process_unref(process); // Avoid to leak memory XBT_INFO("Goodbye now!"); diff --git a/examples/msg/process-join/process-join.tesh b/examples/msg/process-join/process-join.tesh index 5147a7973d..3f5bc3277a 100644 --- a/examples/msg/process-join/process-join.tesh +++ b/examples/msg/process-join/process-join.tesh @@ -16,7 +16,7 @@ $ ./process-join$EXEEXT ${srcdir:=.}/small_platform.xml > [Tremblay:master:(1) 7.000000] [msg_test/INFO] Waiting 4 > [Tremblay:slave from master:(4) 8.000000] [msg_test/INFO] I'm done. See you! > [Tremblay:slave from master:(5) 10.000000] [msg_test/INFO] I'm done. See you! -> [Tremblay:master:(1) 11.000000] [msg_test/INFO] Join the slave (timeout 1) +> [Tremblay:master:(1) 11.000000] [msg_test/INFO] Join the slave after its end (timeout 1) > [Tremblay:master:(1) 11.000000] [msg_test/INFO] Goodbye now! > [Tremblay:master:(1) 12.000000] [msg_test/INFO] Goodbye now! > [12.000000] [msg_test/INFO] Simulation time 12 diff --git a/include/simgrid/msg.h b/include/simgrid/msg.h index c553e6e088..8bf566d858 100644 --- a/include/simgrid/msg.h +++ b/include/simgrid/msg.h @@ -318,6 +318,8 @@ XBT_PUBLIC(void) MSG_process_auto_restart_set(msg_process_t process, int auto_re XBT_PUBLIC(void) MSG_process_daemonize(msg_process_t process); XBT_PUBLIC(msg_process_t) MSG_process_restart(msg_process_t process); +XBT_PUBLIC(void) MSG_process_ref(msg_process_t process); +XBT_PUBLIC(void) MSG_process_unref(msg_process_t process); /************************** Task handling ************************************/ XBT_PUBLIC(msg_task_t) MSG_task_create(const char *name, diff --git a/src/msg/msg_process.cpp b/src/msg/msg_process.cpp index cf9b59da86..eeb371925e 100644 --- a/src/msg/msg_process.cpp +++ b/src/msg/msg_process.cpp @@ -476,4 +476,19 @@ XBT_PUBLIC(void) MSG_process_daemonize(msg_process_t process) }); } +/** @ingroup m_process_management + * @brief Take an extra reference on that process to prevent it to be garbage-collected + */ +XBT_PUBLIC(void) MSG_process_ref(msg_process_t process) +{ + intrusive_ptr_add_ref(process); +} +/** @ingroup m_process_management + * @brief Release a reference on that process so that it can get be garbage-collected + */ +XBT_PUBLIC(void) MSG_process_unref(msg_process_t process) +{ + intrusive_ptr_release(process); +} + SG_END_DECL() -- 2.20.1