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