[2020-02] Avoid following invalid pointers in mono_w32process_get_modules on Darwin...
[mono-project.git] / mono / tests / synchronized.cs
blob9d56cd8c365a121503d886399b40ec3d2a5c5081
1 //
2 // synchronized.cs:
3 //
4 // Tests for the 'synchronized' method attribute
5 //
7 using System;
8 using System.Threading;
9 using System.Runtime.CompilerServices;
11 class Tests {
13 // We use Monitor.Pulse to test that the object is synchronized
15 [MethodImplAttribute(MethodImplOptions.Synchronized)]
16 public int test () {
17 Monitor.Pulse (this);
18 //Monitor.Enter (this);
19 return 2 + 2;
22 [MethodImplAttribute(MethodImplOptions.Synchronized)]
23 public static int test_static () {
24 Monitor.Pulse (typeof (Tests));
25 return 2 + 2;
28 [MethodImplAttribute(MethodImplOptions.Synchronized)]
29 public int test_exception () {
30 Monitor.Exit (this);
31 throw new Exception ("A");
34 [MethodImplAttribute(MethodImplOptions.Synchronized)]
35 public virtual int test_virtual () {
36 Monitor.Pulse (this);
37 return 2 + 2;
40 public static bool is_synchronized (object o) {
41 try {
42 Monitor.Pulse (o);
44 catch (SynchronizationLockException ex) {
45 return false;
47 return true;
50 class Gen<T>
52 [MethodImpl(MethodImplOptions.Synchronized)]
53 public static void Run ()
58 public delegate int Delegate1 ();
60 static public int Main (String[] args) {
61 Tests b = new Tests ();
62 int res, err;
64 Console.WriteLine ("Test1...");
65 b.test ();
66 if (is_synchronized (b))
67 return 1;
69 Console.WriteLine ("Test2...");
70 test_static ();
71 if (is_synchronized (typeof (Tests)))
72 return 1;
74 Console.WriteLine ("Test3...");
75 try {
76 b.test_exception ();
78 catch (SynchronizationLockException ex) {
79 // OK
81 catch (Exception ex) {
82 // The other exception should be overwritten by the lock one
83 return 1;
85 if (is_synchronized (b))
86 return 1;
88 Console.WriteLine ("Test4...");
89 b.test_virtual ();
90 if (is_synchronized (b))
91 return 1;
93 Console.WriteLine ("Test5...");
94 Delegate1 d = new Delegate1 (b.test);
95 res = d ();
96 if (is_synchronized (b))
97 return 1;
99 Console.WriteLine ("Test6...");
100 d = new Delegate1 (test_static);
101 res = d ();
102 if (is_synchronized (typeof (Tests)))
103 return 1;
105 Console.WriteLine ("Test7...");
106 d = new Delegate1 (b.test_virtual);
107 res = d ();
108 if (is_synchronized (b))
109 return 1;
111 Console.WriteLine ("Test8...");
112 d = new Delegate1 (b.test_exception);
113 try {
114 d ();
116 catch (SynchronizationLockException ex) {
117 // OK
119 catch (Exception ex) {
120 return 2;
122 if (is_synchronized (b))
123 return 1;
125 Monitor.Enter (typeof (Gen<>));
126 Thread t = new Thread (() =>
128 Gen<object>.Run ();
130 t.Start ();
131 t.Join ();
132 Monitor.Exit (typeof (Gen<>));
134 return 0;