jdk/src/java.desktop/share/classes/sun/java2d/Disposer.java
author lbourges
Mon, 23 Nov 2015 15:02:19 -0800
changeset 34419 14108cfd0823
parent 31879 03c5b4e9687e
child 37550 c8252b8fea3d
permissions -rw-r--r--
8143849: Integrate Marlin renderer per JEP 265 Reviewed-by: flar, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
     2
 * Copyright (c) 2002, 2015, 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
24520
e8afd90fcb69 8035169: Move ThreadGroupUtils from the sun.misc package
pchelko
parents: 23900
diff changeset
    28
import sun.awt.util.ThreadGroupUtils;
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
    29
import sun.misc.ManagedLocalsThread;
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23288
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.ref.PhantomReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23288
diff changeset
    35
import java.security.AccessController;
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23288
diff changeset
    36
import java.security.PrivilegedAction;
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
    37
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class is used for registering and disposing the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * data associated with java objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The object can register itself by calling one of the addRecord
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * methods and providing either the pointer to the native disposal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * method or a descendant of the DisposerRecord class with overridden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * dispose() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * When the object becomes unreachable, the dispose() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * of the associated DisposerRecord object will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see DisposerRecord
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class Disposer implements Runnable {
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
    55
    private static final ReferenceQueue<Object> queue = new ReferenceQueue<>();
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
    56
    private static final Hashtable<java.lang.ref.Reference<Object>, DisposerRecord> records =
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
    57
        new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static Disposer disposerInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int WEAK = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int PHANTOM = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static int refType = PHANTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        java.security.AccessController.doPrivileged(
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    66
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    67
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    68
                    System.loadLibrary("awt");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    69
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    70
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 7668
diff changeset
    71
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        initIDs();
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20811
diff changeset
    73
        String type = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                new sun.security.action.GetPropertyAction("sun.java2d.reftype"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (type.equals("weak")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                refType = WEAK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                System.err.println("Using WEAK refs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                refType = PHANTOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                System.err.println("Using PHANTOM refs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        disposerInstance = new Disposer();
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    85
        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    86
            String name = "Java2D Disposer";
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
    87
            ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
    88
            Thread t = new ManagedLocalsThread(rootTG, disposerInstance, name);
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    89
            t.setContextClassLoader(null);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    90
            t.setDaemon(true);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    91
            t.setPriority(Thread.MAX_PRIORITY);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    92
            t.start();
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    93
            return null;
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 25859
diff changeset
    94
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Registers the object and the native data for later disposal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param target Object to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param disposeMethod pointer to the native disposal method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param pData pointer to the data to be passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *              native disposal method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void addRecord(Object target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                 long disposeMethod, long pData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        disposerInstance.add(target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                             new DefaultDisposerRecord(disposeMethod, pData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Registers the object and the native data for later disposal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param target Object to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param rec the associated DisposerRecord object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see DisposerRecord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static void addRecord(Object target, DisposerRecord rec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        disposerInstance.add(target, rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Performs the actual registration of the target object to be disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param target Object to be registered, or if target is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *               of DisposerTarget, its associated disposer referent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *               will be the Object that is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param rec the associated DisposerRecord object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @see DisposerRecord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    synchronized void add(Object target, DisposerRecord rec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (target instanceof DisposerTarget) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            target = ((DisposerTarget)target).getDisposerReferent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   133
        java.lang.ref.Reference<Object> ref;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (refType == PHANTOM) {
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   135
            ref = new PhantomReference<>(target, queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   137
            ref = new WeakReference<>(target, queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        records.put(ref, rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                Object obj = queue.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ((Reference)obj).clear();
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   147
                DisposerRecord rec = records.remove(obj);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                rec.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                obj = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                rec = null;
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   151
                clearDeferredRecords();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } catch (Exception e) {
20811
6b25b85f10fd 8017287: Better resource disposal
bae
parents: 12559
diff changeset
   153
                System.out.println("Exception while removing reference.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   158
    /*
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   159
     * 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
   160
     * 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
   161
     * 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
   162
     * 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
   163
     */
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   164
    public static interface PollDisposable {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   165
    };
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   166
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   167
    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
   168
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   169
    private static void clearDeferredRecords() {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   170
        if (deferredRecords == null || deferredRecords.isEmpty()) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   171
            return;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   172
        }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   173
        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
   174
            try {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   175
                DisposerRecord rec = deferredRecords.get(i);
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   176
                rec.dispose();
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   177
            } catch (Exception e) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   178
                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
   179
            }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   180
        }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   181
        deferredRecords.clear();
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   182
    }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   183
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
     * 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
   186
     */
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   187
    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
   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
     * 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
   191
     * 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
   192
     * 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
   193
     * on that thread.
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
    public static void pollRemove() {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   196
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   197
        /* 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
   198
         * 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
   199
         */
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   200
        if (pollingQueue) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   201
            return;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   202
        }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   203
        Object obj;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   204
        pollingQueue = true;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   205
        int freed = 0;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   206
        int deferred = 0;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   207
        try {
31879
03c5b4e9687e 8129457: Disposer.pollRemove may fail to dispose
psadhukhan
parents: 31653
diff changeset
   208
            while ( freed < 10000 && deferred < 100 &&
03c5b4e9687e 8129457: Disposer.pollRemove may fail to dispose
psadhukhan
parents: 31653
diff changeset
   209
                    (obj = queue.poll()) != null ) {
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   210
                freed++;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   211
                ((Reference)obj).clear();
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   212
                DisposerRecord rec = records.remove(obj);
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   213
                if (rec instanceof PollDisposable) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   214
                    rec.dispose();
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   215
                    obj = null;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   216
                    rec = null;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   217
                } else {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   218
                    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
   219
                        continue;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   220
                    }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   221
                    deferred++;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   222
                    if (deferredRecords == null) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   223
                      deferredRecords = new ArrayList<DisposerRecord>(5);
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   224
                    }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   225
                    deferredRecords.add(rec);
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   226
                }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   227
            }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   228
        } catch (Exception e) {
20811
6b25b85f10fd 8017287: Better resource disposal
bae
parents: 12559
diff changeset
   229
            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
   230
        } finally {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   231
            pollingQueue = false;
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
    }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 2
diff changeset
   234
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * This was added for use by the 2D font implementation to avoid creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * of an additional disposer thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * WARNING: this thread class monitors a specific queue, so a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * added here must have been created with this queue. Failure to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * so will clutter the records hashmap and no one will be cleaning up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * the reference queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   245
    @SuppressWarnings("unchecked")
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   246
    public static void addReference(Reference<Object> ref, DisposerRecord rec) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        records.put(ref, rec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static void addObjectRecord(Object obj, DisposerRecord rec) {
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   251
        records.put(new WeakReference<>(obj, queue) , rec);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /* This is intended for use in conjunction with addReference(..)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
23288
b7183846db97 8033624: Fix raw and unchecked lint warnings in sun.font
darcy
parents: 22584
diff changeset
   256
    public static ReferenceQueue<Object> getQueue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
}