From 9b9529107c61ffabe6e8e8e0f449ddf8f6bbf714 Mon Sep 17 00:00:00 2001 From: Maplewheels Date: Sat, 31 Jan 2026 22:02:16 -0500 Subject: [PATCH] Added limited multithreaded compatibility. Still requires locks. --- .../LuaCs/Services/EventService.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs index 8f825ae6e..38743666d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/EventService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -56,10 +57,10 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin /// ---- Key: Either string identifier or subscriber instance pointer
/// ---- Value: Subscriber delegate
/// - private readonly Dictionary, IEvent>> _subscriptions = new(); - private readonly Dictionary _eventTypeNameAliases = new(); + private readonly ConcurrentDictionary, IEvent>> _subscriptions = new(); + private readonly ConcurrentDictionary _eventTypeNameAliases = new(); private readonly Lazy _pluginManagementService; - private readonly Dictionary>> _luaSubscriptionFactories = new(); + private readonly ConcurrentDictionary>> _luaSubscriptionFactories = new(); /// /// A collection of factories to produce subscribers from a single lua function handle. For legacy Add() API. /// @@ -192,7 +193,7 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin return FluentResults.Result.Ok().WithReason(new Success($"The event {type.Name} is already registered.")); try { - _luaSubscriptionFactories.Add(type, (ident, funcDict) => + _luaSubscriptionFactories.TryAdd(type, (ident, funcDict) => { var runner = T.GetLuaRunner(funcDict); var dict = _subscriptions.TryGetOrSet(type, () => new Dictionary, IEvent>()); @@ -214,7 +215,7 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin public FluentResults.Result UnregisterSafeEvent() where T : IEvent { ((IService)this).CheckDisposed(); - _luaSubscriptionFactories.Remove(typeof(T)); + _luaSubscriptionFactories.TryRemove(typeof(T), out _); if (!_subscriptions.TryGetValue(typeof(T), out var dict)) return FluentResults.Result.Ok(); dict.Values.Where(value => value.IsLuaRunner()).ToImmutableArray().ForEach(Unsubscribe); @@ -257,7 +258,7 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin public void RemoveEventAlias(string alias) { - _eventTypeNameAliases.Remove(alias); + _eventTypeNameAliases.TryRemove(alias, out _); } public void RemoveAllEventAliases() where T : IEvent @@ -266,7 +267,7 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin .Where(kvp => kvp.Value.IsNullOrWhiteSpace() || kvp.Value == typeof(T).Name) .Select(kvp => kvp.Key).ToImmutableArray()) { - _eventTypeNameAliases.Remove(keys); + _eventTypeNameAliases.TryRemove(keys, out _); } } @@ -296,7 +297,7 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin public void ClearAllEventSubscribers() where T : IEvent { - _subscriptions.Remove(typeof(T)); + _subscriptions.TryRemove(typeof(T), out _); if (typeof(IEventAssemblyContextUnloading) == typeof(T)) { this.Subscribe(this); @@ -377,8 +378,8 @@ public partial class EventService : IEventService, IEventAssemblyContextUnloadin continue; foreach (var type in types) { - _subscriptions.Remove(type); - _luaSubscriptionFactories.Remove(type); + _subscriptions.TryRemove(type, out _); + _luaSubscriptionFactories.TryRemove(type, out _); } } }