Skip to content
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

Cleaning up .NET 9 locking using source generator. #66

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions src/ZeroLog.Benchmarks/Tools/SerilogTestSink.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using Serilog.Core;
using Serilog.Events;
using System.Collections.Generic;
using System.Threading;
using Serilog.Core;
using Serilog.Events;

namespace ZeroLog.Benchmarks.Tools;

public class SerilogTestSink : ILogEventSink
{
private readonly bool _captureLoggedMessages;
private readonly object _lock = new();
private readonly Lock _lock = new();
private int _messageCount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this!

private ManualResetEventSlim _signal;
private int _messageCountTarget;
Expand Down
7 changes: 1 addition & 6 deletions src/ZeroLog.Impl.Full/BufferSegmentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ namespace ZeroLog;

internal unsafe class BufferSegmentProvider
{
#if NET9_0_OR_GREATER
private readonly System.Threading.Lock _lock = new();
#else
private readonly object _lock = new();
#endif

private readonly Lock _lock = LockFactory.Create();
private readonly int _segmentCount;
private readonly int _segmentSize;

Expand Down
6 changes: 1 addition & 5 deletions src/ZeroLog.Impl.Full/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,7 @@ public override void Flush()

internal sealed class SyncRunner(ZeroLogConfiguration config) : Runner(config)
{
#if NET9_0_OR_GREATER
private readonly Lock _lock = new();
#else
private readonly object _lock = new();
#endif
private readonly Lock _lock = LockFactory.Create();

public override void Submit(LogMessage message)
{
Expand Down
7 changes: 1 addition & 6 deletions src/ZeroLog.Impl.Full/UnmanagedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ internal delegate bool FormatterDelegate(
ZeroLogConfiguration config
);

#if NET9_0_OR_GREATER
private static readonly System.Threading.Lock _lock = new();
#else
private static readonly object _lock = new();
#endif

private static readonly Lock _lock = LockFactory.Create();
private static readonly Dictionary<IntPtr, FormatterDelegate> _unmanagedStructs = new();
private static readonly MethodInfo _registerMethod = typeof(UnmanagedCache).GetMethod(nameof(Register), Type.EmptyTypes)!;

Expand Down
10 changes: 10 additions & 0 deletions src/ZeroLog.Impl.Full/ZeroLog.Impl.Full.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<PackageReference Include="InlineIL.Fody" Version="1.9.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Backport.System.Threading.Lock" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

<ItemGroup>
<Compile Include="../ZeroLog.Impl.Base/**/*.cs" Exclude="../ZeroLog.Impl.Base/obj/**/*;../ZeroLog.Impl.Base/bin/**/*" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/ZeroLog/ZeroLog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
<PackageReference Include="System.Memory" Version="4.6.0" Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Backport.System.Threading.Lock" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

<ItemGroup>
<Compile Include="../ZeroLog.Impl.Base/**/*.cs" Exclude="../ZeroLog.Impl.Base/obj/**/*;../ZeroLog.Impl.Base/bin/**/*" Visible="false" />
<Compile Include="../ZeroLog.Impl.Full/**/*.cs" Exclude="../ZeroLog.Impl.Full/obj/**/*;../ZeroLog.Impl.Full/bin/**/*" Visible="false" Condition="'$(TargetFramework)' != 'netstandard2.0'" />
Expand Down
Loading