[runtime] Accomplish BITCODE build symbol sharing with only make (#3329)
[mono-project.git] / mono / tests / finalizer-wait.cs
blob7bbfbe38545bd3b0ccef5a7488286416ecead59b
1 using System;
2 using System.Threading;
3 using System.Runtime.ConstrainedExecution;
5 class P
7 int index;
8 ManualResetEvent mre;
10 public static int Count = 0;
12 public P (int index, ManualResetEvent mre)
14 this.index = index;
15 this.mre = mre;
18 ~P ()
20 mre.Set ();
22 Console.Write (String.Format ("[{0}] Finalize\n", index));
23 Count ++;
24 Console.Write (String.Format ("[{0}] Finalize -- end\n", index));
28 class Driver
30 static int Main ()
32 Thread thread;
33 ManualResetEvent mre;
34 int collected, total = 100;
36 for (int i = 1; i <= 1000; ++i) {
37 P.Count = 0;
39 mre = new ManualResetEvent (false);
41 thread = new Thread (() => {
42 for (int j = 0; j < total; ++j)
43 new P (i, mre);
44 });
45 thread.Start ();
46 thread.Join ();
48 GC.Collect ();
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));
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;
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));
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;
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));
79 return 0;