From 9d3f01a27c5f4e9fefcbad48f22622c54c0938d6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 30 Jun 2018 11:25:38 +0200 Subject: libnm: add code comment about thread-safty of _nm_utils_init() and use atomic Although we don't really need protect for thread safety in _nm_utils_init(), avoid using static variables without lock/atomic operations. libnm is not thread-safe, but we still should try to avoid potential issues whenever it is easy. --- libnm-core/nm-utils.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index b700c7f5fd..702e05f4b8 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -230,19 +230,24 @@ get_system_encodings (void) return cached_encodings; } -/* init libnm */ - -static gboolean initialized = FALSE; +/*****************************************************************************/ static void __attribute__((constructor)) _nm_utils_init (void) { + static int initialized = 0; GModule *self; gpointer func; - if (initialized) + if (g_atomic_int_get (&initialized) != 0) return; - initialized = TRUE; + + /* we don't expect this code to run multiple times, nor on multiple threads. + * + * In practice, it would not be a problem if two threads concurrently try to + * run the initialization code below, all code below itself is thread-safe, + * Hence, a poor-man guard "initialized" above is more than sufficient, + * although it does not guarantee that the code is not run concurrently. */ self = g_module_open (NULL, 0); if (g_module_symbol (self, "nm_util_get_private", &func)) @@ -253,8 +258,12 @@ _nm_utils_init (void) bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); _nm_dbus_errors_init (); + + g_atomic_int_set (&initialized, 1); } +/*****************************************************************************/ + gboolean _nm_utils_is_manager_process; /* ssid helpers */ -- cgit v1.2.1