Merge pull request #704 from jgagnon/master
[mono.git] / mcs / class / corlib / Test / System.Threading / SpinLockTests.cs
1 //
2 // SpinLockTests.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
6 //
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 using System;
28 using System.Threading;
29
30 using NUnit.Framework;
31
32 using MonoTests.System.Threading.Tasks;
33
34 namespace MonoTests.System.Threading
35 {
36         [TestFixture]
37         public class SpinLockTests
38         {
39                 SpinLock sl;
40
41                 [SetUp]
42                 public void Setup ()
43                 {
44                         sl = new SpinLock (true);
45                 }
46
47                 [Test, ExpectedException (typeof (LockRecursionException))]
48                 public void RecursionExceptionTest ()
49                 {
50                         sl = new SpinLock (true);
51                         bool taken = false, taken2 = false;
52
53                         sl.Enter (ref taken);
54                         Assert.IsTrue (taken, "#1");
55                         sl.Enter (ref taken2);
56                 }
57
58                 [Test]
59                 public void SimpleEnterExitSchemeTest ()
60                 {
61                         bool taken = false;
62
63                         for (int i = 0; i < 50000; i++) {
64                                 sl.Enter (ref taken);
65                                 Assert.IsTrue (taken, "#" + i.ToString ());
66                                 sl.Exit ();
67                                 taken = false;
68                         }
69                 }
70
71                 [Test]
72                 public void SemanticCorrectnessTest ()
73                 {
74                         sl = new SpinLock (false);
75
76                         bool taken = false;
77                         bool taken2 = false;
78
79                         sl.Enter (ref taken);
80                         Assert.IsTrue (taken, "#1");
81                         sl.TryEnter (ref taken2);
82                         Assert.IsFalse (taken2, "#2");
83                         sl.Exit ();
84
85                         sl.TryEnter (ref taken2);
86                         Assert.IsTrue (taken2, "#3");
87
88                         sl.Exit ();
89                         Assert.IsFalse (sl.IsHeld);
90                 }
91
92                 [Test, ExpectedException (typeof (ArgumentException))]
93                 public void FirstTakenParameterTest ()
94                 {
95                         bool taken = true;
96
97                         sl.Enter (ref taken);
98                 }
99
100                 [Test, ExpectedException (typeof (ArgumentException))]
101                 public void SecondTakenParameterTest ()
102                 {
103                         bool taken = true;
104
105                         sl.TryEnter (ref taken);
106                 }
107
108                 internal class SpinLockWrapper
109                 {
110                         public SpinLock Lock = new SpinLock (false);
111                 }
112
113                 [Test]
114                 public void LockUnicityTest ()
115                 {
116                         ParallelTestHelper.Repeat (delegate {
117                                 int currentCount = 0;
118                                 bool fail = false;
119                                 SpinLockWrapper wrapper = new SpinLockWrapper ();
120
121                                 ParallelTestHelper.ParallelStressTest (wrapper, delegate {
122                                         bool taken = false;
123                                         wrapper.Lock.Enter (ref taken);
124                                         int current = currentCount++;
125                                         if (current != 0)
126                                                 fail = true;
127
128                                         SpinWait sw = new SpinWait ();
129                                         for (int i = 0; i < 200; i++)
130                                                 sw.SpinOnce ();
131                                         currentCount -= 1;
132
133                                         wrapper.Lock.Exit ();
134                                 }, 4);
135
136                                 Assert.IsFalse (fail);
137                         }, 200);
138                 }
139
140                 [Test]
141                 public void IsHeldByCurrentThreadTest ()
142                 {
143                         bool lockTaken = false;
144
145                         sl.Enter (ref lockTaken);
146                         Assert.IsTrue (lockTaken, "#1");
147                         Assert.IsTrue (sl.IsHeldByCurrentThread, "#2");
148
149                         lockTaken = false;
150                         sl = new SpinLock (true);
151
152                         sl.Enter (ref lockTaken);
153                         Assert.IsTrue (lockTaken, "#3");
154                         Assert.IsTrue (sl.IsHeldByCurrentThread, "#4");
155                 }
156         }
157 }