jdk/src/share/classes/java/awt/EventQueue.java
author denis
Thu, 17 Mar 2011 17:16:35 -0700
changeset 8816 29f983feda95
parent 7668 d4a77089c587
child 9035 1255eb81cc2f
permissions -rw-r--r--
6907662: System clipboard should ensure access restrictions Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6484
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4365
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: 4365
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: 4365
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4365
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4365
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 java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.EmptyStackException;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
    39
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.AWTAutoShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.awt.PeerEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.awt.SunToolkit;
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
    45
import sun.awt.EventQueueItem;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
    46
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    48
import java.util.concurrent.locks.Condition;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    49
import java.util.concurrent.locks.Lock;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    50
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    51
import java.security.AccessControlContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    52
import java.security.ProtectionDomain;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    53
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    54
import sun.misc.SharedSecrets;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    55
import sun.misc.JavaSecurityAccess;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <code>EventQueue</code> is a platform-independent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * that queues events, both from the underlying peer classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and from trusted application classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * It encapsulates asynchronous event dispatch machinery which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * extracts events from the queue and dispatches them by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@link #dispatchEvent(AWTEvent) dispatchEvent(AWTEvent)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * on this <code>EventQueue</code> with the event to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * as an argument.  The particular behavior of this machinery is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * implementation-dependent.  The only requirements are that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * which were actually enqueued to this queue (note that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * being posted to the <code>EventQueue</code> can be coalesced)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * are dispatched:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   <dt> Sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   <dd> That is, it is not permitted that several events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *        this queue are dispatched simultaneously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   <dt> In the same order as they are enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   <dd> That is, if <code>AWTEvent</code>&nbsp;A is enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *        to the <code>EventQueue</code> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *        <code>AWTEvent</code>&nbsp;B then event B will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *        dispatched before event A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * Some browsers partition applets in different code bases into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * separate contexts, and establish walls between these contexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * In such a scenario, there will be one <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * per context. Other browsers place all applets into the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * context, implying that there will be only a single, global
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>EventQueue</code> for all applets. This behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * implementation-dependent.  Consult your browser's documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * For information on the threading issues of the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * machinery, see <a href="doc-files/AWTThreadIssues.html#Autoshutdown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * >AWT Threading Issues</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @since       1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
public class EventQueue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // From Thread.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static int threadInitNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static synchronized int nextThreadNum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return threadInitNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final int LOW_PRIORITY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final int NORM_PRIORITY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static final int HIGH_PRIORITY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static final int ULTIMATE_PRIORITY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final int NUM_PRIORITIES = ULTIMATE_PRIORITY + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * We maintain one Queue for each priority that the EventQueue supports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * That is, the EventQueue object is actually implemented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * NUM_PRIORITIES queues and all Events on a particular internal Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * have identical priority. Events are pulled off the EventQueue starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * with the Queue of highest priority. We progress in decreasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * across all Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private Queue[] queues = new Queue[NUM_PRIORITIES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The next EventQueue on the stack, or null if this EventQueue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * on the top of the stack.  If nextQueue is non-null, requests to post
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * an event are forwarded to nextQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private EventQueue nextQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The previous EventQueue on the stack, or null if this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * "base" EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private EventQueue previousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   139
    /*
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   140
     * A single lock to synchronize the push()/pop() and related operations with
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   141
     * all the EventQueues from the AppContext. Synchronization on any particular
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   142
     * event queue(s) is not enough: we should lock the whole stack.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   143
     */
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   144
    private final Lock pushPopLock;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   145
    private final Condition pushPopCond;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   146
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   147
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   148
     * Dummy runnable to wake up EDT from getNextEvent() after
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   149
     push/pop is performed
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   150
     */
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   151
    private final static Runnable dummyRunnable = new Runnable() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   152
        public void run() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   153
        }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   154
    };
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private EventDispatchThread dispatchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private final ThreadGroup threadGroup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private final ClassLoader classLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The time stamp of the last dispatched InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private long mostRecentEventTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The modifiers field of the current event, if the current event is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private WeakReference currentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Non-zero if a thread is waiting in getNextEvent(int) for an event of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * a particular ID to be posted to the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private int waitForID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private final String name = "AWT-EventQueue-" + nextThreadNum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   182
    private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   184
    static {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   185
        AWTAccessor.setEventQueueAccessor(
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   186
            new AWTAccessor.EventQueueAccessor() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   187
                public Thread getDispatchThread(EventQueue eventQueue) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   188
                    return eventQueue.getDispatchThread();
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   189
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   190
                public boolean isDispatchThreadImpl(EventQueue eventQueue) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   191
                    return eventQueue.isDispatchThreadImpl();
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   192
                }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   193
            });
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   194
    }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   195
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public EventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            queues[i] = new Queue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * NOTE: if you ever have to start the associated event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * thread at this point, be aware of the following problem:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * If this EventQueue instance is created in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * SunToolkit.createNewAppContext() the started dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * may call AppContext.getAppContext() before createNewAppContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * completes thus causing mess in thread group to appcontext mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   208
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   209
        pushPopLock = (Lock)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_LOCK_KEY);
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   210
        pushPopCond = (Condition)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_COND_KEY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Posts a 1.1-style event to the <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * If there is an existing event on the queue with the same ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * and event source, the source <code>Component</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <code>coalesceEvents</code> method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @throws NullPointerException if <code>theEvent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void postEvent(AWTEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        SunToolkit.flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        postEventPrivate(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Posts a 1.1-style event to the <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * If there is an existing event on the queue with the same ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * and event source, the source <code>Component</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <code>coalesceEvents</code> method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   237
    private final void postEventPrivate(AWTEvent theEvent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        theEvent.isPosted = true;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   239
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   240
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   241
            if (nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   242
                // Forward the event to the top of EventQueue stack
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   243
                nextQueue.postEventPrivate(theEvent);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   244
                return;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   245
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   246
            if (dispatchThread == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (theEvent.getSource() == AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            postEvent(theEvent, getPriority(theEvent));
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   254
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   255
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private static int getPriority(AWTEvent theEvent) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   260
        if (theEvent instanceof PeerEvent) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   261
            PeerEvent peerEvent = (PeerEvent)theEvent;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   262
            if ((peerEvent.getFlags() & PeerEvent.ULTIMATE_PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   263
                return ULTIMATE_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   264
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   265
            if ((peerEvent.getFlags() & PeerEvent.PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   266
                return HIGH_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   267
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   268
            if ((peerEvent.getFlags() & PeerEvent.LOW_PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   269
                return LOW_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   270
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int id = theEvent.getID();
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   273
        if ((id >= PaintEvent.PAINT_FIRST) && (id <= PaintEvent.PAINT_LAST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return LOW_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return NORM_PRIORITY;
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
     * Posts the event to the internal Queue of specified priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * coalescing as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param priority  the desired priority of the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private void postEvent(AWTEvent theEvent, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (coalesceEvent(theEvent, priority)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        EventQueueItem newItem = new EventQueueItem(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        cacheEQItem(newItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        boolean notifyID = (theEvent.getID() == this.waitForID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (queues[priority].head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            boolean shouldNotify = noEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            queues[priority].head = queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (shouldNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (theEvent.getSource() != AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   306
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else if (notifyID) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   308
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // The event was not coalesced or has non-Component source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // Insert it at the end of the appropriate Queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            queues[priority].tail.next = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (notifyID) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   316
                pushPopCond.signalAll();
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private boolean coalescePaintEvent(PaintEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        ComponentPeer sourcePeer = ((Component)e.getSource()).peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (sourcePeer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            sourcePeer.coalescePaintEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            PaintEvent merged = mergePaintEvents(e, (PaintEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (merged != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                cache[index].event = merged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private PaintEvent mergePaintEvents(PaintEvent a, PaintEvent b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Rectangle aRect = a.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Rectangle bRect = b.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (bRect.contains(aRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (aRect.contains(bRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private boolean coalesceMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private boolean coalescePeerEvent(PeerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            e = e.coalesceEvents((PeerEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                cache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return false;
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
     * Should avoid of calling this method by any means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * as it's working time is dependant on EQ length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * In the wors case this method alone can slow down the entire application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * 10 times by stalling the Event processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Only here by backward compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private boolean coalesceOtherEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        Component source = (Component)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        for (EventQueueItem entry = queues[priority].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            entry != null; entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // Give Component.coalesceEvents a chance
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   399
            if (entry.event.getSource() == source && entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                AWTEvent coalescedEvent = source.coalesceEvents(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    entry.event, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (coalescedEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    entry.event = coalescedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private boolean coalesceEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (!(e.getSource() instanceof Component)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (e instanceof PeerEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return coalescePeerEvent((PeerEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // The worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (((Component)e.getSource()).isCoalescingEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            && coalesceOtherEvent(e, priority))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (e instanceof PaintEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return coalescePaintEvent((PaintEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (e instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return coalesceMouseEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    private void cacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                source.eventCache = new EventQueueItem[CACHE_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            source.eventCache[index] = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private void uncacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            source.eventCache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private static final int PAINT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static final int UPDATE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private static final int MOVE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private static final int DRAG = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    private static final int PEER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private static final int CACHE_LENGTH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private static int eventToCacheIndex(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        case PaintEvent.PAINT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return PAINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        case PaintEvent.UPDATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return DRAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return e instanceof PeerEvent ? PEER : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Returns whether an event is pending on any of the separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @return whether an event is pending on any of the separate Queues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private boolean noEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (queues[i].head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Removes an event from the <code>EventQueue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * returns it.  This method will block until an event has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * been posted by another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @return the next <code>AWTEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @exception InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *            if any thread has interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public AWTEvent getNextEvent() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            SunToolkit.flushPendingEvents();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   508
            pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   509
            try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   510
                AWTEvent event = getNextEventPrivate();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   511
                if (event != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   512
                    return event;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                AWTAutoShutdown.getInstance().notifyThreadFree(dispatchThread);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   515
                pushPopCond.await();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   516
            } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   517
                pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   522
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   523
     * Must be called under the lock. Doesn't call flushPendingEvents()
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   524
     */
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   525
    AWTEvent getNextEventPrivate() throws InterruptedException {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   526
        for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   527
            if (queues[i].head != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   528
                EventQueueItem entry = queues[i].head;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   529
                queues[i].head = entry.next;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   530
                if (entry.next == null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   531
                    queues[i].tail = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   532
                }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   533
                uncacheEQItem(entry);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   534
                return entry.event;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   535
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   536
        }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   537
        return null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   538
    }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   539
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    AWTEvent getNextEvent(int id) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            SunToolkit.flushPendingEvents();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   548
            pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   549
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    for (EventQueueItem entry = queues[i].head, prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                         entry != null; prev = entry, entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    {
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   554
                        if (entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                            if (queues[i].tail == entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                            uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            return entry.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   568
                waitForID = id;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   569
                pushPopCond.await();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   570
                waitForID = 0;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   571
            } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   572
                pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the first event on the <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * without removing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @return the first event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   582
    public AWTEvent peekEvent() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   583
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   584
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   585
            for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   586
                if (queues[i].head != null) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   587
                    return queues[i].head.event;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   588
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   590
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   591
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Returns the first event with the specified id, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param id the id of the type of event desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return the first event of the specified id or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *    if there is no such event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   603
    public AWTEvent peekEvent(int id) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   604
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   605
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   606
            for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   607
                EventQueueItem q = queues[i].head;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   608
                for (; q != null; q = q.next) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   609
                    if (q.event.getID() == id) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   610
                        return q.event;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   611
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   614
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   615
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   621
    private static final JavaSecurityAccess javaSecurityAccess =
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   622
        SharedSecrets.getJavaSecurityAccess();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   623
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Dispatches an event. The manner in which the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * dispatched depends upon the type of the event and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * type of the event's source object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <table border=1 summary="Event types, source types, and dispatch methods">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *     <th>Event Type</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *     <th>Source Type</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *     <th>Dispatched To</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *     <td>ActiveEvent</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *     <td>Any</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *     <td>event.dispatch()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *     <td>Component</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *     <td>source.dispatchEvent(AWTEvent)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *     <td>MenuComponent</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *     <td>source.dispatchEvent(AWTEvent)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *     <td>No action (ignored)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param event an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @throws NullPointerException if <code>event</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   662
    protected void dispatchEvent(final AWTEvent event) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   663
        final Object src = event.getSource();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   664
        final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   665
            public Void run() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   666
                dispatchEventImpl(event, src);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   667
                return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   668
            }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   669
        };
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   670
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   671
        final AccessControlContext stack = AccessController.getContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   672
        final AccessControlContext srcAcc = getAccessControlContextFrom(src);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   673
        final AccessControlContext eventAcc = event.getAccessControlContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   674
        if (srcAcc == null) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   675
            javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   676
        } else {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   677
            javaSecurityAccess.doIntersectionPrivilege(
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   678
                new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   679
                    public Void run() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   680
                        javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   681
                        return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   682
                    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   683
                }, stack, srcAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   684
        }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   685
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   686
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   687
    private static AccessControlContext getAccessControlContextFrom(Object src) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   688
        return src instanceof Component ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   689
            ((Component)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   690
            src instanceof MenuComponent ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   691
                ((MenuComponent)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   692
                src instanceof TrayIcon ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   693
                    ((TrayIcon)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   694
                    null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   695
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   696
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   697
    /**
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   698
     * Called from dispatchEvent() under a correct AccessControlContext
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   699
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   700
    private void dispatchEventImpl(final AWTEvent event, final Object src) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        event.isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (event instanceof ActiveEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            // This could become the sole method of dispatching in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            setCurrentEventAndMostRecentTimeImpl(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            ((ActiveEvent)event).dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else if (src instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            ((Component)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            event.dispatched();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        } else if (src instanceof MenuComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            ((MenuComponent)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        } else if (src instanceof TrayIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            ((TrayIcon)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        } else if (src instanceof AWTAutoShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (noEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                dispatchThread.stopDispatching();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        } else {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   718
            if (eventLog.isLoggable(PlatformLogger.FINE)) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   719
                eventLog.fine("Unable to dispatch event: " + event);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   720
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Returns the timestamp of the most recent event that had a timestamp, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * that was dispatched from the <code>EventQueue</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * calling thread. If an event with a timestamp is currently being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * dispatched, its timestamp will be returned. If no events have yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * been dispatched, the EventQueue's initialization time will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * returned instead.In the current version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * the JDK, only <code>InputEvent</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <code>ActionEvent</code>s, and <code>InvocationEvent</code>s have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * timestamps; however, future versions of the JDK may add timestamps to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * additional event types. Note that this method should only be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * from an application's {@link #isDispatchThread event dispatching thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * invoked from another thread, the current system time (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * <code>System.currentTimeMillis()</code>) will be returned instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @return the timestamp of the last <code>InputEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *         <code>ActionEvent</code>, or <code>InvocationEvent</code> to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *         dispatched, or <code>System.currentTimeMillis()</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *         method is invoked on a thread other than an event dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *         thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @see java.awt.event.InputEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @see java.awt.event.ActionEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @see java.awt.event.InvocationEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @see #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public static long getMostRecentEventTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        return Toolkit.getEventQueue().getMostRecentEventTimeImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   755
    private long getMostRecentEventTimeImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   756
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   757
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   758
            return (Thread.currentThread() == dispatchThread)
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   759
                ? mostRecentEventTime
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   760
                : System.currentTimeMillis();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   761
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   762
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   763
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @return most recent event time on all threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   769
    long getMostRecentEventTimeEx() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   770
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   771
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   772
            return mostRecentEventTime;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   773
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   774
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   775
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Returns the the event currently being dispatched by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <code>EventQueue</code> associated with the calling thread. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * useful if a method needs access to the event, but was not designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * receive a reference to it as an argument. Note that this method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * only be invoked from an application's event dispatching thread. If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * method is invoked from another thread, null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return the event currently being dispatched, or null if this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *         invoked on a thread other than an event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public static AWTEvent getCurrentEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        return Toolkit.getEventQueue().getCurrentEventImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   793
    private AWTEvent getCurrentEventImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   794
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   795
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   796
                return (Thread.currentThread() == dispatchThread)
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   797
                ? ((AWTEvent)currentEvent.get())
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   798
                : null;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   799
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   800
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   801
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Replaces the existing <code>EventQueue</code> with the specified one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Any pending events are transferred to the new <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * for processing by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @param newEventQueue an <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *          (or subclass thereof) instance to be use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @see      java.awt.EventQueue#pop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @throws NullPointerException if <code>newEventQueue</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   815
    public void push(EventQueue newEventQueue) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   816
        if (eventLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   817
            eventLog.fine("EventQueue.push(" + newEventQueue + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   820
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   821
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   822
            EventQueue topQueue = this;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   823
            while (topQueue.nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   824
                topQueue = topQueue.nextQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   825
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   826
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   827
            if ((topQueue.dispatchThread != null) &&
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   828
                (topQueue.dispatchThread.getEventQueue() == this))
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   829
            {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   830
                newEventQueue.dispatchThread = topQueue.dispatchThread;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   831
                topQueue.dispatchThread.setEventQueue(newEventQueue);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   832
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            // Transfer all events forward to new EventQueue.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   835
            while (topQueue.peekEvent() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   837
                    // Use getNextEventPrivate() as it doesn't call flushPendingEvents()
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   838
                    newEventQueue.postEventPrivate(topQueue.getNextEventPrivate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                } catch (InterruptedException ie) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   840
                    if (eventLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   841
                        eventLog.fine("Interrupted push", ie);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   846
            // Wake up EDT waiting in getNextEvent(), so it can
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   847
            // pick up a new EventQueue. Post the waking event before
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   848
            // topQueue.nextQueue is assigned, otherwise the event would
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   849
            // go newEventQueue
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   850
            topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   852
            newEventQueue.previousQueue = topQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   853
            topQueue.nextQueue = newEventQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   854
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   855
            AppContext appContext = AppContext.getAppContext();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   856
            if (appContext.get(AppContext.EVENT_QUEUE_KEY) == topQueue) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   857
                appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   858
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   860
            pushPopCond.signalAll();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   861
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   862
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Stops dispatching events using this <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Any pending events are transferred to the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <code>EventQueue</code> for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Warning: To avoid deadlock, do not declare this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * synchronized in a subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @exception EmptyStackException if no previous push was made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *  on this <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @see      java.awt.EventQueue#push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    protected void pop() throws EmptyStackException {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   880
        if (eventLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   881
            eventLog.fine("EventQueue.pop(" + this + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   884
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   885
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   886
            EventQueue topQueue = this;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   887
            while (topQueue.nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   888
                topQueue = topQueue.nextQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   890
            EventQueue prevQueue = topQueue.previousQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   891
            if (prevQueue == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                throw new EmptyStackException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   894
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   895
            topQueue.previousQueue = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   896
            prevQueue.nextQueue = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            // Transfer all events back to previous EventQueue.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   899
            while (topQueue.peekEvent() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   901
                    prevQueue.postEventPrivate(topQueue.getNextEventPrivate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                } catch (InterruptedException ie) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   903
                    if (eventLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
   904
                        eventLog.fine("Interrupted pop", ie);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   908
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   909
            if ((topQueue.dispatchThread != null) &&
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   910
                (topQueue.dispatchThread.getEventQueue() == this))
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   911
            {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   912
                prevQueue.dispatchThread = topQueue.dispatchThread;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   913
                topQueue.dispatchThread.setEventQueue(prevQueue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   916
            AppContext appContext = AppContext.getAppContext();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   917
            if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   918
                appContext.put(AppContext.EVENT_QUEUE_KEY, prevQueue);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   919
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   920
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   921
            // Wake up EDT waiting in getNextEvent(), so it can
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   922
            // pick up a new EventQueue
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   923
            topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable));
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   924
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   925
            pushPopCond.signalAll();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   926
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   927
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   932
     * Creates a new {@code secondary loop} associated with this
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   933
     * event queue. Use the {@link SecondaryLoop#enter} and
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   934
     * {@link SecondaryLoop#exit} methods to start and stop the
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   935
     * event loop and dispatch the events from this queue.
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   936
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   937
     * @return secondaryLoop A new secondary loop object, which can
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   938
     *                       be used to launch a new nested event
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   939
     *                       loop and dispatch events from this queue
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   940
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   941
     * @see SecondaryLoop#enter
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   942
     * @see SecondaryLoop#exit
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   943
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   944
     * @since 1.7
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   945
     */
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   946
    public SecondaryLoop createSecondaryLoop() {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   947
        return createSecondaryLoop(null, null, 0);
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   948
    }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   949
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   950
    SecondaryLoop createSecondaryLoop(Conditional cond, EventFilter filter, long interval) {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   951
        pushPopLock.lock();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   952
        try {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   953
            if (nextQueue != null) {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   954
                // Forward the request to the top of EventQueue stack
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   955
                return nextQueue.createSecondaryLoop(cond, filter, interval);
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   956
            }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   957
            if (dispatchThread == null) {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   958
                initDispatchThread();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   959
            }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   960
            return new WaitDispatchSupport(dispatchThread, cond, filter, interval);
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   961
        } finally {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   962
            pushPopLock.unlock();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   963
        }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   964
    }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   965
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   966
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Returns true if the calling thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * dispatch thread. Use this method to ensure that a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * task is being executed (or not being) there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Note: use the {@link #invokeLater} or {@link #invokeAndWait}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * methods to execute a task in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @return true if running in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    public static boolean isDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        EventQueue eq = Toolkit.getEventQueue();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   988
        return eq.isDispatchThreadImpl();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   989
    }
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   990
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   991
    final boolean isDispatchThreadImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   992
        EventQueue eq = this;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   993
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   994
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   995
            EventQueue next = eq.nextQueue;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   996
            while (next != null) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   997
                eq = next;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   998
                next = eq.nextQueue;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   999
            }
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1000
            return (Thread.currentThread() == eq.dispatchThread);
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1001
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1002
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    final void initDispatchThread() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1007
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1008
        try {
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 3084
diff changeset
  1009
            AppContext appContext = AppContext.getAppContext();
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 3084
diff changeset
  1010
            if (dispatchThread == null && !threadGroup.isDestroyed() && !appContext.isDisposed()) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1011
                dispatchThread = AccessController.doPrivileged(
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1012
                    new PrivilegedAction<EventDispatchThread>() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1013
                        public EventDispatchThread run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                            EventDispatchThread t =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                                new EventDispatchThread(threadGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                                                        name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                                        EventQueue.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                            t.setContextClassLoader(classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                            t.setPriority(Thread.NORM_PRIORITY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                            t.setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1023
                    }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1024
                );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                dispatchThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1028
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1029
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1033
    final boolean detachDispatchThread(EventDispatchThread edt) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1034
        /*
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1035
         * This synchronized block is to secure that the event dispatch
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1036
         * thread won't die in the middle of posting a new event to the
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1037
         * associated event queue. It is important because we notify
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1038
         * that the event dispatch thread is busy after posting a new event
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1039
         * to its queue, so the EventQueue.dispatchThread reference must
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1040
         * be valid at that point.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1041
         */
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1042
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1043
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1044
            if (edt == dispatchThread) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1045
                /*
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1046
                 * Don't detach the thread if any events are pending. Not
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1047
                 * sure if it's a possible scenario, though.
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1048
                 *
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1049
                 * Fix for 4648733. Check both the associated java event
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1050
                 * queue and the PostEventQueue.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1051
                 */
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1052
                if ((peekEvent() != null) || !SunToolkit.isPostEventQueueEmpty()) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1053
                    return false;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1054
                }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1055
                dispatchThread = null;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1056
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1057
            AWTAutoShutdown.getInstance().notifyThreadFree(edt);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1058
            return true;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1059
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1060
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1061
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * Gets the <code>EventDispatchThread</code> for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @return the event dispatch thread associated with this event queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *         or <code>null</code> if this event queue doesn't have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *         working thread associated with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @see    java.awt.EventQueue#initDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * @see    java.awt.EventQueue#detachDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    final EventDispatchThread getDispatchThread() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1074
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1075
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1076
            return dispatchThread;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1077
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1078
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1079
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Removes any pending events for the specified source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * If removeAllEvents parameter is <code>true</code> then all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * events for the specified source object are removed, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * is <code>false</code> then <code>SequencedEvent</code>, <code>SentEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <code>FocusEvent</code>, <code>WindowEvent</code>, <code>KeyEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * and <code>InputMethodEvent</code> are kept in the queue, but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * events are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * This method is normally called by the source's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <code>removeNotify</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    final void removeSourceEvents(Object source, boolean removeAllEvents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        SunToolkit.flushPendingEvents();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1096
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1097
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                EventQueueItem entry = queues[i].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                EventQueueItem prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                while (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    if ((entry.event.getSource() == source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                        && (removeAllEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                            || ! (entry.event instanceof SequencedEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                  || entry.event instanceof SentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                                  || entry.event instanceof FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                                  || entry.event instanceof WindowEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                  || entry.event instanceof KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                  || entry.event instanceof InputMethodEvent)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        if (entry.event instanceof SequencedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                            ((SequencedEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                        if (entry.event instanceof SentEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                            ((SentEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                            queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                            prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                        uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                        prev = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1130
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1131
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    static void setCurrentEventAndMostRecentTime(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1138
    private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1139
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1140
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1141
            if (Thread.currentThread() != dispatchThread) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1142
                return;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1143
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1145
            currentEvent = new WeakReference(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1147
            // This series of 'instanceof' checks should be replaced with a
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1148
            // polymorphic type (for example, an interface which declares a
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1149
            // getWhen() method). However, this would require us to make such
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1150
            // a type public, or to place it in sun.awt. Both of these approaches
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1151
            // have been frowned upon. So for now, we hack.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1152
            //
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1153
            // In tiger, we will probably give timestamps to all events, so this
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1154
            // will no longer be an issue.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1155
            long mostRecentEventTime2 = Long.MIN_VALUE;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1156
            if (e instanceof InputEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1157
                InputEvent ie = (InputEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1158
                mostRecentEventTime2 = ie.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1159
            } else if (e instanceof InputMethodEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1160
                InputMethodEvent ime = (InputMethodEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1161
                mostRecentEventTime2 = ime.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1162
            } else if (e instanceof ActionEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1163
                ActionEvent ae = (ActionEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1164
                mostRecentEventTime2 = ae.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1165
            } else if (e instanceof InvocationEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1166
                InvocationEvent ie = (InvocationEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1167
                mostRecentEventTime2 = ie.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1168
            }
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1169
            mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1170
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1171
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * Causes <code>runnable</code> to have its <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @param runnable  the <code>Runnable</code> whose <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *                  asynchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    public static void invokeLater(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Causes <code>runnable</code> to have its <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * The call blocks until this has happened.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * will throw an Error if called from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * {@link #isDispatchThread event dispatcher thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @param runnable  the <code>Runnable</code> whose <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *                  synchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @exception       InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *                  interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @exception       InvocationTargetException  if an throwable is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *                  when running <code>runnable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    public static void invokeAndWait(Runnable runnable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
             throws InterruptedException, InvocationTargetException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        class AWTInvocationLock {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        Object lock = new AWTInvocationLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        InvocationEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                                true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            Toolkit.getEventQueue().postEvent(event);
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1235
            while (!event.isDispatched()) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1236
                lock.wait();
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1237
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        Throwable eventThrowable = event.getThrowable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        if (eventThrowable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            throw new InvocationTargetException(eventThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * Called from PostEventQueue.postEvent to notify that a new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * appeared. First it proceeds to the EventQueue on the top of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * stack, then notifies the associated dispatch thread if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * or starts a new one otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    private void wakeup(boolean isShutdown) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1253
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1254
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                // Forward call to the top of EventQueue stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                nextQueue.wakeup(isShutdown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            } else if (dispatchThread != null) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1259
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            } else if (!isShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1263
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1264
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
 * The Queue object holds pointers to the beginning and end of one internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
 * queue. An EventQueue object is composed of multiple internal Queues, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
 * for each priority supported by the EventQueue. All Events on a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
 * internal Queue have identical priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
class Queue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    EventQueueItem head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    EventQueueItem tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
}