[runtime] Make WaitForPendingFinalizers () really wait for the finalizers added by a previous GC. Fixes #20503.

This commit is contained in:
Zoltan Varga committed 2014-07-01 21:10:33 +02:00
1 parent e773c8681b
commit a57fb5b645
2 files changed
+27 -23

No files matched your search

+17 -3
View File
@@ -1062,6 +1062,8 @@ finalize_domain_objects (DomainFinalizationReq *req)
static guint32
finalizer_thread (gpointer unused)
{
gboolean wait = TRUE;
while (!finished) {
/* Wait to be notified that there's at least one
* finaliser to run
@@ -1069,12 +1071,15 @@ finalizer_thread (gpointer unused)
g_assert (mono_domain_get () == mono_get_root_domain ());
if (wait) {
/* An alertable wait is required so this thread can be suspended on windows */
#ifdef MONO_HAS_SEMAPHORES
MONO_SEM_WAIT_ALERTABLE (&finalizer_sem, TRUE);
MONO_SEM_WAIT_ALERTABLE (&finalizer_sem, TRUE);
#else
WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
#endif
}
wait = TRUE;
mono_threads_perform_thread_dump ();
@@ -1106,7 +1111,16 @@ finalizer_thread (gpointer unused)
reference_queue_proccess_all ();
SetEvent (pending_done_event);
#ifdef MONO_HAS_SEMAPHORES
/* Avoid posting the pending done event until there are pending finalizers */
if (MONO_SEM_TIMEDWAIT (&finalizer_sem, 0) == 0)
/* Don't wait again at the start of the loop */
wait = FALSE;
else
SetEvent (pending_done_event);
#else
SetEvent (pending_done_event);
#endif
}
SetEvent (shutdown_event);
+10 -20
View File
@@ -6,35 +6,25 @@ class P {
static public int count = 0;
~P () {
T.finalized = true;
Thread.Sleep (1000);
// Console.WriteLine ("finalizer done");
count++;
}
}
class T {
static public bool finalized = false;
static void makeP () {
P p = new P ();
p = null;
}
static int Main () {
var t = new Thread (makeP);
t.Start ();
t.Join ();
for (int i = 0; i < 1000; ++i) {
var t = new Thread (() => {
P p = new P ();
});
t.Start ();
t.Join ();
GC.Collect ();
while (!finalized) {
Thread.Sleep (100);
GC.Collect ();
GC.WaitForPendingFinalizers ();
if (P.count != i + 1)
return 1;
}
GC.WaitForPendingFinalizers ();
if (P.count == 0)
return 1;
return 0;
}
}