-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix critical finalization test (#75952)
- Loading branch information
1 parent
e013f9d
commit 82d76a4
Showing
4 changed files
with
75 additions
and
55 deletions.
There are no files selected for viewing
53 changes: 0 additions & 53 deletions
53
src/tests/baseservices/critical_finalization/critical_finalization.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// Tests the weak ordering among normal and critical finalizers: for objects reclaimed by garbage collection | ||
// at the same time, all the noncritical finalizers must be called before any of the critical finalizers. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.ConstrainedExecution; | ||
using System.Threading.Tasks; | ||
|
||
class Normal | ||
{ | ||
public static int Finalized; | ||
|
||
~Normal() => Finalized++; | ||
} | ||
|
||
class Critical : CriticalFinalizerObject | ||
{ | ||
public static int Finalized; | ||
public static int NormalFinalizedBeforeFirstCritical; | ||
|
||
~Critical() | ||
{ | ||
if (++Finalized == 1) | ||
NormalFinalizedBeforeFirstCritical = Normal.Finalized; | ||
} | ||
} | ||
|
||
static class CriticalFinalizerTest | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
static void AllocateObjects(int count) | ||
{ | ||
var arr = new object[checked(count * 2)]; | ||
|
||
Parallel.For(0, count, i => | ||
{ | ||
arr[i * 2] = new Normal(); | ||
arr[i * 2 + 1] = new Critical(); | ||
}); | ||
|
||
GC.KeepAlive(arr); | ||
} | ||
|
||
static int Main() | ||
{ | ||
const int Count = 100; | ||
|
||
// Allocate a bunch of Normal and Critical objects, then unroot them | ||
AllocateObjects(Count); | ||
|
||
// Force a garbage collection and wait until all finalizers are executed | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
// Check that all Normal objects were finalized before all Critical objects | ||
int normalFinalized = Normal.Finalized; | ||
int criticalFinalized = Critical.Finalized; | ||
int normalFinalizedBeforeFirstCritical = Critical.NormalFinalizedBeforeFirstCritical; | ||
|
||
if (normalFinalized != Count || criticalFinalized != Count || normalFinalizedBeforeFirstCritical != Count) | ||
{ | ||
Console.WriteLine($"Finalized {normalFinalized} {nameof(Normal)} and {criticalFinalized} {nameof(Critical)} objects."); | ||
Console.WriteLine($"The first {nameof(Critical)} object was finalized after {normalFinalizedBeforeFirstCritical} {nameof(Normal)} objects."); | ||
return 101; | ||
} | ||
|
||
return 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters