diff options
author | Thomas Haller <thaller@redhat.com> | 2018-06-30 11:25:38 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-07-01 18:17:31 +0200 |
commit | 9d3f01a27c5f4e9fefcbad48f22622c54c0938d6 (patch) | |
tree | e288b2104abb3051f5da60d77dbf0c98f9bb0f0b | |
parent | fa9fe466dbf74dd07acfd83ba440a0f48c41d401 (diff) | |
download | NetworkManager-9d3f01a27c5f4e9fefcbad48f22622c54c0938d6.tar.gz |
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.
-rw-r--r-- | libnm-core/nm-utils.c | 19 |
1 files 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 */ |