From 055982ae09386cc8e9da76d5143edef2ada1ae04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Laval?= Date: Tue, 21 Sep 2010 16:15:17 +0100 Subject: [PATCH] Use static field initialization for system/session bus singletons. Fix possible race resulting in double instantiation. --- src/Bus.cs | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/src/Bus.cs b/src/Bus.cs index 0b44b10..2d5190c 100644 --- a/src/Bus.cs +++ b/src/Bus.cs @@ -10,40 +10,18 @@ namespace DBus { public sealed class Bus : Connection { - static Bus systemBus = null; + static Bus systemBus = Address.StarterBusType == "system" ? Starter : Bus.Open (Address.System); public static Bus System { get { - if (systemBus == null) { - try { - if (Address.StarterBusType == "system") - systemBus = Starter; - else - systemBus = Bus.Open (Address.System); - } catch (Exception e) { - throw new Exception ("Unable to open the system message bus.", e); - } - } - return systemBus; } } - static Bus sessionBus = null; + static Bus sessionBus = Address.StarterBusType == "session" ? Starter : Bus.Open (Address.Session); public static Bus Session { get { - if (sessionBus == null) { - try { - if (Address.StarterBusType == "session") - sessionBus = Starter; - else - sessionBus = Bus.Open (Address.Session); - } catch (Exception e) { - throw new Exception ("Unable to open the session message bus.", e); - } - } - return sessionBus; } }