Revert some changes which don't have proper dependencies.
[mono-project.git] / mono / tests / spinlock-stress.cs
blobc9be92cb9e19eee50c22bf0994339d6c8f44cebc
1 using System;
2 using System.Threading;
4 internal class SpinLockWrapper
6 public SpinLock Lock = new SpinLock (false);
9 public class Tests
11 public static void Main (string[] args)
13 int iterations = 200;
14 if (args.Length > 0)
15 iterations = Int32.Parse (args [0]);
17 ParallelTestHelper.Repeat (delegate {
18 int currentCount = 0;
19 bool fail = false;
20 SpinLockWrapper wrapper = new SpinLockWrapper ();
22 ParallelTestHelper.ParallelStressTest (wrapper, delegate {
23 bool taken = false;
24 wrapper.Lock.Enter (ref taken);
25 int current = currentCount++;
26 if (current != 0)
27 fail = true;
29 SpinWait sw = new SpinWait ();
30 for (int i = 0; i < 200; i++)
31 sw.SpinOnce ();
32 currentCount -= 1;
34 wrapper.Lock.Exit ();
35 }, 4);
37 if (fail)
38 Environment.Exit (1);
39 }, iterations);
40 Environment.Exit (0);
44 static class ParallelTestHelper
46 public static void Repeat (Action action, int numRun)
48 for (int i = 0; i < numRun; i++) {
49 //Console.WriteLine ("Run " + i.ToString ());
50 action ();
54 public static void ParallelStressTest<TSource>(TSource obj, Action<TSource> action, int numThread)
56 Thread[] threads = new Thread[numThread];
57 for (int i = 0; i < numThread; i++) {
58 threads[i] = new Thread(new ThreadStart(delegate { action(obj); }));
59 threads[i].Start();
62 // Wait for the completion
63 for (int i = 0; i < numThread; i++)
64 threads[i].Join();