Skip to content

Faster locking through .NET 9's System.Threading.Lock #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.6.24327.7</ExtensionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Backport.System.Threading.Lock" Version="1.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(ExtensionPackageVersion)" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
Expand All @@ -33,12 +34,12 @@
<ItemGroup>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
<PackageVersion Include="Dapper" Version="2.1.44" />
</ItemGroup>
<ItemGroup>
<GlobalPackageReference Include="PolySharp" Version="1.14.1" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageIcon>icon.jpg</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>Preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoggerConfiguration> configureAction)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net9.0</TargetFrameworks>
<PackageProjectUrl>https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common.Logging.Serilog</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>$(PackageTags);logging;serilog</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Aspect/ProxyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class ProxyUtils
private static readonly Dictionary<string, Type> _proxyTypes = [];

private const string TargetFieldName = "__target";
private static readonly object _typeLock = new();
private static readonly Lock _typeLock = new();

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

Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/DependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WeihanLi.Common;
/// </summary>
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<TService>() => Current.ResolveService<TService>();
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Helpers/InvokeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static async Task<double> ProfileAsync<T1, T2, T3>(Func<T1, T2, T3, Task>

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

private static readonly object _exitLock = new();
private static readonly Lock _exitLock = new();
private static volatile bool _exited;
private static readonly Lazy<CancellationTokenSource> LazyCancellationTokenSource = new();
private static void InvokeExitHandler(object? sender, EventArgs? args)
Expand Down
3 changes: 3 additions & 0 deletions src/WeihanLi.Common/WeihanLi.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<PackageReference Include="System.ComponentModel.Annotations" />
<PackageReference Include="Microsoft.CSharp" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net7.0' OR '$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Backport.System.Threading.Lock" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Logging" />
Expand Down
Loading