0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d;
0N/A
0N/Aimport java.lang.ref.Reference;
0N/Aimport java.lang.ref.ReferenceQueue;
0N/Aimport java.lang.ref.PhantomReference;
0N/Aimport java.lang.ref.WeakReference;
1886N/Aimport java.util.ArrayList;
0N/Aimport java.util.Hashtable;
0N/A
0N/A/**
0N/A * This class is used for registering and disposing the native
0N/A * data associated with java objects.
0N/A *
0N/A * The object can register itself by calling one of the addRecord
0N/A * methods and providing either the pointer to the native disposal
0N/A * method or a descendant of the DisposerRecord class with overridden
0N/A * dispose() method.
0N/A *
0N/A * When the object becomes unreachable, the dispose() method
0N/A * of the associated DisposerRecord object will be called.
0N/A *
0N/A * @see DisposerRecord
0N/A */
0N/Apublic class Disposer implements Runnable {
0N/A private static final ReferenceQueue queue = new ReferenceQueue();
0N/A private static final Hashtable records = new Hashtable();
0N/A
0N/A private static Disposer disposerInstance;
0N/A public static final int WEAK = 0;
0N/A public static final int PHANTOM = 1;
0N/A public static int refType = PHANTOM;
0N/A
0N/A static {
0N/A java.security.AccessController.doPrivileged(
0N/A new sun.security.action.LoadLibraryAction("awt"));
0N/A initIDs();
0N/A String type = (String) java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("sun.java2d.reftype"));
0N/A if (type != null) {
0N/A if (type.equals("weak")) {
0N/A refType = WEAK;
0N/A System.err.println("Using WEAK refs");
0N/A } else {
0N/A refType = PHANTOM;
0N/A System.err.println("Using PHANTOM refs");
0N/A }
0N/A }
0N/A disposerInstance = new Disposer();
0N/A java.security.AccessController.doPrivileged(
0N/A new java.security.PrivilegedAction() {
0N/A public Object run() {
0N/A /* The thread must be a member of a thread group
0N/A * which will not get GCed before VM exit.
0N/A * Make its parent the top-level thread group.
0N/A */
0N/A ThreadGroup tg = Thread.currentThread().getThreadGroup();
0N/A for (ThreadGroup tgn = tg;
0N/A tgn != null;
0N/A tg = tgn, tgn = tg.getParent());
0N/A Thread t =
0N/A new Thread(tg, disposerInstance, "Java2D Disposer");
2285N/A t.setContextClassLoader(null);
0N/A t.setDaemon(true);
0N/A t.setPriority(Thread.MAX_PRIORITY);
0N/A t.start();
0N/A return null;
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A /**
0N/A * Registers the object and the native data for later disposal.
0N/A * @param target Object to be registered
0N/A * @param disposeMethod pointer to the native disposal method
0N/A * @param pData pointer to the data to be passed to the
0N/A * native disposal method
0N/A */
0N/A public static void addRecord(Object target,
0N/A long disposeMethod, long pData)
0N/A {
0N/A disposerInstance.add(target,
0N/A new DefaultDisposerRecord(disposeMethod, pData));
0N/A }
0N/A
0N/A /**
0N/A * Registers the object and the native data for later disposal.
0N/A * @param target Object to be registered
0N/A * @param rec the associated DisposerRecord object
0N/A * @see DisposerRecord
0N/A */
0N/A public static void addRecord(Object target, DisposerRecord rec) {
0N/A disposerInstance.add(target, rec);
0N/A }
0N/A
0N/A /**
0N/A * Performs the actual registration of the target object to be disposed.
0N/A * @param target Object to be registered, or if target is an instance
0N/A * of DisposerTarget, its associated disposer referent
0N/A * will be the Object that is registered
0N/A * @param rec the associated DisposerRecord object
0N/A * @see DisposerRecord
0N/A */
0N/A synchronized void add(Object target, DisposerRecord rec) {
0N/A if (target instanceof DisposerTarget) {
0N/A target = ((DisposerTarget)target).getDisposerReferent();
0N/A }
0N/A java.lang.ref.Reference ref;
0N/A if (refType == PHANTOM) {
0N/A ref = new PhantomReference(target, queue);
0N/A } else {
0N/A ref = new WeakReference(target, queue);
0N/A }
0N/A records.put(ref, rec);
0N/A }
0N/A
0N/A public void run() {
0N/A while (true) {
0N/A try {
0N/A Object obj = queue.remove();
0N/A ((Reference)obj).clear();
0N/A DisposerRecord rec = (DisposerRecord)records.remove(obj);
0N/A rec.dispose();
0N/A obj = null;
0N/A rec = null;
1886N/A clearDeferredRecords();
0N/A } catch (Exception e) {
0N/A System.out.println("Exception while removing reference: " + e);
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A }
0N/A
1886N/A /*
1886N/A * This is a marker interface that, if implemented, means it
1886N/A * doesn't acquire any special locks, and is safe to
1886N/A * be disposed in the poll loop on whatever thread
1886N/A * which happens to be the Toolkit thread, is in use.
1886N/A */
1886N/A public static interface PollDisposable {
1886N/A };
1886N/A
1886N/A private static ArrayList<DisposerRecord> deferredRecords = null;
1886N/A
1886N/A private static void clearDeferredRecords() {
1886N/A if (deferredRecords == null || deferredRecords.isEmpty()) {
1886N/A return;
1886N/A }
1886N/A for (int i=0;i<deferredRecords.size(); i++) {
1886N/A try {
1886N/A DisposerRecord rec = deferredRecords.get(i);
1886N/A rec.dispose();
1886N/A } catch (Exception e) {
1886N/A System.out.println("Exception while disposing deferred rec.");
1886N/A e.printStackTrace();
1886N/A }
1886N/A }
1886N/A deferredRecords.clear();
1886N/A }
1886N/A
1886N/A /*
1886N/A * Set to indicate the queue is presently being polled.
1886N/A */
1886N/A public static volatile boolean pollingQueue = false;
1886N/A
1886N/A /*
1886N/A * The pollRemove() method is called back from a dispose method
1886N/A * that is running on the toolkit thread and wants to
1886N/A * dispose any pending refs that are safe to be disposed
1886N/A * on that thread.
1886N/A */
1886N/A public static void pollRemove() {
1886N/A
1886N/A /* This should never be called recursively, so this check
1886N/A * is just a safeguard against the unexpected.
1886N/A */
1886N/A if (pollingQueue) {
1886N/A return;
1886N/A }
1886N/A Object obj;
1886N/A pollingQueue = true;
1886N/A int freed = 0;
1886N/A int deferred = 0;
1886N/A try {
1886N/A while ((obj = queue.poll()) != null
1886N/A && freed < 10000 && deferred < 100) {
1886N/A freed++;
1886N/A ((Reference)obj).clear();
1886N/A DisposerRecord rec = (DisposerRecord)records.remove(obj);
1886N/A if (rec instanceof PollDisposable) {
1886N/A rec.dispose();
1886N/A obj = null;
1886N/A rec = null;
1886N/A } else {
1886N/A if (rec == null) { // shouldn't happen, but just in case.
1886N/A continue;
1886N/A }
1886N/A deferred++;
1886N/A if (deferredRecords == null) {
1886N/A deferredRecords = new ArrayList<DisposerRecord>(5);
1886N/A }
1886N/A deferredRecords.add(rec);
1886N/A }
1886N/A }
1886N/A } catch (Exception e) {
1886N/A System.out.println("Exception while removing reference: " + e);
1886N/A e.printStackTrace();
1886N/A } finally {
1886N/A pollingQueue = false;
1886N/A }
1886N/A }
1886N/A
0N/A private static native void initIDs();
0N/A
0N/A /*
0N/A * This was added for use by the 2D font implementation to avoid creation
0N/A * of an additional disposer thread.
0N/A * WARNING: this thread class monitors a specific queue, so a reference
0N/A * added here must have been created with this queue. Failure to do
0N/A * so will clutter the records hashmap and no one will be cleaning up
0N/A * the reference queue.
0N/A */
0N/A public static void addReference(Reference ref, DisposerRecord rec) {
0N/A records.put(ref, rec);
0N/A }
0N/A
0N/A public static void addObjectRecord(Object obj, DisposerRecord rec) {
0N/A records.put(new WeakReference(obj, queue) , rec);
0N/A }
0N/A
0N/A /* This is intended for use in conjunction with addReference(..)
0N/A */
0N/A public static ReferenceQueue getQueue() {
0N/A return queue;
0N/A }
0N/A
0N/A}