1 /*
   2  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d;
  27 
  28 import java.lang.ref.Reference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.lang.ref.PhantomReference;
  31 import java.lang.ref.WeakReference;
  32 import java.util.ArrayList;
  33 import java.util.Hashtable;
  34 
  35 /**
  36  * This class is used for registering and disposing the native
  37  * data associated with java objects.
  38  *
  39  * The object can register itself by calling one of the addRecord
  40  * methods and providing either the pointer to the native disposal
  41  * method or a descendant of the DisposerRecord class with overridden
  42  * dispose() method.
  43  *
  44  * When the object becomes unreachable, the dispose() method
  45  * of the associated DisposerRecord object will be called.
  46  *
  47  * @see DisposerRecord
  48  */
  49 public class Disposer implements Runnable {
  50     private static final ReferenceQueue queue = new ReferenceQueue();
  51     private static final Hashtable records = new Hashtable();
  52 
  53     private static Disposer disposerInstance;
  54     public static final int WEAK = 0;
  55     public static final int PHANTOM = 1;
  56     public static int refType = PHANTOM;
  57 
  58     static {
  59         java.security.AccessController.doPrivileged(
  60             new java.security.PrivilegedAction<Void>() {
  61                 public Void run() {
  62                     System.loadLibrary("awt");
  63                     return null;
  64                 }
  65             });
  66         initIDs();
  67         String type = (String) java.security.AccessController.doPrivileged(
  68                 new sun.security.action.GetPropertyAction("sun.java2d.reftype"));
  69         if (type != null) {
  70             if (type.equals("weak")) {
  71                 refType = WEAK;
  72                 System.err.println("Using WEAK refs");
  73             } else {
  74                 refType = PHANTOM;
  75                 System.err.println("Using PHANTOM refs");
  76             }
  77         }
  78         disposerInstance = new Disposer();
  79         java.security.AccessController.doPrivileged(
  80             new java.security.PrivilegedAction() {
  81                 public Object run() {
  82                     /* The thread must be a member of a thread group
  83                      * which will not get GCed before VM exit.
  84                      * Make its parent the top-level thread group.
  85                      */
  86                     ThreadGroup tg = Thread.currentThread().getThreadGroup();
  87                     for (ThreadGroup tgn = tg;
  88                          tgn != null;
  89                          tg = tgn, tgn = tg.getParent());
  90                     Thread t =
  91                         new Thread(tg, disposerInstance, "Java2D Disposer");
  92                     t.setContextClassLoader(null);
  93                     t.setDaemon(true);
  94                     t.setPriority(Thread.MAX_PRIORITY);
  95                     t.start();
  96                     return null;
  97                 }
  98             }
  99         );
 100     }
 101 
 102     /**
 103      * Registers the object and the native data for later disposal.
 104      * @param target Object to be registered
 105      * @param disposeMethod pointer to the native disposal method
 106      * @param pData pointer to the data to be passed to the
 107      *              native disposal method
 108      */
 109     public static void addRecord(Object target,
 110                                  long disposeMethod, long pData)
 111     {
 112         disposerInstance.add(target,
 113                              new DefaultDisposerRecord(disposeMethod, pData));
 114     }
 115 
 116     /**
 117      * Registers the object and the native data for later disposal.
 118      * @param target Object to be registered
 119      * @param rec the associated DisposerRecord object
 120      * @see DisposerRecord
 121      */
 122     public static void addRecord(Object target, DisposerRecord rec) {
 123         disposerInstance.add(target, rec);
 124     }
 125 
 126     /**
 127      * Performs the actual registration of the target object to be disposed.
 128      * @param target Object to be registered, or if target is an instance
 129      *               of DisposerTarget, its associated disposer referent
 130      *               will be the Object that is registered
 131      * @param rec the associated DisposerRecord object
 132      * @see DisposerRecord
 133      */
 134     synchronized void add(Object target, DisposerRecord rec) {
 135         if (target instanceof DisposerTarget) {
 136             target = ((DisposerTarget)target).getDisposerReferent();
 137         }
 138         java.lang.ref.Reference ref;
 139         if (refType == PHANTOM) {
 140             ref = new PhantomReference(target, queue);
 141         } else {
 142             ref = new WeakReference(target, queue);
 143         }
 144         records.put(ref, rec);
 145     }
 146 
 147     public void run() {
 148         while (true) {
 149             try {
 150                 Object obj = queue.remove();
 151                 ((Reference)obj).clear();
 152                 DisposerRecord rec = (DisposerRecord)records.remove(obj);
 153                 rec.dispose();
 154                 obj = null;
 155                 rec = null;
 156                 clearDeferredRecords();
 157             } catch (Exception e) {
 158                 System.out.println("Exception while removing reference: " + e);
 159                 e.printStackTrace();
 160             }
 161         }
 162     }
 163 
 164     /*
 165      * This is a marker interface that, if implemented, means it
 166      * doesn't acquire any special locks, and is safe to
 167      * be disposed in the poll loop on whatever thread
 168      * which happens to be the Toolkit thread, is in use.
 169      */
 170     public static interface PollDisposable {
 171     };
 172 
 173     private static ArrayList<DisposerRecord> deferredRecords = null;
 174 
 175     private static void clearDeferredRecords() {
 176         if (deferredRecords == null || deferredRecords.isEmpty()) {
 177             return;
 178         }
 179         for (int i=0;i<deferredRecords.size(); i++) {
 180             try {
 181                 DisposerRecord rec = deferredRecords.get(i);
 182                 rec.dispose();
 183             } catch (Exception e) {
 184                 System.out.println("Exception while disposing deferred rec.");
 185                 e.printStackTrace();
 186             }
 187         }
 188         deferredRecords.clear();
 189     }
 190 
 191     /*
 192      * Set to indicate the queue is presently being polled.
 193      */
 194     public static volatile boolean pollingQueue = false;
 195 
 196     /*
 197      * The pollRemove() method is called back from a dispose method
 198      * that is running on the toolkit thread and wants to
 199      * dispose any pending refs that are safe to be disposed
 200      * on that thread.
 201      */
 202     public static void pollRemove() {
 203 
 204         /* This should never be called recursively, so this check
 205          * is just a safeguard against the unexpected.
 206          */
 207         if (pollingQueue) {
 208             return;
 209         }
 210         Object obj;
 211         pollingQueue = true;
 212         int freed = 0;
 213         int deferred = 0;
 214         try {
 215             while ((obj = queue.poll()) != null
 216                    && freed < 10000 && deferred < 100) {
 217                 freed++;
 218                 ((Reference)obj).clear();
 219                 DisposerRecord rec = (DisposerRecord)records.remove(obj);
 220                 if (rec instanceof PollDisposable) {
 221                     rec.dispose();
 222                     obj = null;
 223                     rec = null;
 224                 } else {
 225                     if (rec == null) { // shouldn't happen, but just in case.
 226                         continue;
 227                     }
 228                     deferred++;
 229                     if (deferredRecords == null) {
 230                       deferredRecords = new ArrayList<DisposerRecord>(5);
 231                     }
 232                     deferredRecords.add(rec);
 233                 }
 234             }
 235         } catch (Exception e) {
 236             System.out.println("Exception while removing reference: " + e);
 237             e.printStackTrace();
 238         } finally {
 239             pollingQueue = false;
 240         }
 241     }
 242 
 243     private static native void initIDs();
 244 
 245     /*
 246      * This was added for use by the 2D font implementation to avoid creation
 247      * of an additional disposer thread.
 248      * WARNING: this thread class monitors a specific queue, so a reference
 249      * added here must have been created with this queue. Failure to do
 250      * so will clutter the records hashmap and no one will be cleaning up
 251      * the reference queue.
 252      */
 253     public static void addReference(Reference ref, DisposerRecord rec) {
 254         records.put(ref, rec);
 255     }
 256 
 257     public static void addObjectRecord(Object obj, DisposerRecord rec) {
 258         records.put(new WeakReference(obj, queue) , rec);
 259     }
 260 
 261     /* This is intended for use in conjunction with addReference(..)
 262      */
 263     public static ReferenceQueue getQueue() {
 264         return queue;
 265     }
 266 
 267 }