From 00ce9eea620652b97b4d3e8cd9218c6fccff8b1c Mon Sep 17 00:00:00 2001 From: Mary Date: Tue, 29 Jun 2021 19:37:13 +0200 Subject: Fix disposing of IPC sessions server at emulation stop (#2334) --- Ryujinx.Audio/Input/AudioInputManager.cs | 25 ++++++++++++++++++++-- Ryujinx.Audio/Input/AudioInputSystem.cs | 13 +++++++++-- Ryujinx.Audio/Output/AudioOutputManager.cs | 25 ++++++++++++++++++++-- Ryujinx.Audio/Output/AudioOutputSystem.cs | 11 +++++++++- Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs | 7 +++++- .../Renderer/Server/AudioRendererManager.cs | 10 ++++++++- 6 files changed, 82 insertions(+), 9 deletions(-) (limited to 'Ryujinx.Audio') diff --git a/Ryujinx.Audio/Input/AudioInputManager.cs b/Ryujinx.Audio/Input/AudioInputManager.cs index e098ae9e..5c1f01db 100644 --- a/Ryujinx.Audio/Input/AudioInputManager.cs +++ b/Ryujinx.Audio/Input/AudioInputManager.cs @@ -21,6 +21,8 @@ using Ryujinx.Common.Logging; using Ryujinx.Memory; using System; using System.Diagnostics; +using System.Linq; +using System.Threading; namespace Ryujinx.Audio.Input { @@ -61,6 +63,11 @@ namespace Ryujinx.Audio.Input /// private int _activeSessionCount; + /// + /// The dispose state. + /// + private int _disposeState; + /// /// Create a new . /// @@ -248,14 +255,28 @@ namespace Ryujinx.Audio.Input public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) { if (disposing) { - // Nothing to do here. + // Clone the sessions array to dispose them outside the lock. + AudioInputSystem[] sessions; + + lock (_sessionLock) + { + sessions = _sessions.ToArray(); + } + + foreach (AudioInputSystem input in sessions) + { + input?.Dispose(); + } } } } diff --git a/Ryujinx.Audio/Input/AudioInputSystem.cs b/Ryujinx.Audio/Input/AudioInputSystem.cs index 8064a947..b3fd91e7 100644 --- a/Ryujinx.Audio/Input/AudioInputSystem.cs +++ b/Ryujinx.Audio/Input/AudioInputSystem.cs @@ -18,6 +18,7 @@ using Ryujinx.Audio.Common; using Ryujinx.Audio.Integration; using System; +using System.Threading; namespace Ryujinx.Audio.Input { @@ -62,10 +63,15 @@ namespace Ryujinx.Audio.Input private AudioInputManager _manager; /// - /// THe lock of the parent. + /// The lock of the parent. /// private object _parentLock; + /// + /// The dispose state. + /// + private int _disposeState; + /// /// Create a new . /// @@ -384,7 +390,10 @@ namespace Ryujinx.Audio.Input public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) diff --git a/Ryujinx.Audio/Output/AudioOutputManager.cs b/Ryujinx.Audio/Output/AudioOutputManager.cs index baa84997..852632fa 100644 --- a/Ryujinx.Audio/Output/AudioOutputManager.cs +++ b/Ryujinx.Audio/Output/AudioOutputManager.cs @@ -21,6 +21,8 @@ using Ryujinx.Common.Logging; using Ryujinx.Memory; using System; using System.Diagnostics; +using System.Linq; +using System.Threading; namespace Ryujinx.Audio.Output { @@ -61,6 +63,11 @@ namespace Ryujinx.Audio.Output /// private int _activeSessionCount; + /// + /// The dispose state. + /// + private int _disposeState; + /// /// Create a new . /// @@ -242,14 +249,28 @@ namespace Ryujinx.Audio.Output public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) { if (disposing) { - // Nothing to do here. + // Clone the sessions array to dispose them outside the lock. + AudioOutputSystem[] sessions; + + lock (_sessionLock) + { + sessions = _sessions.ToArray(); + } + + foreach (AudioOutputSystem output in sessions) + { + output?.Dispose(); + } } } } diff --git a/Ryujinx.Audio/Output/AudioOutputSystem.cs b/Ryujinx.Audio/Output/AudioOutputSystem.cs index f5db9d7a..d32d417a 100644 --- a/Ryujinx.Audio/Output/AudioOutputSystem.cs +++ b/Ryujinx.Audio/Output/AudioOutputSystem.cs @@ -18,6 +18,7 @@ using Ryujinx.Audio.Common; using Ryujinx.Audio.Integration; using System; +using System.Threading; namespace Ryujinx.Audio.Output { @@ -66,6 +67,11 @@ namespace Ryujinx.Audio.Output /// private object _parentLock; + /// + /// The dispose state. + /// + private int _disposeState; + /// /// Create a new . /// @@ -357,7 +363,10 @@ namespace Ryujinx.Audio.Output public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) diff --git a/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs b/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs index 943a2d78..6aed3c5d 100644 --- a/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs +++ b/Ryujinx.Audio/Renderer/Server/AudioRenderSystem.cs @@ -94,6 +94,8 @@ namespace Ryujinx.Audio.Renderer.Server private AudioRendererManager _manager; + private int _disposeState; + public AudioRenderSystem(AudioRendererManager manager, IWritableEvent systemEvent) { _manager = manager; @@ -811,7 +813,10 @@ namespace Ryujinx.Audio.Renderer.Server public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) diff --git a/Ryujinx.Audio/Renderer/Server/AudioRendererManager.cs b/Ryujinx.Audio/Renderer/Server/AudioRendererManager.cs index 004ac656..71d0f318 100644 --- a/Ryujinx.Audio/Renderer/Server/AudioRendererManager.cs +++ b/Ryujinx.Audio/Renderer/Server/AudioRendererManager.cs @@ -82,6 +82,11 @@ namespace Ryujinx.Audio.Renderer.Server /// public AudioProcessor Processor { get; } + /// + /// The dispose state. + /// + private int _disposeState; + /// /// Create a new . /// @@ -313,7 +318,10 @@ namespace Ryujinx.Audio.Renderer.Server public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) -- cgit v1.2.3