Skip to content

Commit

Permalink
Use .NET 9 System.Threading.Lock. (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds authored Dec 18, 2024
1 parent f0b2c29 commit 4567301
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Install .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"
dotnet-version: "9.0.x"

- name: Install reportgenerator tool
run: dotnet tool install --global dotnet-reportgenerator-globaltool
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Protocol/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum ConnectionState
Disconnected
}

private readonly object _gate = new object();
private readonly Lock _gate = new();
private readonly ClientConnectionOptions _connectionOptions;
private DBusConnection? _connection;
private CancellationTokenSource? _connectCts;
Expand Down Expand Up @@ -97,7 +97,7 @@ private ValueTask<DBusConnection> ConnectCoreAsync(bool explicitConnect = false)

private async Task<DBusConnection> DoConnectAsync()
{
Debug.Assert(Monitor.IsEntered(_gate));
Debug.Assert(_gate.IsHeldByCurrentThread);

DBusConnection? connection = null;
try
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Protocol/DBusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void Invoke(Exception? exception, Message message)
private readonly object? _state4;
}

private readonly object _gate = new object();
private readonly Lock _gate = new();
private readonly Connection _parentConnection;
private readonly Dictionary<uint, MessageHandler> _pendingCalls;
private readonly CancellationTokenSource _connectCts;
Expand Down Expand Up @@ -903,7 +903,7 @@ MessageBuffer CreateAddMatchMessage(string ruleString)

sealed class Observer : IDisposable
{
private readonly object _gate = new object();
private readonly Lock _gate = new();
private readonly SynchronizationContext? _synchronizationContext;
private readonly MatchMaker _matchMaker;
private readonly MessageHandler4 _messageHandler;
Expand Down
5 changes: 3 additions & 2 deletions src/Tmds.DBus.Protocol/MessageBufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class MessageBufferPool
public static readonly MessageBufferPool Shared = new MessageBufferPool(Environment.ProcessorCount * 2);

private readonly int _maxSize;
private readonly Lock _gate = new();
private readonly Stack<MessageBuffer> _pool = new Stack<MessageBuffer>();

internal MessageBufferPool(int maxSize)
Expand All @@ -16,7 +17,7 @@ internal MessageBufferPool(int maxSize)

public MessageBuffer Rent()
{
lock (_pool)
lock (_gate)
{
if (_pool.Count > 0)
{
Expand All @@ -31,7 +32,7 @@ public MessageBuffer Rent()

internal void Return(MessageBuffer value)
{
lock (_pool)
lock (_gate)
{
if (_pool.Count < _maxSize)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Tmds.DBus.Protocol/Polyfill/Lock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if !NET9_0_OR_GREATER

namespace Tmds.DBus.Protocol;

// Polyfill for System.Threading.Lock for use with the `lock` keyword.
sealed class Lock
{
public bool IsHeldByCurrentThread => Monitor.IsEntered(this);
}

#endif
4 changes: 2 additions & 2 deletions src/Tmds.DBus.Protocol/Tmds.DBus.Protocol.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>12</LangVersion>
<LangVersion>13</LangVersion>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

Expand Down
17 changes: 8 additions & 9 deletions src/Tmds.DBus.Protocol/UnixFdCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ sealed class UnixFdCollection : IReadOnlyList<SafeHandle>, IDisposable

// The gate guards someone removing handles while the UnixFdCollection gets disposed by the message.
// We don't need to lock it while adding handles, or reading them to send them.
private readonly object _gate;
private bool _disposed;
private bool _handlesReffed;

internal object SyncObject => _gate;
internal object SyncObject { get; }

internal bool IsRawHandleCollection => _rawHandles is not null;

internal UnixFdCollection(bool isRawHandleCollection = true)
{
if (isRawHandleCollection)
{
_gate = _rawHandles = new();
SyncObject = _rawHandles = new();
}
else
{
_gate = _handles = new();
SyncObject = _handles = new();
}
}

Expand Down Expand Up @@ -55,7 +54,7 @@ internal void AddHandle(SafeHandle handle)
// We remain responsible for disposing the handle.
public IntPtr ReadHandleRaw(int index)
{
lock (_gate)
lock (SyncObject)
{
if (_disposed)
{
Expand Down Expand Up @@ -103,7 +102,7 @@ private void ThrowDisposed()
// The caller of this method owns the handle and is responsible for Disposing it.
internal T? ReadHandleGeneric<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>(int index)
{
lock (_gate)
lock (SyncObject)
{
if (_disposed)
{
Expand Down Expand Up @@ -155,7 +154,7 @@ IEnumerator IEnumerable.GetEnumerator()

public void Dispose()
{
lock (_gate)
lock (SyncObject)
{
if (_disposed)
{
Expand All @@ -171,7 +170,7 @@ public void Dispose()

internal void RefHandles()
{
lock (_gate)
lock (SyncObject)
{
if (_disposed)
{
Expand Down Expand Up @@ -206,7 +205,7 @@ internal void RefHandles()

internal int DangerousGetHandle(int i)
{
Debug.Assert(Monitor.IsEntered(_gate));
Debug.Assert(Monitor.IsEntered(SyncObject));
if (!_handlesReffed)
{
throw new InvalidOperationException("Trying to send an unreffed handle.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand Down
2 changes: 1 addition & 1 deletion test/Tmds.DBus.Tests/Tmds.DBus.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\sign.snk</AssemblyOriginatorKeyFile>
<PublicSign>true</PublicSign>
Expand Down

0 comments on commit 4567301

Please sign in to comment.