summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-10-12 14:03:36 +0200
committerThomas Haller <thaller@redhat.com>2019-10-18 22:09:18 +0200
commit58a48acfd942947df69c754962589e02566ac73c (patch)
tree0359f053d652b09fa869ce0abb27214e0a6a13e1
parenta57a1ba2fc411d209f9c159ea58fe71c620c9f48 (diff)
downloadNetworkManager-58a48acfd942947df69c754962589e02566ac73c.tar.gz
libnm: rework caching singleton value in _nm_dbus_bus_type()
No need for g_once_init_enter(). In case of a race, we can just twice determine the value. As long as only one thread wins the race, this is totally fine (also, both threads probably would give the same result anyway).
-rw-r--r--libnm/nm-dbus-helpers.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/libnm/nm-dbus-helpers.c b/libnm/nm-dbus-helpers.c
index 8112b5594e..6272dd5820 100644
--- a/libnm/nm-dbus-helpers.c
+++ b/libnm/nm-dbus-helpers.c
@@ -9,21 +9,22 @@
#include "nm-dbus-interface.h"
-static GBusType nm_bus = G_BUS_TYPE_SYSTEM;
-
GBusType
_nm_dbus_bus_type (void)
{
- static gsize init_value = 0;
+ static volatile int bus_type = G_BUS_TYPE_NONE;
+ int v;
- if (g_once_init_enter (&init_value)) {
+ v = g_atomic_int_get (&bus_type);
+ if (G_UNLIKELY (v == G_BUS_TYPE_NONE)) {
+ v = G_BUS_TYPE_SYSTEM;
if (g_getenv ("LIBNM_USE_SESSION_BUS"))
- nm_bus = G_BUS_TYPE_SESSION;
-
- g_once_init_leave (&init_value, 1);
+ v = G_BUS_TYPE_SESSION;
+ if (!g_atomic_int_compare_and_exchange (&bus_type, G_BUS_TYPE_NONE, v))
+ v = g_atomic_int_get (&bus_type);
+ nm_assert (v != G_BUS_TYPE_NONE);
}
-
- return nm_bus;
+ return v;
}
/* D-Bus has an upper limit on number of Match rules and it's rather easy