[interp] disable assemblyresolve_event6.exe
[mono.git] / mono / tests / monitor-resurrection.cs
1 using System; 
2 using System.Collections; 
3 using System.Threading;
4 using System.Collections.Generic;
5
6 public class Foo  {
7         static Foo resurrect;
8         static Foo reference;
9         static List<Foo> list = new List<Foo> ();
10
11         ~Foo() {
12                 resurrect = this;
13         }
14
15         public static void EnterMonitor (object obj)
16         {
17                 for (int i = 0; i < 257; i++)
18                         Monitor.Enter (obj);
19         }
20
21         public static void ExitMonitor (object obj)
22         {
23                 for (int i = 0; i < 257; i++)
24                         Monitor.Exit (obj);
25         }
26
27         public static void CreateFoo (int level)
28         {
29                 if (level == 0) {
30                         reference = new Foo ();
31
32                         /* Allocate a MonoThreadsSync for the object */
33                         EnterMonitor (reference);
34                         ExitMonitor (reference);
35                         reference = null;
36                 } else {
37                         CreateFoo (level - 1);
38                 }
39         }
40
41         public static int Main() {
42                 /* Allocate an object down the stack so it doesn't get pinned */
43                 CreateFoo (100);
44
45                 Console.WriteLine (".");
46
47                 /* resurrect obj */
48                 GC.Collect ();
49                 GC.WaitForPendingFinalizers ();
50
51                 /* Allocate MonoThreadsSyncs for another thread that are locked */
52                 Thread t = new Thread (new ThreadStart (resurrect.Func));
53                 t.Start ();
54                 t.Join ();
55
56                 /* Make sure that none of the other structures overlap with the original one */
57                 Monitor.Enter (resurrect);
58                 return 0;
59         }
60
61         public void Func () {
62                 for (int i = 0; i < 100; i++) {
63                         Foo foo = new Foo ();
64                         /* Make sure these are not collected */
65                         list.Add (foo);
66
67                         EnterMonitor (foo);
68                 }
69         }
70
71