diff options
Diffstat (limited to 'Ryujinx.Audio.Backends.SoundIo')
-rw-r--r-- | Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs | 26 | ||||
-rw-r--r-- | Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs | 6 |
2 files changed, 27 insertions, 5 deletions
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<SoundIoHardwareDeviceSession> _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(); diff --git a/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs b/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs index 925a1cb4..884e75ed 100644 --- a/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs +++ b/Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs @@ -17,6 +17,7 @@ namespace Ryujinx.Audio.Backends.SoundIo private DynamicRingBuffer _ringBuffer; private ulong _playedSampleCount; private ManualResetEvent _updateRequiredEvent; + private int _disposeState; public SoundIoHardwareDeviceSession(SoundIoHardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount) { @@ -435,7 +436,10 @@ namespace Ryujinx.Audio.Backends.SoundIo public override void Dispose() { - Dispose(true); + if (Interlocked.CompareExchange(ref _disposeState, 1, 0) == 0) + { + Dispose(true); + } } } } |