jdk/src/java.desktop/share/classes/sun/awt/AWTAutoShutdown.java
author serb
Fri, 17 Apr 2015 16:54:13 +0300
changeset 30469 bac0a7ff7e1e
parent 29922 7b9c1e1532cf
child 30488 3a8007752b94
permissions -rw-r--r--
8074028: Remove API references to java.awt.peer Reviewed-by: alanb, ant, yan, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18178
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 3972
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: 3972
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: 3972
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3972
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3972
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.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.AWTEvent;
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    29
23893
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    30
import java.security.AccessController;
1a92335b55bb 8025010: Enhance AWT contexts
pchelko
parents: 23010
diff changeset
    31
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.IdentityHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    35
import java.util.Set;
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    36
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
    37
import sun.misc.InnocuousThread;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2
diff changeset
    38
import sun.util.logging.PlatformLogger;
24520
e8afd90fcb69 8035169: Move ThreadGroupUtils from the sun.misc package
pchelko
parents: 23900
diff changeset
    39
import sun.awt.util.ThreadGroupUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class is to let AWT shutdown automatically when a user is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * with AWT. It tracks AWT state using the following parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li><code>peerMap</code> - the map between the existing peer objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     and their associated targets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li><code>toolkitThreadBusy</code> - whether the toolkit thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     is waiting for a new native event to appear in its queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     or is dispatching an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li><code>busyThreadSet</code> - a set of all the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     threads that are busy at this moment, i.e. those that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     waiting for a new event to appear in their event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </ul><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * AWT is considered to be in ready-to-shutdown state when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>peerMap</code> is empty and <code>toolkitThreadBusy</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * is false and <code>busyThreadSet</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The internal AWTAutoShutdown logic secures that the single non-daemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * thread (<code>blockerThread</code>) is running when AWT is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * ready-to-shutdown state. This blocker thread is to prevent AWT from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * exiting since the toolkit thread is now daemon and all the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * dispatch threads are started only when needed. Once it is detected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * that AWT is in ready-to-shutdown state this blocker thread waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * for a certain timeout and if AWT state doesn't change during timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * this blocker thread terminates all the event dispatch threads and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * exits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public final class AWTAutoShutdown implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final AWTAutoShutdown theInstance = new AWTAutoShutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * This lock object is used to synchronize shutdown operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final Object mainLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * This lock object is to secure that when a new blocker thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * started it will be the first who acquire the main lock after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * the thread that created the new blocker released the main lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * by calling lock.wait() to wait for the blocker to start.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final Object activationLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * This set keeps references to all the event dispatch threads that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * are busy at this moment, i.e. those that are not waiting for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * new event to appear in their event queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Access is synchronized on the main lock object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
    90
    private final Set<Thread> busyThreadSet = new HashSet<>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Indicates whether the toolkit thread is waiting for a new native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * event to appear or is dispatching an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private boolean toolkitThreadBusy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * This is a map between components and their peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * we should work with in under activationLock&mainLock lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   102
    private final Map<Object, Object> peerMap = new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * References the alive non-daemon thread that is currently used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * for keeping AWT from exiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Thread blockerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * We need this flag to secure that AWT state hasn't changed while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * we were waiting for the safety timeout to pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private boolean timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Once we detect that AWT is ready to shutdown we wait for a certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * timeout to pass before stopping event dispatch threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final int SAFETY_TIMEOUT = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Constructor method is intentionally made private to secure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * a single instance. Use getInstance() to reference it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @see     AWTAutoShutdown#getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private AWTAutoShutdown() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns reference to a single AWTAutoShutdown instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static AWTAutoShutdown getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return theInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Notify that the toolkit thread is not waiting for a native event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * to appear in its queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see     AWTAutoShutdown#notifyToolkitThreadFree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see     AWTAutoShutdown#setToolkitBusy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static void notifyToolkitThreadBusy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        getInstance().setToolkitBusy(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Notify that the toolkit thread is waiting for a native event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * to appear in its queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see     AWTAutoShutdown#notifyToolkitThreadFree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see     AWTAutoShutdown#setToolkitBusy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static void notifyToolkitThreadFree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        getInstance().setToolkitBusy(false);
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
     * Add a specified thread to the set of busy event dispatch threads.
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   163
     * If this set already contains the specified thread or the thread is null,
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   164
     * the call leaves this set unchanged and returns silently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param thread thread to be added to this set, if not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see     AWTAutoShutdown#notifyThreadFree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void notifyThreadBusy(final Thread thread) {
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   171
        if (thread == null) {
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   172
            return;
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   173
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (blockerThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    activateBlockerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else if (isReadyToShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                busyThreadSet.add(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Remove a specified thread from the set of busy event dispatch threads.
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   189
     * If this set doesn't contain the specified thread or the thread is null,
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   190
     * the call leaves this set unchanged and returns silently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param thread thread to be removed from this set, if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see     AWTAutoShutdown#notifyThreadBusy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void notifyThreadFree(final Thread thread) {
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   197
        if (thread == null) {
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   198
            return;
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 2
diff changeset
   199
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                busyThreadSet.remove(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if (isReadyToShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Notify that the peermap has been updated, that means a new peer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * has been created or some existing peer has been disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    void notifyPeerMapUpdated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                if (!isReadyToShutdown() && blockerThread == null) {
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   221
                    AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   222
                        activateBlockerThread();
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   223
                        return null;
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   224
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Determine whether AWT is currently in ready-to-shutdown state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * AWT is considered to be in ready-to-shutdown state if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <code>peerMap</code> is empty and <code>toolkitThreadBusy</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * is false and <code>busyThreadSet</code> is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return true if AWT is in ready-to-shutdown state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    private boolean isReadyToShutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return (!toolkitThreadBusy &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                 peerMap.isEmpty() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                 busyThreadSet.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Notify about the toolkit thread state change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param busy true if the toolkit thread state changes from idle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *             to busy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see     AWTAutoShutdown#notifyToolkitThreadBusy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see     AWTAutoShutdown#notifyToolkitThreadFree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private void setToolkitBusy(final boolean busy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (busy != toolkitThreadBusy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    if (busy != toolkitThreadBusy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        if (busy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                            if (blockerThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                activateBlockerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            } else if (isReadyToShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            toolkitThreadBusy = busy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                            toolkitThreadBusy = busy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            if (isReadyToShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Implementation of the Runnable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Incapsulates the blocker thread functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @see     AWTAutoShutdown#isReadyToShutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Thread currentThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        boolean interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                /* Notify that the thread is started. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                mainLock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                while (blockerThread == currentThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    mainLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                     * This loop is introduced to handle the following case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                     * it is possible that while we are waiting for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                     * safety timeout to pass AWT state can change to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                     * not-ready-to-shutdown and back to ready-to-shutdown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                     * In this case we have to wait once again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                     * NOTE: we shouldn't break into the outer loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                     * in this case, since we may never be notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     * in an outer infinite wait at this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    while (isReadyToShutdown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        if (timeoutPassed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            timeoutPassed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                            blockerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        timeoutPassed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        mainLock.wait(SAFETY_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                interrupted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (blockerThread == currentThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    blockerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (!interrupted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            AppContext.stopEventDispatchThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   331
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static AWTEvent getShutdownEvent() {
11264
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   333
        return new AWTEvent(getInstance(), 0) {
54f2f4c6bd30 7117008: Warnings cleanup day: reduce number of javac warnings in the sun.awt package
art
parents: 5506
diff changeset
   334
        };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Creates and starts a new blocker thread. Doesn't return until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * the new blocker thread starts.
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   340
     *
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   341
     * Must be called with {@link sun.security.util.SecurityConstants#MODIFY_THREADGROUP_PERMISSION}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private void activateBlockerThread() {
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   344
        Thread thread;
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   345
        String name =  "AWT-Shutdown";
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   346
        if (System.getSecurityManager() == null) {
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   347
            thread = new Thread(ThreadGroupUtils.getRootThreadGroup(), this, name);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   348
        } else {
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   349
            thread = new InnocuousThread(this, name);
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26037
diff changeset
   350
        }
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23893
diff changeset
   351
        thread.setContextClassLoader(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        thread.setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        blockerThread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            /* Wait for the blocker thread to start. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            mainLock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            System.err.println("AWT blocker activation interrupted:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 24520
diff changeset
   364
    void registerPeer(final Object target, final Object peer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                peerMap.put(target, peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                notifyPeerMapUpdated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 24520
diff changeset
   373
    void unregisterPeer(final Object target, final Object peer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if (peerMap.get(target) == peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    peerMap.remove(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    notifyPeerMapUpdated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 24520
diff changeset
   384
    Object getPeer(final Object target) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        synchronized (activationLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            synchronized (mainLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                return peerMap.get(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 24520
diff changeset
   392
    void dumpPeers(final PlatformLogger aLog) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   393
        if (aLog.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   394
            synchronized (activationLock) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   395
                synchronized (mainLock) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   396
                    aLog.fine("Mapped peers:");
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   397
                    for (Object key : peerMap.keySet()) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   398
                        aLog.fine(key + "->" + peerMap.get(key));
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 11264
diff changeset
   399
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
} // class AWTAutoShutdown