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) --- .../SoundIoHardwareDeviceDriver.cs | 26 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs') diff --git a/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs b/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs index 00977fcb..b9b549e6 100644 --- a/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs +++ b/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs @@ -18,6 +18,7 @@ namespace Ryujinx.Audio.Backends.SoundIo private SoundIODevice _audioDevice; private ManualResetEvent _updateRequiredEvent; private List _sessions; + private int _disposeState; public SoundIoHardwareDeviceDriver() { @@ -208,19 +209,36 @@ namespace Ryujinx.Audio.Backends.SoundIo public void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } protected virtual void Dispose(bool disposing) { if (disposing) { - while (_sessions.Count > 0) + int sessionCount = 0; + + // NOTE: This is done in a way to avoid possible situations when the SoundIoHardwareDeviceSession is already being dispose in another thread but doesn't hold the lock and tries to Unregister. + do { - SoundIoHardwareDeviceSession session = _sessions[_sessions.Count - 1]; + lock (_lock) + { + if (_sessions.Count == 0) + { + break; + } + + SoundIoHardwareDeviceSession session = _sessions[_sessions.Count - 1]; + + session.Dispose(); - session.Dispose(); + sessionCount = _sessions.Count; + } } + while (sessionCount > 0); _audioContext.Disconnect(); _audioContext.Dispose(); -- cgit v1.2.3