aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Audio.Backends.SoundIo
diff options
context:
space:
mode:
authorMary <me@thog.eu>2021-06-29 19:37:13 +0200
committerGitHub <noreply@github.com>2021-06-29 19:37:13 +0200
commit00ce9eea620652b97b4d3e8cd9218c6fccff8b1c (patch)
treef488b1b378a8ecbeaf54d5a7916062784a5588dc /Ryujinx.Audio.Backends.SoundIo
parentfbb4019ed5c12c4a888c7b09db648ac595366896 (diff)
Fix disposing of IPC sessions server at emulation stop (#2334)
Diffstat (limited to 'Ryujinx.Audio.Backends.SoundIo')
-rw-r--r--Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceDriver.cs26
-rw-r--r--Ryujinx.Audio.Backends.SoundIo/SoundIoHardwareDeviceSession.cs6
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);
+ }
}
}
}