Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / shared-generic-synchronized.2.cs
1 //
2 //  shared-generic-synchronized.2.cs:
3 //
4 //    Tests for the 'synchronized' method attribute in shared generic methods
5 //
6
7 using System;
8 using System.Threading;
9 using System.Runtime.CompilerServices;
10
11 public class Test<T> {
12
13         [MethodImplAttribute(MethodImplOptions.Synchronized)]
14         public int test () {
15                 Monitor.Exit (this);
16                 Monitor.Enter (this);
17                 return 2 + 2;
18         }
19
20         [MethodImplAttribute(MethodImplOptions.Synchronized)]
21         public static int test_static () {
22                 Monitor.Exit (typeof (Test<T>));
23                 Monitor.Enter (typeof (Test<T>));
24                 return 2 + 2;
25         }
26
27         [MethodImplAttribute(MethodImplOptions.Synchronized)]
28         public int test_exception () {
29                 Monitor.Exit (this);
30                 throw new Exception ("A");
31         }
32
33         [MethodImplAttribute(MethodImplOptions.Synchronized)]
34         public virtual int test_virtual () {
35                 Monitor.Exit (this);
36                 Monitor.Enter (this);
37                 return 2 + 2;
38         }
39 }
40
41 class main {
42         public delegate int Delegate1 ();
43
44         static public int Main (String[] args) {
45                 Test<string> b = new Test<string> ();
46                 int res;
47
48                 Console.WriteLine ("Test1...");
49                 b.test ();
50                 Console.WriteLine ("Test2...");
51                 Test<string>.test_static ();
52                 Console.WriteLine ("Test3...");
53                 try {
54                         b.test_exception ();
55                 }
56                 catch (SynchronizationLockException ex) {
57                         // OK
58                 }
59                 catch (Exception ex) {
60                         // The other exception should be overwritten by the lock one
61                         return 1;
62                 }
63
64                 Console.WriteLine ("Test4...");
65                 b.test_virtual ();
66
67                 Console.WriteLine ("Test5...");
68                 Delegate1 d = new Delegate1 (b.test);
69                 res = d ();
70
71                 Console.WriteLine ("Test6...");
72                 d = new Delegate1 (Test<string>.test_static);
73                 res = d ();
74
75                 Console.WriteLine ("Test7...");
76                 d = new Delegate1 (b.test_virtual);
77                 res = d ();
78
79                 Console.WriteLine ("Test8...");
80                 d = new Delegate1 (b.test_exception);
81                 try {
82                         d ();
83                 }
84                 catch (SynchronizationLockException ex) {
85                         // OK
86                 }
87                 catch (Exception ex) {
88                         return 2;
89                 }
90
91                 return 0;
92         }
93 }