Ryujinx.Ava: fixes for random hangs on exit (#4827)

* Attempt at fixing hang on exit by ending the WindowNotificationManager notification loop, so that the Thread running it can exit.

* explicitly apply the NotificationManager template to allow the notification loop to begin

* NotificationHelper - remove explicity call to ApplyTemplate(). Change to ManualResetEventSlim so we can cancel the Wait on it.

* add a timeout to AudioRenderSystem.Stop()'s waiting for the termination signal, log a warning if this timeout occurs, and continue execution

* NotifiationHelper - cancel first, the CompleteAdding()

* Remove AudioRenderSystem._terminationEvent, redundant

* NotificationHelper - use host.Closing event to trigger cancellation instead of _notifationManager.DetachedFromLogicalTree

* Change NotificationHelper to use an explicit Thread for background work.  Wait on the cancellationToken's WaitHandle so the Thread doesn't have to deal with async. Wrap foreach in try/catch (OperationCanceledException) to swallow the escaping exception from the GetConsumingEnumerable().

* adjust formatting of AsyncWorkQueue constructor to use object initializers consistently

* use AsyncWorkQueue to do everything I added in SetNotificationManager()

* Revert "use AsyncWorkQueue to do everything I added in SetNotificationManager()"

This reverts commit f0e78366b8776ec8e2fef8ab023c0db1833155d3.

* use AsyncWorkQueue to handle the Thread-related changes previously made to NotificationHelper.SetNotificationHelper(). Wrap it in Lazy<T> and force instantiation in the TemplateApplied event handler to accomodate for the fact that AsyncWorkQueue starts immediately, and the notification dispatch loop was being delayed by _templateAppliedEvent.

* impl changes suggested by AcK77

* impl changes suggested by AcK77 (more)
This commit is contained in:
jhorv 2023-05-26 17:57:43 -04:00 committed by GitHub
parent 3b375525fb
commit 42b9c1e8fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 32 deletions

View File

@ -31,7 +31,6 @@ namespace Ryujinx.Audio.Renderer.Server
private AudioRendererRenderingDevice _renderingDevice; private AudioRendererRenderingDevice _renderingDevice;
private AudioRendererExecutionMode _executionMode; private AudioRendererExecutionMode _executionMode;
private IWritableEvent _systemEvent; private IWritableEvent _systemEvent;
private ManualResetEvent _terminationEvent;
private MemoryPoolState _dspMemoryPoolState; private MemoryPoolState _dspMemoryPoolState;
private VoiceContext _voiceContext; private VoiceContext _voiceContext;
private MixContext _mixContext; private MixContext _mixContext;
@ -83,7 +82,6 @@ namespace Ryujinx.Audio.Renderer.Server
public AudioRenderSystem(AudioRendererManager manager, IWritableEvent systemEvent) public AudioRenderSystem(AudioRendererManager manager, IWritableEvent systemEvent)
{ {
_manager = manager; _manager = manager;
_terminationEvent = new ManualResetEvent(false);
_dspMemoryPoolState = MemoryPoolState.Create(MemoryPoolState.LocationType.Dsp); _dspMemoryPoolState = MemoryPoolState.Create(MemoryPoolState.LocationType.Dsp);
_voiceContext = new VoiceContext(); _voiceContext = new VoiceContext();
_mixContext = new MixContext(); _mixContext = new MixContext();
@ -387,11 +385,6 @@ namespace Ryujinx.Audio.Renderer.Server
_isActive = false; _isActive = false;
} }
if (_executionMode == AudioRendererExecutionMode.Auto)
{
_terminationEvent.WaitOne();
}
Logger.Info?.Print(LogClass.AudioRenderer, $"Stopped renderer id {_sessionId}"); Logger.Info?.Print(LogClass.AudioRenderer, $"Stopped renderer id {_sessionId}");
} }
@ -668,8 +661,6 @@ namespace Ryujinx.Audio.Renderer.Server
{ {
if (_isActive) if (_isActive)
{ {
_terminationEvent.Reset();
if (!_manager.Processor.HasRemainingCommands(_sessionId)) if (!_manager.Processor.HasRemainingCommands(_sessionId))
{ {
GenerateCommandList(out CommandList commands); GenerateCommandList(out CommandList commands);
@ -686,10 +677,6 @@ namespace Ryujinx.Audio.Renderer.Server
_isDspRunningBehind = true; _isDspRunningBehind = true;
} }
} }
else
{
_terminationEvent.Set();
}
} }
} }
@ -857,7 +844,6 @@ namespace Ryujinx.Audio.Renderer.Server
} }
_manager.Unregister(this); _manager.Unregister(this);
_terminationEvent.Dispose();
_workBufferMemoryPin.Dispose(); _workBufferMemoryPin.Dispose();
if (MemoryManager is IRefCounted rc) if (MemoryManager is IRefCounted rc)

View File

@ -3,10 +3,10 @@ using Avalonia.Controls;
using Avalonia.Controls.Notifications; using Avalonia.Controls.Notifications;
using Avalonia.Threading; using Avalonia.Threading;
using Ryujinx.Ava.Common.Locale; using Ryujinx.Ava.Common.Locale;
using Ryujinx.Common;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
namespace Ryujinx.Ava.UI.Helpers namespace Ryujinx.Ava.UI.Helpers
{ {
@ -17,7 +17,6 @@ namespace Ryujinx.Ava.UI.Helpers
private static WindowNotificationManager _notificationManager; private static WindowNotificationManager _notificationManager;
private static readonly ManualResetEvent _templateAppliedEvent = new(false);
private static readonly BlockingCollection<Notification> _notifications = new(); private static readonly BlockingCollection<Notification> _notifications = new();
public static void SetNotificationManager(Window host) public static void SetNotificationManager(Window host)
@ -29,25 +28,31 @@ namespace Ryujinx.Ava.UI.Helpers
Margin = new Thickness(0, 0, 15, 40) Margin = new Thickness(0, 0, 15, 40)
}; };
_notificationManager.TemplateApplied += (sender, args) => var maybeAsyncWorkQueue = new Lazy<AsyncWorkQueue<Notification>>(
{ () => new AsyncWorkQueue<Notification>(notification =>
_templateAppliedEvent.Set();
};
Task.Run(async () =>
{
_templateAppliedEvent.WaitOne();
foreach (var notification in _notifications.GetConsumingEnumerable())
{ {
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>
{ {
_notificationManager.Show(notification); _notificationManager.Show(notification);
}); });
},
"UI.NotificationThread",
_notifications),
LazyThreadSafetyMode.ExecutionAndPublication);
await Task.Delay(NotificationDelayInMs / MaxNotifications); _notificationManager.TemplateApplied += (sender, args) =>
{
// NOTE: Force creation of the AsyncWorkQueue.
_ = maybeAsyncWorkQueue.Value;
};
host.Closing += (sender, args) =>
{
if (maybeAsyncWorkQueue.IsValueCreated)
{
maybeAsyncWorkQueue.Value.Dispose();
} }
}); };
} }
public static void Show(string title, string text, NotificationType type, bool waitingExit = false, Action onClick = null, Action onClose = null) public static void Show(string title, string text, NotificationType type, bool waitingExit = false, Action onClick = null, Action onClose = null)

View File

@ -22,9 +22,11 @@ namespace Ryujinx.Common
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
_queue = collection; _queue = collection;
_workerAction = callback; _workerAction = callback;
_workerThread = new Thread(DoWork) { Name = name }; _workerThread = new Thread(DoWork)
{
_workerThread.IsBackground = true; Name = name,
IsBackground = true
};
_workerThread.Start(); _workerThread.Start();
} }