Skip to content

Commit 29b62b2

Browse files
Dropped dependency.
1 parent b53aaa6 commit 29b62b2

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="$(OpenTelemetryIntergationPackageVersion)" />
5858

5959
<!-- Other -->
60-
<PackageVersion Include="Backport.System.Threading.Lock" Version="1.1.1" />
6160
<PackageVersion Include="BenchmarkDotNet" Version="0.13.1" />
6261
<PackageVersion Include="CommandLineParser" Version="2.5.0" />
6362
<PackageVersion Include="Google.Api.CommonProtos" Version="2.15.0" />

src/Grpc.HealthCheck/Grpc.HealthCheck.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0'">
2828
<PackageReference Include="System.Threading.Channels" />
29-
<PackageReference Include="Backport.System.Threading.Lock" />
29+
<Compile Include="..\Shared\Lock.cs" Link="Internal\Lock.cs" />
3030
</ItemGroup>
3131

3232
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
@@ -35,7 +35,7 @@
3535
</ItemGroup>
3636

3737
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
38-
<PackageReference Include="Backport.System.Threading.Lock" />
38+
<Compile Include="..\Shared\Lock.cs" Link="Internal\Lock.cs" />
3939
</ItemGroup>
4040

4141
</Project>

src/Grpc.Net.Common/Grpc.Net.Common.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
2020
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
21-
<PackageReference Include="Backport.System.Threading.Lock" />
21+
<Compile Include="..\Shared\Lock.cs" Link="Internal\Lock.cs" />
2222
</ItemGroup>
2323

2424
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
25-
<PackageReference Include="Backport.System.Threading.Lock" />
25+
<Compile Include="..\Shared\Lock.cs" Link="Internal\Lock.cs" />
2626
</ItemGroup>
2727

2828
</Project>

src/Shared/Lock.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Credit: Mark Cilia Vincenti, 2024
2+
// Taken from: https://github.com/MarkCiliaVincenti/Backport.System.Threading.Lock
3+
// NuGet package: https://www.nuget.org/packages/Backport.System.Threading.Lock
4+
5+
using System.Runtime.CompilerServices;
6+
#if NET9_0_OR_GREATER
7+
[assembly: TypeForwardedTo(typeof(System.Threading.Lock))]
8+
#else
9+
namespace System.Threading;
10+
/// <summary>
11+
/// A backport of .NET 9.0+'s System.Threading.Lock.
12+
/// </summary>
13+
public sealed class Lock
14+
{
15+
#pragma warning disable CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement.
16+
/// <summary>
17+
/// <inheritdoc cref="Monitor.Enter(object)"/>
18+
/// </summary>
19+
/// <exception cref="ArgumentNullException"/>
20+
#if !PARTIAL_SUPPORT
21+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22+
#endif
23+
public void Enter() => Monitor.Enter(this);
24+
25+
/// <summary>
26+
/// <inheritdoc cref="Monitor.TryEnter(object)"/>
27+
/// </summary>
28+
/// <returns>
29+
/// <inheritdoc cref="Monitor.TryEnter(object)"/>
30+
/// </returns>
31+
/// <exception cref="ArgumentNullException"/>
32+
#if !PARTIAL_SUPPORT
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
#endif
35+
public bool TryEnter() => Monitor.TryEnter(this);
36+
37+
/// <summary>
38+
/// <inheritdoc cref="Monitor.TryEnter(object, TimeSpan)"/>
39+
/// </summary>
40+
/// <returns>
41+
/// <inheritdoc cref="Monitor.TryEnter(object, TimeSpan)"/>
42+
/// </returns>
43+
/// <exception cref="ArgumentNullException"/>
44+
/// <exception cref="ArgumentOutOfRangeException"/>
45+
#if !PARTIAL_SUPPORT
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
#endif
48+
public bool TryEnter(TimeSpan timeout) => Monitor.TryEnter(this, timeout);
49+
50+
/// <summary>
51+
/// <inheritdoc cref="Monitor.TryEnter(object, int)"/>
52+
/// </summary>
53+
/// <returns>
54+
/// <inheritdoc cref="Monitor.TryEnter(object, int)"/>
55+
/// </returns>
56+
/// <exception cref="ArgumentNullException"/>
57+
/// <exception cref="ArgumentOutOfRangeException"/>
58+
#if !PARTIAL_SUPPORT
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
#endif
61+
public bool TryEnter(int millisecondsTimeout) => Monitor.TryEnter(this, millisecondsTimeout);
62+
63+
/// <summary>
64+
/// <inheritdoc cref="Monitor.Exit(object)"/>
65+
/// </summary>
66+
/// <exception cref="ArgumentNullException"/>
67+
/// <exception cref="SynchronizationLockException"/>
68+
#if !PARTIAL_SUPPORT
69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70+
#endif
71+
public void Exit() => Monitor.Exit(this);
72+
73+
/// <summary>
74+
/// Determines whether the current thread holds this lock.
75+
/// </summary>
76+
/// <returns>
77+
/// true if the current thread holds this lock; otherwise, false.
78+
/// </returns>
79+
/// <exception cref="ArgumentNullException"/>
80+
#if !PARTIAL_SUPPORT
81+
public bool IsHeldByCurrentThread => Monitor.IsEntered(this);
82+
#else
83+
public bool IsHeldByCurrentThread => throw new NotSupportedException("IsHeldByCurrentThread is only supported on .NET Framework 4.5 or greater.");
84+
#endif
85+
#pragma warning restore CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement.
86+
87+
/// <summary>
88+
/// Enters the lock and returns a <see cref="Scope"/> that may be disposed to exit the lock. Once the method returns,
89+
/// the calling thread would be the only thread that holds the lock. This method is intended to be used along with a
90+
/// language construct that would automatically dispose the <see cref="Scope"/>, such as with the C# <code>using</code>
91+
/// statement.
92+
/// </summary>
93+
/// <returns>
94+
/// A <see cref="Scope"/> that may be disposed to exit the lock.
95+
/// </returns>
96+
/// <remarks>
97+
/// If the lock cannot be entered immediately, the calling thread waits for the lock to be exited. If the lock is
98+
/// already held by the calling thread, the lock is entered again. The calling thread should exit the lock, such as by
99+
/// disposing the returned <see cref="Scope"/>, as many times as it had entered the lock to fully exit the lock and
100+
/// allow other threads to enter the lock.
101+
/// </remarks>
102+
#if !PARTIAL_SUPPORT
103+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104+
#endif
105+
public Scope EnterScope()
106+
{
107+
Enter();
108+
return new Scope(this);
109+
}
110+
111+
/// <summary>
112+
/// A disposable structure that is returned by <see cref="EnterScope()"/>, which when disposed, exits the lock.
113+
/// </summary>
114+
public ref struct Scope(Lock @lock)
115+
{
116+
/// <summary>
117+
/// Exits the lock.
118+
/// </summary>
119+
/// <exception cref="SynchronizationLockException"/>
120+
#if !PARTIAL_SUPPORT
121+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
122+
#endif
123+
public readonly void Dispose() => @lock.Exit();
124+
}
125+
}
126+
#endif

0 commit comments

Comments
 (0)