From 9b29324fc91498f0964750ad2b072d42d8b724d8 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 12 Aug 2024 11:53:24 +0200 Subject: [PATCH 1/3] Faster locking through .NET 9's System.Threading.Lock --- Directory.Packages.props | 5 +++-- src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs | 2 +- .../WeihanLi.Common.Logging.Serilog.csproj | 3 ++- src/WeihanLi.Common/Aspect/ProxyUtils.cs | 2 +- src/WeihanLi.Common/DependencyResolver.cs | 2 +- src/WeihanLi.Common/Helpers/InvokeHelper.cs | 2 +- .../Helpers/PeriodBatching/PeriodicBatching.cs | 2 +- src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs | 2 +- src/WeihanLi.Common/WeihanLi.Common.csproj | 4 ++++ 9 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index cf845708..d0f508ac 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,6 +8,7 @@ 9.0.0-preview.6.24327.7 + @@ -33,7 +34,7 @@ - + @@ -41,4 +42,4 @@ - + \ No newline at end of file diff --git a/src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs b/src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs index 280f003a..2ccb0e27 100644 --- a/src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs +++ b/src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs @@ -4,7 +4,7 @@ namespace WeihanLi.Common.Logging.Serilog; public static class SerilogHelper { - private static readonly object Locker = new(); + private static readonly Lock Locker = new(); public static void LogInit(Action configureAction) { diff --git a/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj b/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj index 8878a006..5d987845 100644 --- a/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj +++ b/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj @@ -1,6 +1,6 @@  - netstandard2.0;net6.0 + netstandard2.0;net6.0;net9.0 https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common.Logging.Serilog MIT $(PackageTags);logging;serilog @@ -10,6 +10,7 @@ https://github.com/WeihanLi/WeihanLi.Common/blob/dev/docs/ReleaseNotes.md + Preview diff --git a/src/WeihanLi.Common/Aspect/ProxyUtils.cs b/src/WeihanLi.Common/Aspect/ProxyUtils.cs index 6fce4fb7..1e38587e 100644 --- a/src/WeihanLi.Common/Aspect/ProxyUtils.cs +++ b/src/WeihanLi.Common/Aspect/ProxyUtils.cs @@ -17,7 +17,7 @@ internal static class ProxyUtils private static readonly Dictionary _proxyTypes = []; private const string TargetFieldName = "__target"; - private static readonly object _typeLock = new(); + private static readonly Lock _typeLock = new(); private static readonly Func _proxyTypeNameResolver; diff --git a/src/WeihanLi.Common/DependencyResolver.cs b/src/WeihanLi.Common/DependencyResolver.cs index bad3f16e..2afd3b2a 100644 --- a/src/WeihanLi.Common/DependencyResolver.cs +++ b/src/WeihanLi.Common/DependencyResolver.cs @@ -10,7 +10,7 @@ namespace WeihanLi.Common; /// public static class DependencyResolver { - private static readonly object _lock = new(); + private static readonly Lock _lock = new(); public static IDependencyResolver Current { get; private set; } = new DefaultDependencyResolver(); public static TService? ResolveService() => Current.ResolveService(); diff --git a/src/WeihanLi.Common/Helpers/InvokeHelper.cs b/src/WeihanLi.Common/Helpers/InvokeHelper.cs index 2b46697c..ba713833 100644 --- a/src/WeihanLi.Common/Helpers/InvokeHelper.cs +++ b/src/WeihanLi.Common/Helpers/InvokeHelper.cs @@ -96,7 +96,7 @@ public static async Task ProfileAsync(Func public static Action? OnInvokeException { get; set; } - private static readonly object _exitLock = new(); + private static readonly Lock _exitLock = new(); private static volatile bool _exited; private static readonly Lazy LazyCancellationTokenSource = new(); private static void InvokeExitHandler(object? sender, EventArgs? args) diff --git a/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs b/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs index 305fc38d..34ab688d 100644 --- a/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs +++ b/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs @@ -32,7 +32,7 @@ public abstract class PeriodicBatching : IDisposable where TEvent : clas private readonly BoundedConcurrentQueue _queue; private readonly BatchedConnectionStatus _status; private readonly Queue _waitingBatch = new(); - private readonly object _stateLock = new(); + private readonly Lock _stateLock = new(); private readonly PortableTimer _timer; private bool _unloading; private bool _started; diff --git a/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs b/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs index 12656841..34598c91 100644 --- a/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs +++ b/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs @@ -18,7 +18,7 @@ namespace WeihanLi.Common.Helpers.PeriodBatching; internal sealed class PortableTimer : IDisposable { - private readonly object _stateLock = new(); + private readonly Lock _stateLock = new(); private readonly Func _onTick; private readonly CancellationTokenSource _cancel = new(); private readonly Timer _timer; diff --git a/src/WeihanLi.Common/WeihanLi.Common.csproj b/src/WeihanLi.Common/WeihanLi.Common.csproj index d6b74e56..50461aa8 100644 --- a/src/WeihanLi.Common/WeihanLi.Common.csproj +++ b/src/WeihanLi.Common/WeihanLi.Common.csproj @@ -8,6 +8,7 @@ common libarary,extensions helpers and useful utilities https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common $(NoWarn);1591;DE0003;SYSLIB0014; + Preview true @@ -23,6 +24,9 @@ + + + From d8b4b156ac403821b1f73762cf6c385b238b10d1 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 12 Aug 2024 13:09:55 +0200 Subject: [PATCH 2/3] Fixes. --- src/Directory.Build.props | 1 + .../WeihanLi.Common.Logging.Serilog.csproj | 1 - src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs | 2 +- src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs | 2 +- src/WeihanLi.Common/WeihanLi.Common.csproj | 1 - 5 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3d1342a7..de1c9f8e 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -23,6 +23,7 @@ icon.jpg README.md MIT + Preview diff --git a/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj b/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj index 5d987845..a6776692 100644 --- a/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj +++ b/src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj @@ -10,7 +10,6 @@ https://github.com/WeihanLi/WeihanLi.Common/blob/dev/docs/ReleaseNotes.md - Preview diff --git a/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs b/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs index 34ab688d..305fc38d 100644 --- a/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs +++ b/src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs @@ -32,7 +32,7 @@ public abstract class PeriodicBatching : IDisposable where TEvent : clas private readonly BoundedConcurrentQueue _queue; private readonly BatchedConnectionStatus _status; private readonly Queue _waitingBatch = new(); - private readonly Lock _stateLock = new(); + private readonly object _stateLock = new(); private readonly PortableTimer _timer; private bool _unloading; private bool _started; diff --git a/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs b/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs index 34598c91..12656841 100644 --- a/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs +++ b/src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs @@ -18,7 +18,7 @@ namespace WeihanLi.Common.Helpers.PeriodBatching; internal sealed class PortableTimer : IDisposable { - private readonly Lock _stateLock = new(); + private readonly object _stateLock = new(); private readonly Func _onTick; private readonly CancellationTokenSource _cancel = new(); private readonly Timer _timer; diff --git a/src/WeihanLi.Common/WeihanLi.Common.csproj b/src/WeihanLi.Common/WeihanLi.Common.csproj index 50461aa8..3f8d7722 100644 --- a/src/WeihanLi.Common/WeihanLi.Common.csproj +++ b/src/WeihanLi.Common/WeihanLi.Common.csproj @@ -8,7 +8,6 @@ common libarary,extensions helpers and useful utilities https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common $(NoWarn);1591;DE0003;SYSLIB0014; - Preview true From 4703803a8fecafc333a8f1a959756609803a57c8 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 12 Aug 2024 19:51:56 +0200 Subject: [PATCH 3/3] Updated Backport.System.Threading.Lock --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index d0f508ac..7a526abf 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,7 +8,7 @@ 9.0.0-preview.6.24327.7 - +