8339687: Rearrange reachabilityFence()s in jdk.test.lib.util.ForceGC

Reviewed-by: dholmes, smarks, kbarrett
This commit is contained in:
Brent Christian 2024-09-11 19:02:05 +00:00
parent d9fdf69c34
commit 51b85a1f69

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -78,29 +78,30 @@ public class ForceGC {
ReferenceQueue<Object> queue = new ReferenceQueue<>(); ReferenceQueue<Object> queue = new ReferenceQueue<>();
Object obj = new Object(); Object obj = new Object();
PhantomReference<Object> ref = new PhantomReference<>(obj, queue); PhantomReference<Object> ref = new PhantomReference<>(obj, queue);
obj = null; try {
Reference.reachabilityFence(obj); obj = null;
Reference.reachabilityFence(ref);
int retries = (int)(timeout / 200); int retries = (int) (timeout / 200);
for (; retries >= 0; retries--) { for (; retries >= 0; retries--) {
if (booleanSupplier.getAsBoolean()) { if (booleanSupplier.getAsBoolean()) {
return true; return true;
} }
System.gc(); System.gc();
try { try {
// The remove() will always block for the specified milliseconds // The remove() will always block for the specified milliseconds
// if the reference has already been removed from the queue. // if the reference has already been removed from the queue.
// But it is fine. For most cases, the 1st GC is sufficient // But it is fine. For most cases, the 1st GC is sufficient
// to trigger and complete the cleanup. // to trigger and complete the cleanup.
queue.remove(200L); queue.remove(200L);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
// ignore, the loop will try again // ignore, the loop will try again
}
} }
} finally {
Reference.reachabilityFence(ref);
} }
return booleanSupplier.getAsBoolean(); return booleanSupplier.getAsBoolean();
} }
} }