Skip to content

Commit 9b29324

Browse files
Faster locking through .NET 9's System.Threading.Lock
1 parent 07ad88a commit 9b29324

File tree

9 files changed

+15
-9
lines changed

9 files changed

+15
-9
lines changed

Directory.Packages.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.6.24327.7</ExtensionPackageVersion>
99
</PropertyGroup>
1010
<ItemGroup>
11+
<PackageVersion Include="Backport.System.Threading.Lock" Version="1.0.1" />
1112
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
1213
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(ExtensionPackageVersion)" />
1314
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
@@ -33,12 +34,12 @@
3334
<ItemGroup>
3435
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
3536
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
36-
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
37+
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
3738
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
3839
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
3940
<PackageVersion Include="Dapper" Version="2.1.44" />
4041
</ItemGroup>
4142
<ItemGroup>
4243
<GlobalPackageReference Include="PolySharp" Version="1.14.1" />
4344
</ItemGroup>
44-
</Project>
45+
</Project>

src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace WeihanLi.Common.Logging.Serilog;
44

55
public static class SerilogHelper
66
{
7-
private static readonly object Locker = new();
7+
private static readonly Lock Locker = new();
88

99
public static void LogInit(Action<LoggerConfiguration> configureAction)
1010
{

src/WeihanLi.Common.Logging.Serilog/WeihanLi.Common.Logging.Serilog.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
3+
<TargetFrameworks>netstandard2.0;net6.0;net9.0</TargetFrameworks>
44
<PackageProjectUrl>https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common.Logging.Serilog</PackageProjectUrl>
55
<PackageLicenseExpression>MIT</PackageLicenseExpression>
66
<PackageTags>$(PackageTags);logging;serilog</PackageTags>
@@ -10,6 +10,7 @@
1010
<PackageReleaseNotes>
1111
https://github.com/WeihanLi/WeihanLi.Common/blob/dev/docs/ReleaseNotes.md
1212
</PackageReleaseNotes>
13+
<LangVersion>Preview</LangVersion>
1314
</PropertyGroup>
1415
<ItemGroup>
1516
<PackageReference Include="Serilog" />

src/WeihanLi.Common/Aspect/ProxyUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static class ProxyUtils
1717
private static readonly Dictionary<string, Type> _proxyTypes = [];
1818

1919
private const string TargetFieldName = "__target";
20-
private static readonly object _typeLock = new();
20+
private static readonly Lock _typeLock = new();
2121

2222
private static readonly Func<Type, Type?, string> _proxyTypeNameResolver;
2323

src/WeihanLi.Common/DependencyResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace WeihanLi.Common;
1010
/// </summary>
1111
public static class DependencyResolver
1212
{
13-
private static readonly object _lock = new();
13+
private static readonly Lock _lock = new();
1414
public static IDependencyResolver Current { get; private set; } = new DefaultDependencyResolver();
1515

1616
public static TService? ResolveService<TService>() => Current.ResolveService<TService>();

src/WeihanLi.Common/Helpers/InvokeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>
9696

9797
public static Action<Exception>? OnInvokeException { get; set; }
9898

99-
private static readonly object _exitLock = new();
99+
private static readonly Lock _exitLock = new();
100100
private static volatile bool _exited;
101101
private static readonly Lazy<CancellationTokenSource> LazyCancellationTokenSource = new();
102102
private static void InvokeExitHandler(object? sender, EventArgs? args)

src/WeihanLi.Common/Helpers/PeriodBatching/PeriodicBatching.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public abstract class PeriodicBatching<TEvent> : IDisposable where TEvent : clas
3232
private readonly BoundedConcurrentQueue<TEvent> _queue;
3333
private readonly BatchedConnectionStatus _status;
3434
private readonly Queue<TEvent> _waitingBatch = new();
35-
private readonly object _stateLock = new();
35+
private readonly Lock _stateLock = new();
3636
private readonly PortableTimer _timer;
3737
private bool _unloading;
3838
private bool _started;

src/WeihanLi.Common/Helpers/PeriodBatching/PortableTimer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace WeihanLi.Common.Helpers.PeriodBatching;
1818

1919
internal sealed class PortableTimer : IDisposable
2020
{
21-
private readonly object _stateLock = new();
21+
private readonly Lock _stateLock = new();
2222
private readonly Func<CancellationToken, Task> _onTick;
2323
private readonly CancellationTokenSource _cancel = new();
2424
private readonly Timer _timer;

src/WeihanLi.Common/WeihanLi.Common.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageSummary>common libarary,extensions helpers and useful utilities</PackageSummary>
99
<PackageProjectUrl>https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common</PackageProjectUrl>
1010
<NoWarn>$(NoWarn);1591;DE0003;SYSLIB0014;</NoWarn>
11+
<LangVersion>Preview</LangVersion>
1112
</PropertyGroup>
1213
<PropertyGroup>
1314
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@@ -23,6 +24,9 @@
2324
<PackageReference Include="System.ComponentModel.Annotations" />
2425
<PackageReference Include="Microsoft.CSharp" PrivateAssets="All" />
2526
</ItemGroup>
27+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0' OR '$(TargetFramework)' == 'net8.0'">
28+
<PackageReference Include="Backport.System.Threading.Lock" />
29+
</ItemGroup>
2630
<ItemGroup>
2731
<PackageReference Include="Microsoft.Extensions.Configuration" />
2832
<PackageReference Include="Microsoft.Extensions.Logging" />

0 commit comments

Comments
 (0)