Remove singleton from dbuswriter

When you create multiple zones, they are added to multiple
threads. Doing so causes multiple threads to access the
single bus object, which is illegal in sd_bus and
occasionally causes a crash on start like the below.

Assertion 'IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)' failed at
../git/src/libsystemd/sd-bus/sd-bus.c:1720, function dispatch_wqueue().
Aborting.
Aborted (core dumped)

Tested-by: Started swampd multiple times and crash went away.

Change-Id: Ic44b93e51ecf3d39cd8b846bb3003516cdaf7816
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/dbus/dbuswrite.cpp b/dbus/dbuswrite.cpp
index dff11d6..9d8b5a9 100644
--- a/dbus/dbuswrite.cpp
+++ b/dbus/dbuswrite.cpp
@@ -26,11 +26,6 @@
 
 using namespace phosphor::logging;
 
-// this bus object is treated as a singleton because the class is constructed in
-// a different thread than it is used, and as bus objects are relatively
-// expensive we'd prefer to only have one
-std::unique_ptr<sdbusplus::bus::bus> writeBus = nullptr;
-
 std::unique_ptr<WriteInterface>
     DbusWritePercent::createDbusWrite(const std::string& path, int64_t min,
                                       int64_t max, DbusHelperInterface& helper)
@@ -50,15 +45,6 @@
     return std::make_unique<DbusWritePercent>(path, min, max, connectionName);
 }
 
-void initBus()
-{
-    if (writeBus == nullptr)
-    {
-        writeBus = std::make_unique<sdbusplus::bus::bus>(
-            sdbusplus::bus::new_default());
-    }
-}
-
 void DbusWritePercent::write(double value)
 {
     double minimum = getMin();
@@ -72,17 +58,17 @@
     {
         return;
     }
-    initBus();
+    auto writeBus = sdbusplus::bus::new_default();
     auto mesg =
-        writeBus->new_method_call(connectionName.c_str(), path.c_str(),
-                                  "org.freedesktop.DBus.Properties", "Set");
+        writeBus.new_method_call(connectionName.c_str(), path.c_str(),
+                                 "org.freedesktop.DBus.Properties", "Set");
     mesg.append(pwmInterface, "Target",
                 sdbusplus::message::variant<uint64_t>(ovalue));
 
     try
     {
         // TODO: if we don't use the reply, call_noreply()
-        auto resp = writeBus->call(mesg);
+        auto resp = writeBus.call(mesg);
     }
     catch (const sdbusplus::exception::SdBusError& ex)
     {
@@ -119,17 +105,17 @@
     {
         return;
     }
-    initBus();
+    auto writeBus = sdbusplus::bus::new_default();
     auto mesg =
-        writeBus->new_method_call(connectionName.c_str(), path.c_str(),
-                                  "org.freedesktop.DBus.Properties", "Set");
+        writeBus.new_method_call(connectionName.c_str(), path.c_str(),
+                                 "org.freedesktop.DBus.Properties", "Set");
     mesg.append(pwmInterface, "Target",
                 sdbusplus::message::variant<uint64_t>(value));
 
     try
     {
         // TODO: consider call_noreplly
-        auto resp = writeBus->call(mesg);
+        auto resp = writeBus.call(mesg);
     }
     catch (const sdbusplus::exception::SdBusError& ex)
     {