Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / finalizer-wait.cs
1 using System;
2 using System.Threading;
3 using System.Runtime.ConstrainedExecution;
4
5 class P
6 {
7         int index;
8         ManualResetEvent mre;
9
10         public static int Count = 0;
11
12         public P (int index, ManualResetEvent mre)
13         {
14                 this.index = index;
15                 this.mre = mre;
16         }
17
18         ~P ()
19         {
20                 mre.Set ();
21
22                 Console.Write (String.Format ("[{0}] Finalize\n", index));
23                 Count ++;
24                 Console.Write (String.Format ("[{0}] Finalize -- end\n", index));
25         }
26 }
27
28 class Driver
29 {
30         static int Main ()
31         {
32                 Thread thread;
33                 ManualResetEvent mre;
34                 int collected, total = 100;
35
36                 for (int i = 1; i <= 1000; ++i) {
37                         P.Count = 0;
38
39                         mre = new ManualResetEvent (false);
40
41                         thread = new Thread (() => {
42                                 for (int j = 0; j < total; ++j)
43                                         new P (i, mre);
44                         });
45                         thread.Start ();
46                         thread.Join ();
47
48                         GC.Collect ();
49
50                         Console.Write (String.Format ("[{0}] Wait for pending finalizers\n", i));
51                         GC.WaitForPendingFinalizers ();
52                         Console.Write (String.Format ("[{0}] Wait for pending finalizers -- end\n", i));
53
54                         collected = P.Count;
55                         if (collected == 0) {
56                                 if (!mre.WaitOne (5000)) {
57                                         Console.Write (String.Format ("[{0}] Finalizer never started\n", i));
58                                         return 1;
59                                 }
60
61                                 Console.Write (String.Format ("[{0}] Wait for pending finalizers (2)\n", i));
62                                 GC.WaitForPendingFinalizers ();
63                                 Console.Write (String.Format ("[{0}] Wait for pending finalizers (2) -- end\n", i));
64
65                                 collected = P.Count;
66                                 if (collected == 0) {
67                                         /* At least 1 finalizer started (as mre has been Set), but P.Count has not been incremented */
68                                         Console.Write (String.Format ("[{0}] Did not wait for finalizers to run\n", i));
69                                         return 2;
70                                 }
71                         }
72
73                         if (collected != total) {
74                                 /* Not all finalizer finished, before returning from WaitForPendingFinalizers. Or not all objects
75                                  * have been garbage collected; this might be due to false pinning */
76                                 Console.Write (String.Format ("[{0}] Finalized {1} of {2} objects\n", i, collected, total));
77                         }
78                 }
79                 return 0;
80         }
81 }