Skip to content

Commit

Permalink
Use some net9.0 features
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Nov 17, 2024
1 parent 2547704 commit e80fb57
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/ZeroLog.Impl.Base/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public sealed partial class LogManager
/// </remarks>
/// <typeparam name="T">The type.</typeparam>
public static Log GetLogger<T>()
#if NET9_0_OR_GREATER
where T : allows ref struct
#endif
=> GetLogger(typeof(T));

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/ZeroLog.Impl.Full/BufferSegmentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ 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 int _segmentCount;
private readonly int _segmentSize;
Expand Down
4 changes: 4 additions & 0 deletions src/ZeroLog.Impl.Full/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ 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

public override void Submit(LogMessage message)
{
Expand Down
10 changes: 8 additions & 2 deletions src/ZeroLog.Impl.Full/UnmanagedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ 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 Dictionary<IntPtr, FormatterDelegate> _unmanagedStructs = new();
private static readonly MethodInfo _registerMethod = typeof(UnmanagedCache).GetMethod(nameof(Register), Type.EmptyTypes)!;

Expand All @@ -52,7 +58,7 @@ public static void Register<T>(UnmanagedFormatterDelegate<T> formatter)
{
ArgumentNullException.ThrowIfNull(formatter);

lock (_unmanagedStructs)
lock (_lock)
{
_unmanagedStructs[typeof(T).TypeHandle.Value] = (byte* valuePtr, Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, ZeroLogConfiguration _)
=> FormatterGeneric(valuePtr, destination, out charsWritten, format, formatter);
Expand Down Expand Up @@ -109,7 +115,7 @@ private static bool FormatterGenericNullable<T>(byte* valuePtr,
public static bool TryGetFormatter(IntPtr typeHandle, out FormatterDelegate formatter)
{
// This is accessed from a single thread, there should be no contention
lock (_unmanagedStructs)
lock (_lock)
{
return _unmanagedStructs.TryGetValue(typeHandle, out formatter!);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZeroLog.Tests/PerformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void should_run_test()
var signal = _testAppender.SetMessageCountTarget(totalMessageCount);
var utcNow = DateTime.UtcNow;

Parallel.For(0, threadCount, i =>
Parallel.For(0, threadCount, _ =>
{
for (var k = 0; k < threadMessageCount; k++)
{
Expand Down
3 changes: 3 additions & 0 deletions src/ZeroLog.Tests/Support/AssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace ZeroLog.Tests.Support;

#nullable enable

#if NET6_0_OR_GREATER
[System.Diagnostics.StackTraceHidden]
#endif
internal static class AssertExtensions
{
public static void ShouldEqual<T>(this T? actual, T? expected)
Expand Down

0 comments on commit e80fb57

Please sign in to comment.