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