Skip to content

Commit 760c3c2

Browse files
committed
Remove SpinLock extension taking an object
1 parent a045b55 commit 760c3c2

File tree

2 files changed

+5
-49
lines changed

2 files changed

+5
-49
lines changed

CommunityToolkit.HighPerformance/Extensions/SpinLockExtensions.cs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public readonly unsafe ref struct UnsafeLock
5757
/// </summary>
5858
/// <param name="spinLock">The target <see cref="SpinLock"/> to use.</param>
5959
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60-
public UnsafeLock(SpinLock* spinLock)
60+
internal UnsafeLock(SpinLock* spinLock)
6161
{
6262
this.spinLock = spinLock;
6363
this.lockTaken = false;
@@ -103,33 +103,6 @@ public static Lock Enter(ref this SpinLock spinLock)
103103
{
104104
return new(ref spinLock);
105105
}
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
133106

134107
/// <summary>
135108
/// 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 +123,18 @@ public readonly ref struct Lock
150123
/// </summary>
151124
private readonly bool lockTaken;
152125

153-
#if NETSTANDARD2_1_OR_GREATER
154126
/// <summary>
155127
/// Initializes a new instance of the <see cref="Lock"/> struct.
156128
/// </summary>
157129
/// <param name="spinLock">The target <see cref="SpinLock"/> to use.</param>
158130
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159-
public Lock(ref SpinLock spinLock)
131+
internal Lock(ref SpinLock spinLock)
160132
{
161133
this.spinLock = new Ref<SpinLock>(ref spinLock);
162134
this.lockTaken = false;
163135

164136
spinLock.Enter(ref this.lockTaken);
165137
}
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
181138

182139
/// <summary>
183140
/// Implements the duck-typed <see cref="System.IDisposable.Dispose"/> method and releases the current <see cref="SpinLock"/> instance.
@@ -191,4 +148,5 @@ public void Dispose()
191148
}
192149
}
193150
}
151+
#endif
194152
}

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)