From f631f0826fb1b72c44e853682242b26d746be57c Mon Sep 17 00:00:00 2001 From: Arimodu Date: Wed, 27 Sep 2023 21:39:36 +0200 Subject: [PATCH] Avoid breaking change by marking obsolete instead --- IPA.Loader/Config/ConfigRuntime.cs | 70 +++++++++++++++---- IPA.Loader/Config/IConfigStore.cs | 13 ++-- .../GeneratedStoreImpl/IGeneratedStore.cs | 21 +----- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/IPA.Loader/Config/ConfigRuntime.cs b/IPA.Loader/Config/ConfigRuntime.cs index 6033c181..609e579a 100644 --- a/IPA.Loader/Config/ConfigRuntime.cs +++ b/IPA.Loader/Config/ConfigRuntime.cs @@ -25,20 +25,17 @@ namespace IPA.Config => obj?.GetHashCode() ?? 0; } - private static readonly ConcurrentBag configs = new ConcurrentBag(); - private static readonly Action configsChangedAction = () => - { - foreach (var config in configs.Where(c => c.Store != null).ToArray()) - config.Store.SyncAction = () => RequiresSave.Add(() => Save(config)); - }; - private static readonly BlockingCollection RequiresSave = new(); + private static readonly ConcurrentBag configs = new(); + private static readonly AutoResetEvent configsChangedWatcher = new(false); + public static readonly BlockingCollection RequiresSave = new(); private static readonly ConcurrentDictionary watchers = new ConcurrentDictionary(new DirInfoEqComparer()); private static readonly ConcurrentDictionary> watcherTrackConfigs = new ConcurrentDictionary>(); - private static SingleThreadTaskScheduler loadScheduler = null; - private static TaskFactory loadFactory = null; - private static Thread saveThread = null; + private static SingleThreadTaskScheduler loadScheduler; + private static TaskFactory loadFactory; + private static Thread saveThread; + private static Thread legacySaveThread; private static void TryStartRuntime() { @@ -55,6 +52,11 @@ namespace IPA.Config saveThread = new Thread(SaveThread); saveThread.Start(); } + if (legacySaveThread == null || !legacySaveThread.IsAlive) + { + legacySaveThread = new Thread(LegacySaveThread); + legacySaveThread.Start(); + } AppDomain.CurrentDomain.ProcessExit -= ShutdownRuntime; AppDomain.CurrentDomain.ProcessExit += ShutdownRuntime; @@ -92,7 +94,7 @@ namespace IPA.Config configs.Add(cfg); } - configsChangedAction.Invoke(); + configsChangedWatcher.Set(); TryStartRuntime(); @@ -101,7 +103,7 @@ namespace IPA.Config public static void ConfigChanged() { - configsChangedAction.Invoke(); + configsChangedWatcher.Set(); } private static void AddConfigToWatchers(Config config) @@ -218,12 +220,11 @@ namespace IPA.Config { try { - configsChangedAction.Invoke(); foreach (var item in RequiresSave.GetConsumingEnumerable()) { try { - item.Invoke(); + Save(configs.First((c) => ReferenceEquals(c.Store.WriteSyncObject, item.WriteSyncObject))); } catch (ThreadAbortException) { @@ -246,5 +247,46 @@ namespace IPA.Config RequiresSave.Dispose(); } } + + private static void LegacySaveThread() + { + try + { + while (true) + { + var configArr = configs.Where(c => c.Store != null).Where(c => c.Store.SyncObject != null).ToArray(); + int index = -1; + try + { + var waitHandles = configArr.Select(c => c.Store.SyncObject) + .Prepend(configsChangedWatcher) + .ToArray(); + index = WaitHandle.WaitAny(waitHandles); + } + catch (ThreadAbortException) + { + break; + } + catch (Exception e) + { + Logger.Config.Error($"Error waiting for in-memory updates"); + Logger.Config.Error(e); + Thread.Sleep(TimeSpan.FromSeconds(1)); + } + + if (index <= 0) + { // we got a signal that the configs collection changed, loop around, or errored + continue; + } + + // otherwise, we have a thing that changed in a store + Save(configArr[index - 1]); + } + } + catch (ThreadAbortException) + { + // we got aborted :( + } + } } } diff --git a/IPA.Loader/Config/IConfigStore.cs b/IPA.Loader/Config/IConfigStore.cs index 270773bf..fee24f36 100644 --- a/IPA.Loader/Config/IConfigStore.cs +++ b/IPA.Loader/Config/IConfigStore.cs @@ -1,11 +1,5 @@ using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; using System.Threading; -using System.Threading.Tasks; -using static UnityEngine.Random; namespace IPA.Config { @@ -15,11 +9,12 @@ namespace IPA.Config public interface IConfigStore { /// - /// An action that is gonna be called when a save is required + /// A synchronization object for the save thread to wait on for changes. /// It should be signaled whenever the internal state of the object is changed. - /// The writer will never signal this. + /// The writer will never signal this handle. /// - public Action SyncAction { get; set; } + [Obsolete("Add a message here...")] // Do this later + WaitHandle SyncObject { get; } /// /// A synchronization object for the load thread and accessors to maintain safe synchronization. diff --git a/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs b/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs index c28e8455..ee8bc0d8 100644 --- a/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs +++ b/IPA.Loader/Config/Stores/GeneratedStoreImpl/IGeneratedStore.cs @@ -47,16 +47,9 @@ namespace IPA.Config.Stores internal static ConstructorInfo Ctor = typeof(Impl).GetConstructor(new[] { typeof(IGeneratedStore) }); public Impl(IGeneratedStore store) => generated = store; - - public Action? SyncAction { get; set; } - public static Action? ImplGetSyncObject(IGeneratedStore s) => FindImpl(s)?.SyncAction; - public static void ImplSetSyncAction(IGeneratedStore s, Action? value) - { - var impl = FindImpl(s); - if (impl != null) impl.SyncAction = value; - } + public WaitHandle? SyncObject => null; + public static WaitHandle? ImplGetSyncObject(IGeneratedStore s) => FindImpl(s)?.SyncObject; internal static MethodInfo ImplGetSyncObjectMethod = typeof(Impl).GetMethod(nameof(ImplGetSyncObject)); - internal static MethodInfo ImplSetSyncActionMethod = typeof(Impl).GetMethod(nameof(ImplSetSyncAction)); public ReaderWriterLockSlim WriteSyncObject { get; } = new(); public static ReaderWriterLockSlim? ImplGetWriteSyncObject(IGeneratedStore s) => FindImpl(s)?.WriteSyncObject; @@ -66,15 +59,7 @@ namespace IPA.Config.Stores public static void ImplSignalChanged(IGeneratedStore s) => FindImpl(s)?.SignalChanged(); public void SignalChanged() { - try - { - SyncAction?.Invoke(); - } - catch (ObjectDisposedException e) - { - Logger.Config.Error($"ObjectDisposedException while signalling a change for generated store {generated?.GetType()}"); - Logger.Config.Error(e); - } + ConfigRuntime.RequiresSave.Add(this); } internal static MethodInfo ImplInvokeChangedMethod = typeof(Impl).GetMethod(nameof(ImplInvokeChanged));