-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathTest_SpinLockExtensions.cs
More file actions
68 lines (56 loc) · 1.56 KB
/
Test_SpinLockExtensions.cs
File metadata and controls
68 lines (56 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#pragma warning disable CS0618
namespace CommunityToolkit.HighPerformance.UnitTests.Extensions;
[TestClass]
public class Test_SpinLockExtensions
{
[TestMethod]
public unsafe void Test_ArrayExtensions_Pointer()
{
SpinLock spinLock = default;
SpinLock* p = &spinLock;
int sum = 0;
_ = Parallel.For(0, 1000, i =>
{
for (int j = 0; j < 10; j++)
{
using (SpinLockExtensions.Enter(p))
{
sum++;
}
}
});
Assert.AreEqual(sum, 1000 * 10);
}
#if NET8_0_OR_GREATER
[TestMethod]
public void Test_ArrayExtensions_Ref()
{
SpinLockOwner? spinLockOwner = new();
int sum = 0;
_ = Parallel.For(0, 1000, i =>
{
for (int j = 0; j < 10; j++)
{
using (spinLockOwner.Lock.Enter())
{
sum++;
}
}
});
Assert.AreEqual(sum, 1000 * 10);
}
/// <summary>
/// A dummy model that owns a <see cref="SpinLock"/> object.
/// </summary>
private sealed class SpinLockOwner
{
public SpinLock Lock;
}
#endif
}