5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 using System.Threading;
30 using NUnit.Framework;
32 using MonoTests.System.Threading.Tasks;
34 namespace MonoTests.System.Threading
37 public class SpinLockTests
44 sl = new SpinLock (true);
47 [Test, ExpectedException (typeof (LockRecursionException))]
48 public void RecursionExceptionTest ()
50 sl = new SpinLock (true);
51 bool taken = false, taken2 = false;
54 Assert.IsTrue (taken, "#1");
55 sl.Enter (ref taken2);
59 public void SimpleEnterExitSchemeTest ()
63 for (int i = 0; i < 50000; i++) {
65 Assert.IsTrue (taken, "#" + i.ToString ());
72 public void SemanticCorrectnessTest ()
74 sl = new SpinLock (false);
80 Assert.IsTrue (taken, "#1");
81 sl.TryEnter (ref taken2);
82 Assert.IsFalse (taken2, "#2");
85 sl.TryEnter (ref taken2);
86 Assert.IsTrue (taken2, "#3");
89 Assert.IsFalse (sl.IsHeld);
92 [Test, ExpectedException (typeof (ArgumentException))]
93 public void FirstTakenParameterTest ()
100 [Test, ExpectedException (typeof (ArgumentException))]
101 public void SecondTakenParameterTest ()
105 sl.TryEnter (ref taken);
108 internal class SpinLockWrapper
110 public SpinLock Lock = new SpinLock (false);
114 public void LockUnicityTest ()
116 ParallelTestHelper.Repeat (delegate {
117 int currentCount = 0;
119 SpinLockWrapper wrapper = new SpinLockWrapper ();
121 ParallelTestHelper.ParallelStressTest (wrapper, delegate {
123 wrapper.Lock.Enter (ref taken);
124 int current = currentCount++;
128 SpinWait sw = new SpinWait ();
129 for (int i = 0; i < 200; i++)
133 wrapper.Lock.Exit ();
136 Assert.IsFalse (fail);
141 public void IsHeldByCurrentThreadTest ()
143 bool lockTaken = false;
145 sl.Enter (ref lockTaken);
146 Assert.IsTrue (lockTaken, "#1");
147 Assert.IsTrue (sl.IsHeldByCurrentThread, "#2");
150 sl = new SpinLock (true);
152 sl.Enter (ref lockTaken);
153 Assert.IsTrue (lockTaken, "#3");
154 Assert.IsTrue (sl.IsHeldByCurrentThread, "#4");