Merge pull request #1406 from BrzVlad/monitor-resurrect
[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 CreateFoo (int level)
16         {
17                 if (level == 0)
18                         Foo.reference = new Foo ();
19                 else
20                         CreateFoo (level - 1);
21         }
22
23         public static int Main() {
24                 /* Allocate an object down the stack so it doesn't get pinned */
25                 CreateFoo (100);
26
27                 /* Allocate a MonoThreadsSync for the object */
28                 Monitor.Enter (reference);
29                 Monitor.Exit (reference);
30                 reference = null;
31
32                 /* resurrect obj */
33                 GC.Collect ();
34                 GC.WaitForPendingFinalizers ();
35
36                 /* Allocate MonoThreadsSyncs for another thread that are locked */
37                 Thread t = new Thread (new ThreadStart (resurrect.Func));
38                 t.Start ();
39                 t.Join ();
40
41                 /* Make sure that none of the other structures overlap with the original one */
42                 Monitor.Enter (resurrect);
43                 return 0;
44         }
45
46         public void Func () {
47                 for (int i = 0; i < 100; i++) {
48                         Foo foo = new Foo ();
49                         /* Make sure these are not collected */
50                         list.Add (foo);
51
52                         Monitor.Enter (foo);
53                 }
54         }
55
56