From fc72604befd8e44288ffe7e289deb8f860ac4694 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Thu, 8 Sep 2022 16:43:47 +0200 Subject: C#: Implement disposable pattern and seal `GodotSynchronizationContext` class and related - `GodotSynchronizationContext` - Implements `IDisposable` to dispose of the disposable field `_queue`. - Makes the class sealed. - `GodotTaskScheduler` - Implements `IDisposable` to dispose of the disposable property `Context`. - Makes the class sealed. - `Dispatcher` - Dispose of previous `GodotTaskScheduler` instances before creating a new one. --- modules/mono/glue/GodotSharp/GodotSharp/Core/Dispatcher.cs | 5 ++++- .../GodotSharp/GodotSharp/Core/GodotSynchronizationContext.cs | 11 ++++++++--- .../glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs | 7 ++++++- 3 files changed, 18 insertions(+), 5 deletions(-) (limited to 'modules/mono/glue/GodotSharp') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dispatcher.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dispatcher.cs index e6cb4171a7..e7370b2b05 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dispatcher.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dispatcher.cs @@ -9,7 +9,10 @@ namespace Godot internal static GodotTaskScheduler DefaultGodotTaskScheduler; internal static void InitializeDefaultGodotTaskScheduler() - => DefaultGodotTaskScheduler = new GodotTaskScheduler(); + { + DefaultGodotTaskScheduler?.Dispose(); + DefaultGodotTaskScheduler = new GodotTaskScheduler(); + } public static GodotSynchronizationContext SynchronizationContext => DefaultGodotTaskScheduler.Context; } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotSynchronizationContext.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotSynchronizationContext.cs index 1b599beab5..027eab30fc 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotSynchronizationContext.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotSynchronizationContext.cs @@ -1,13 +1,13 @@ +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; namespace Godot { - public class GodotSynchronizationContext : SynchronizationContext + public sealed class GodotSynchronizationContext : SynchronizationContext, IDisposable { - private readonly BlockingCollection> _queue = - new BlockingCollection>(); + private readonly BlockingCollection> _queue = new(); public override void Post(SendOrPostCallback d, object state) { @@ -24,5 +24,10 @@ namespace Godot workItem.Key(workItem.Value); } } + + public void Dispose() + { + _queue.Dispose(); + } } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs index 408bed71b2..f6c36455b2 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs @@ -10,7 +10,7 @@ namespace Godot /// GodotTaskScheduler contains a linked list of tasks to perform as a queue. Methods /// within the class are used to control the queue and perform the contained tasks. /// - public class GodotTaskScheduler : TaskScheduler + public sealed class GodotTaskScheduler : TaskScheduler, IDisposable { /// /// The current synchronization context. @@ -108,5 +108,10 @@ namespace Godot } } } + + public void Dispose() + { + Context.Dispose(); + } } } -- cgit v1.2.3