[netcore] Ongoing work. (#13391)
[mono-project.git] / mono / tests / abort-try-holes.cs
blob9f476a89fc3d709edac02f897b6025d64dac0ce1
1 using System;
2 using System.Threading;
4 public class Program
6 private static readonly ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
8 private static int abort_count = 0;
9 private const int num_iterations = 10;
11 private static int Main(string[] args)
13 for (int i = 0; i < num_iterations; i++) {
14 Console.WriteLine("Next iter");
16 Barrier barrier = new Barrier (3);
18 Thread reader = new Thread (UseLockForRead);
19 Thread writer = new Thread (UseLockForWrite);
20 reader.Start (barrier);
21 writer.Start (barrier);
23 barrier.SignalAndWait ();
25 writer.Abort();
26 reader.Abort();
28 reader.Join();
29 writer.Join();
32 if (abort_count != (num_iterations * 2)) {
33 Console.WriteLine ("Only {0} aborts", abort_count);
34 return 1;
36 return 0;
39 private static void UseLockForRead (object barrier)
41 bool locked = false;
42 try {
43 ((Barrier)barrier).SignalAndWait ();
44 for (;;) {
45 try {
46 try {}
47 finally {
48 rwl.EnterReadLock();
49 locked=true;
52 finally {
53 if (locked) {
54 rwl.ExitReadLock();
55 locked=false;
59 } catch (ThreadAbortException) {
60 Interlocked.Increment (ref abort_count);
64 private static void UseLockForWrite (object barrier)
66 bool locked = false;
68 try {
69 ((Barrier)barrier).SignalAndWait ();
70 for (;;) {
71 try {
72 try {}
73 finally {
74 rwl.EnterWriteLock();
75 locked = true;
77 } finally {
78 if (locked)
80 rwl.ExitWriteLock();
81 locked=false;
85 } catch (ThreadAbortException) {
86 Interlocked.Increment (ref abort_count);