From cabe2557e4b9bfa7e7632fac6e1256ff76f2a92f Mon Sep 17 00:00:00 2001 From: Nikita Voronchev Date: Thu, 23 May 2019 16:38:10 +0300 Subject: [PATCH] Synchronize Main Loop methods in UiaDbusSource --- .../UiaDbusSource/UiaDbusAutomationSource.cs | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/UiaDbus/UiaDbusSource/UiaDbusAutomationSource.cs b/UiaDbus/UiaDbusSource/UiaDbusAutomationSource.cs index e963bddb1..96f57db1b 100644 --- a/UiaDbus/UiaDbusSource/UiaDbusAutomationSource.cs +++ b/UiaDbus/UiaDbusSource/UiaDbusAutomationSource.cs @@ -702,28 +702,34 @@ private object DeserializeAutomationPropertyValue (string busName, int propId, o private volatile static bool runMainLoop = false; private Thread mainLoop = null; + private object mainLoopMethodsLock = new object (); + private void CheckMainLoop () { - if (mainLoopStarted) - return; - runMainLoop = true; + lock (mainLoopMethodsLock) { + if (mainLoopStarted) + return; + runMainLoop = true; - Log.Info ("UiaDbusAutomationSource: Starting dbus main loop"); - mainLoop = new Thread (new ThreadStart (MainLoop)); - mainLoop.IsBackground = true; - mainLoop.Start (); - mainLoopStarted = true; + Log.Info ("UiaDbusAutomationSource: Starting dbus main loop"); + mainLoop = new Thread (new ThreadStart (MainLoop)); + mainLoop.IsBackground = true; + mainLoop.Start (); + mainLoopStarted = true; + } } private void AbortMainLoop () { - runMainLoop = false; - if (mainLoop != null) { - Log.Info ("UiaDbusAutomationSource: Stopping dbus main loop"); - mainLoop.Abort (); + lock (mainLoopMethodsLock) { + runMainLoop = false; + if (mainLoop != null) { + Log.Info ("UiaDbusAutomationSource: Stopping dbus main loop"); + mainLoop.Abort (); + mainLoop = null; + } + mainLoopStarted = false; } - mainLoopStarted = false; - mainLoop = null; } private void MainLoop () @@ -731,7 +737,11 @@ private void MainLoop () // TODO: Why does it not work if I uncomment this and // do bus.Iterate (); ? //Bus bus = Bus.Session; - while (runMainLoop) { + while (true) { + lock (mainLoopMethodsLock) + if (!runMainLoop) + break; + // TODO: Likely crash source (ndesk-dbus bugs?) try { Bus.Session.Iterate (); @@ -741,7 +751,7 @@ private void MainLoop () "The ThreadAbortException has been catched in the Iterate(). Assume normal program exit.{0}{1}", Environment.NewLine, ex.StackTrace); } catch (Exception e) { - Log.Error ("UiaDbusSource: Exception in iterate: " + e); + Log.Error ("UiaDbusSource iterate error: " + e); } } }