From 0576a2ebb888be6947230ce6c46331224adc24d3 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Mon, 27 Apr 2020 19:24:18 -0500 Subject: [PATCH] Fixed lack of strong reference to the tasks in UnityMainThreadTaskScheduler --- .../Async/SingleCreationValueCache.cs | 2 +- .../Async/UnityMainThreadTaskScheduler.cs | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/IPA.Loader/Utilities/Async/SingleCreationValueCache.cs b/IPA.Loader/Utilities/Async/SingleCreationValueCache.cs index 71d42e8a..ea361c55 100644 --- a/IPA.Loader/Utilities/Async/SingleCreationValueCache.cs +++ b/IPA.Loader/Utilities/Async/SingleCreationValueCache.cs @@ -105,7 +105,7 @@ namespace IPA.Utilities.Async /// if the value was found, otherwise public bool TryGetValue(TKey key, out TValue value) { - if (dict.TryGetValue(key, out var pair) && pair.wh != null) + if (dict.TryGetValue(key, out var pair) && pair.wh == null) { value = pair.val; return true; diff --git a/IPA.Loader/Utilities/Async/UnityMainThreadTaskScheduler.cs b/IPA.Loader/Utilities/Async/UnityMainThreadTaskScheduler.cs index eb53fc92..e3fb3f54 100644 --- a/IPA.Loader/Utilities/Async/UnityMainThreadTaskScheduler.cs +++ b/IPA.Loader/Utilities/Async/UnityMainThreadTaskScheduler.cs @@ -32,20 +32,27 @@ namespace IPA.Utilities.Async private class QueueItem : IEquatable, IEquatable { - public bool HasTask; - private readonly WeakReference weakTask = null; - public Task Task => weakTask.TryGetTarget(out var task) ? task : null; + private bool hasTask; + public bool HasTask + { + get => hasTask; + set + { + hasTask = value; + if (!hasTask) Task = null; + } + } + + public Task Task { get; private set; } = null; public QueueItem(Task task) { HasTask = true; - weakTask = new WeakReference(task); + Task = task; } - private bool Equals(WeakReference task) - => weakTask.TryGetTarget(out var t1) && task.TryGetTarget(out var t2) && t1.Equals(t2); - public bool Equals(Task other) => HasTask && weakTask.TryGetTarget(out var task) && other.Equals(task); - public bool Equals(QueueItem other) => other.HasTask == HasTask && Equals(other.weakTask); + public bool Equals(Task other) => HasTask && other.Equals(Task); + public bool Equals(QueueItem other) => other.HasTask == HasTask && Equals(other.Task); } /// @@ -174,10 +181,7 @@ namespace IPA.Utilities.Async /// nothing /// Always. protected override IEnumerable GetScheduledTasks() - { - // this is only for debuggers which we can't use sooooo - throw new NotSupportedException(); - } + => tasks.ToArray().Where(q => q.HasTask).Select(q => q.Task).ToArray(); /// /// Queues a given to this scheduler. The must be @@ -199,7 +203,7 @@ namespace IPA.Utilities.Async /// /// the task to attempt to execute /// whether the task was previously queued to this scheduler - /// + /// if the task could not be run, if it was /// Thrown if this object has already been disposed. protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued) {