Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / monitor.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Threading;
6
7 public class Tests  {
8         static void Main ()
9         {
10                 TestDriver.RunTests (typeof (Tests));
11         }
12
13         // Check that try-catch clauses are not enlarged to encompass a Monitor.Enter
14         public static int test_0_enter_catch_clause () {
15                 try {
16                         Monitor.Enter (null);
17                         try {
18                                 Console.WriteLine ();
19                         } catch (Exception ex) {
20                                 return 1;
21                         }
22                 } catch (Exception ex) {
23                         return 0;
24                 }
25                 return 1;
26         }
27
28         const int thread_count = 3;
29
30         // #651546
31         public static int test_0_enter_abort_race () {
32                 AppDomain ad = AppDomain.CreateDomain ("foo");
33                 Thread t = new Thread (StartAppDomain);
34                 t.Start (ad);
35                 Thread.Sleep (thread_count * 100 * 2);
36                 // This will abort the threads created by StartAppDomain
37                 AppDomain.Unload (ad);
38                 return 0;
39         }
40         
41         static void StartAppDomain (object dummy)
42         {
43                 ((AppDomain) dummy).DoCallBack (Main2);
44         }
45
46         static void Main2 ()
47         {
48                 Thread[] t = new Thread [thread_count];
49                 for (int i = 0; i < t.Length; i++) {
50                         t[i] = new Thread (LockMe);
51                         t[i].Start (i);
52                         Thread.Sleep (100); // this is just so that gdb's [New Thread ...] message are properly coupled with our "Thread # entered" messages
53                 }
54                 Thread.Sleep ((int) (thread_count * 100 * 1.5));
55         }
56
57         static object the_lock = new object ();
58
59         static void LockMe (object thread_id)
60         {
61                 bool unlocked = false;
62                 try {
63                         Monitor.Enter (the_lock);
64                         try {
65                                 Thread.Sleep (thread_count * 1000);
66                         } finally {
67                                 unlocked = true;
68                                 Monitor.Exit (the_lock);
69                         }
70                 
71                 } catch (Exception ex) {
72                         if (!unlocked) {
73                         }
74                 } finally {
75                 }
76         }
77 }