1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Microsoft.Xunit.Performance;
6 
7 namespace System.Threading.Tests
8 {
9     public class Perf_SpinLock
10     {
11         [Benchmark(InnerIterationCount = 100)]
EnterExit()12         public void EnterExit()
13         {
14             SpinLock spinLock = new SpinLock();
15 
16             foreach (var iteration in Benchmark.Iterations)
17             {
18                 using (iteration.StartMeasurement())
19                 {
20                     for (int i = 0; i < Benchmark.InnerIterationCount; i++)
21                     {
22                         bool lockTaken = false;
23 
24                         spinLock.Enter(ref lockTaken);
25                         spinLock.Exit();
26                     }
27                 }
28             }
29         }
30 
31         [Benchmark(InnerIterationCount = 100)]
TryEnterExit()32         public void TryEnterExit()
33         {
34             SpinLock spinLock = new SpinLock();
35 
36             foreach (var iteration in Benchmark.Iterations)
37             {
38                 using (iteration.StartMeasurement())
39                 {
40                     for (int i = 0; i < Benchmark.InnerIterationCount; i++)
41                     {
42                         bool lockTaken = false;
43 
44                         spinLock.TryEnter(0, ref lockTaken);
45                         spinLock.Exit();
46                     }
47                 }
48             }
49         }
50     }
51 }
52