From 58a48acfd942947df69c754962589e02566ac73c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 12 Oct 2019 14:03:36 +0200 Subject: 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). --- libnm/nm-dbus-helpers.c | 19 ++++++++++--------- 1 file 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 -- cgit v1.2.1