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