Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/PhantomReference/PhantomHelper.java
41155 views
/*1* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package gc.gctests.PhantomReference;2324import java.lang.ref.ReferenceQueue;25import java.util.Date;26import java.util.HashMap;2728/**29* Helper class that tracks the original hash code for the30* object.31*/32public final class PhantomHelper {3334private int originalHashCode;35private int hashCounter;3637/**38* Constructor for helper class that tracks the hash code.39*40* @param originalHashCode Referred objects hash code41*/42public PhantomHelper(int originalHashCode) {43this.originalHashCode = originalHashCode;44hashCounter = 1;45}4647/**48* Get the referred objects original hash code.49*50* @return Original referred objects hash code51*/52public int getOriginalHashCode() {53return originalHashCode;54}5556/**57* Increase the counter for the number of objects58* using this hash code.59*/60public void increaseHashCounter() {61hashCounter++;62}6364/**65* Decrease the counter for the number of objects66* using this hash code.67*/68public void decreaseHashCounter() {69hashCounter--;70}7172/**73* Retreive the hash code counter.74*75* @return Hash code counter value76*/77public int getHashCounter() {78return hashCounter;79}8081/**82* Verify all the hash codes from the objects in the reference83* queue against the hash map.84*85* @param rq Reference queue for the phantom references.86* @param hmHelper Hashmap that contains all the hash codes87* @param maxWaitTime Maximum time to wait for completion of deref:ing88* from the reference queue.89* @return True if all hash codes matches90*/91public static final String checkAllHashCodes(ReferenceQueue rq,92HashMap hmHelper,93long maxWaitTime) {94// Check all the phantom references95long startTime = new Date().getTime();96boolean keepRunning = true;9798while (keepRunning) {99try {100PRHelper prh = (PRHelper) rq.remove(1000);101102if (prh != null) {103Integer ik = Integer.valueOf(prh.getReferentHashCode());104PhantomHelper ph = (PhantomHelper) hmHelper.get(ik);105106if (ph != null) {107if (ph.getOriginalHashCode()108== prh.getReferentHashCode()) {109ph.decreaseHashCounter();110if (ph.getHashCounter() == 0) {111hmHelper.remove(112Integer.valueOf(prh.getReferentHashCode()));113} else {114hmHelper.put(ik, ph);115}116prh.clear();117}118} else {119return "Unmapped hash code detected. The test is faulty.";120}121122prh = null;123}124} catch (InterruptedException e) {125; // Checkstyle wants at least one line here...126}127128if (new Date().getTime() - startTime > maxWaitTime) {129return "All phantom references weren't processed "130+ "in the configured max time ("131+ (maxWaitTime / 1000) + " secs)";132}133134if (hmHelper.size() == 0) {135keepRunning = false;136}137}138139return null;140}141}142143144