4 // Tests for the 'synchronized' method attribute
8 using System.Threading;
9 using System.Runtime.CompilerServices;
13 // We use Monitor.Pulse to test that the object is synchronized
15 [MethodImplAttribute(MethodImplOptions.Synchronized)]
18 //Monitor.Enter (this);
22 [MethodImplAttribute(MethodImplOptions.Synchronized)]
23 public static int test_static () {
24 Monitor.Pulse (typeof (Tests));
28 [MethodImplAttribute(MethodImplOptions.Synchronized)]
29 public int test_exception () {
31 throw new Exception ("A");
34 [MethodImplAttribute(MethodImplOptions.Synchronized)]
35 public virtual int test_virtual () {
40 public static bool is_synchronized (object o) {
44 catch (SynchronizationLockException ex) {
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 ();
64 Console.WriteLine ("Test1...");
66 if (is_synchronized (b))
69 Console.WriteLine ("Test2...");
71 if (is_synchronized (typeof (Tests)))
74 Console.WriteLine ("Test3...");
78 catch (SynchronizationLockException ex) {
81 catch (Exception ex) {
82 // The other exception should be overwritten by the lock one
85 if (is_synchronized (b))
88 Console.WriteLine ("Test4...");
90 if (is_synchronized (b))
93 Console.WriteLine ("Test5...");
94 Delegate1 d = new Delegate1 (b.test);
96 if (is_synchronized (b))
99 Console.WriteLine ("Test6...");
100 d = new Delegate1 (test_static);
102 if (is_synchronized (typeof (Tests)))
105 Console.WriteLine ("Test7...");
106 d = new Delegate1 (b.test_virtual);
108 if (is_synchronized (b))
111 Console.WriteLine ("Test8...");
112 d = new Delegate1 (b.test_exception);
116 catch (SynchronizationLockException ex) {
119 catch (Exception ex) {
122 if (is_synchronized (b))
125 Monitor.Enter (typeof (Gen<>));
126 Thread t = new Thread (() =>
132 Monitor.Exit (typeof (Gen<>));