Skip to content

Commit 3b3c8b1

Browse files
authored
Merge pull request #183 from CommunityToolkit/dev/remove-spinlock-object-extension
Remove SpinLock extension taking an object
2 parents 0e2a94c + 40d06fa commit 3b3c8b1

File tree

2 files changed

+8
-50
lines changed

2 files changed

+8
-50
lines changed

CommunityToolkit.HighPerformance/Extensions/SpinLockExtensions.cs

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using System.ComponentModel;
66
using System.Runtime.CompilerServices;
7+
#if NETSTANDARD2_1_OR_GREATER
78
using System.Runtime.Versioning;
9+
#endif
810
using System.Threading;
911

1012
namespace CommunityToolkit.HighPerformance;
@@ -48,7 +50,7 @@ public readonly unsafe ref struct UnsafeLock
4850
private readonly SpinLock* spinLock;
4951

5052
/// <summary>
51-
/// A value indicating whether or not the lock is taken by this <see cref="Lock"/> instance.
53+
/// A value indicating whether or not the lock is taken by this <see cref="UnsafeLock"/> instance.
5254
/// </summary>
5355
private readonly bool lockTaken;
5456

@@ -57,7 +59,7 @@ public readonly unsafe ref struct UnsafeLock
5759
/// </summary>
5860
/// <param name="spinLock">The target <see cref="SpinLock"/> to use.</param>
5961
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60-
public UnsafeLock(SpinLock* spinLock)
62+
internal UnsafeLock(SpinLock* spinLock)
6163
{
6264
this.spinLock = spinLock;
6365
this.lockTaken = false;
@@ -103,33 +105,6 @@ public static Lock Enter(ref this SpinLock spinLock)
103105
{
104106
return new(ref spinLock);
105107
}
106-
#else
107-
/// <summary>
108-
/// Enters a specified <see cref="SpinLock"/> instance and returns a wrapper to use to release the lock.
109-
/// This extension should be used though a <see langword="using"/> block or statement:
110-
/// <code>
111-
/// private SpinLock spinLock = new SpinLock();
112-
///
113-
/// public void Foo()
114-
/// {
115-
/// using (SpinLockExtensions.Enter(this, ref spinLock))
116-
/// {
117-
/// // Thread-safe code here...
118-
/// }
119-
/// }
120-
/// </code>
121-
/// The compiler will take care of releasing the SpinLock when the code goes out of that <see langword="using"/> scope.
122-
/// </summary>
123-
/// <param name="owner">The owner <see cref="object"/> to create a portable reference for.</param>
124-
/// <param name="spinLock">The target <see cref="SpinLock"/> to use (it must be within <paramref name="owner"/>).</param>
125-
/// <returns>A wrapper type that will release <paramref name="spinLock"/> when its <see cref="System.IDisposable.Dispose"/> method is called.</returns>
126-
/// <remarks>The returned <see cref="Lock"/> value shouldn't be used directly: use this extension in a <see langword="using"/> block or statement.</remarks>
127-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
128-
public static Lock Enter(object owner, ref SpinLock spinLock)
129-
{
130-
return new(owner, ref spinLock);
131-
}
132-
#endif
133108

134109
/// <summary>
135110
/// A <see langword="struct"/> that is used to enter and hold a <see cref="SpinLock"/> through a <see langword="using"/> block or statement.
@@ -150,34 +125,18 @@ public readonly ref struct Lock
150125
/// </summary>
151126
private readonly bool lockTaken;
152127

153-
#if NETSTANDARD2_1_OR_GREATER
154128
/// <summary>
155129
/// Initializes a new instance of the <see cref="Lock"/> struct.
156130
/// </summary>
157131
/// <param name="spinLock">The target <see cref="SpinLock"/> to use.</param>
158132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159-
public Lock(ref SpinLock spinLock)
133+
internal Lock(ref SpinLock spinLock)
160134
{
161135
this.spinLock = new Ref<SpinLock>(ref spinLock);
162136
this.lockTaken = false;
163137

164138
spinLock.Enter(ref this.lockTaken);
165139
}
166-
#else
167-
/// <summary>
168-
/// Initializes a new instance of the <see cref="Lock"/> struct.
169-
/// </summary>
170-
/// <param name="owner">The owner <see cref="object"/> to create a portable reference for.</param>
171-
/// <param name="spinLock">The target <see cref="SpinLock"/> to use (it must be within <paramref name="owner"/>).</param>
172-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
173-
public Lock(object owner, ref SpinLock spinLock)
174-
{
175-
this.spinLock = new Ref<SpinLock>(owner, ref spinLock);
176-
this.lockTaken = false;
177-
178-
spinLock.Enter(ref this.lockTaken);
179-
}
180-
#endif
181140

182141
/// <summary>
183142
/// Implements the duck-typed <see cref="System.IDisposable.Dispose"/> method and releases the current <see cref="SpinLock"/> instance.
@@ -191,4 +150,5 @@ public void Dispose()
191150
}
192151
}
193152
}
153+
#endif
194154
}

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_SpinLockExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public unsafe void Test_ArrayExtensions_Pointer()
3333
Assert.AreEqual(sum, 1000 * 10);
3434
}
3535

36+
#if !NETFRAMEWORK
3637
[TestMethod]
3738
public void Test_ArrayExtensions_Ref()
3839
{
@@ -44,11 +45,7 @@ public void Test_ArrayExtensions_Ref()
4445
{
4546
for (int j = 0; j < 10; j++)
4647
{
47-
#if NETFRAMEWORK
48-
using (SpinLockExtensions.Enter(spinLockOwner, ref spinLockOwner.Lock))
49-
#else
5048
using (spinLockOwner.Lock.Enter())
51-
#endif
5249
{
5350
sum++;
5451
}
@@ -57,6 +54,7 @@ public void Test_ArrayExtensions_Ref()
5754

5855
Assert.AreEqual(sum, 1000 * 10);
5956
}
57+
#endif
6058

6159
/// <summary>
6260
/// A dummy model that owns a <see cref="SpinLock"/> object.

0 commit comments

Comments
 (0)