src/java.desktop/share/classes/java/awt/EventQueue.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 47971 75686e8da573
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47971
diff changeset
     2
 * Copyright (c) 1996, 2018, 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;
13541
5626307c2f86 7177144: [macosx] Drag and drop not working (regression in 7u6)
kizune
parents: 10583
diff changeset
    39
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
    40
import sun.awt.*;
13541
5626307c2f86 7177144: [macosx] Drag and drop not working (regression in 7u6)
kizune
parents: 10583
diff changeset
    41
import sun.awt.dnd.SunDropTargetEvent;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3084
diff changeset
    42
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    44
import java.util.concurrent.locks.Condition;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    45
import java.util.concurrent.locks.Lock;
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 9035
diff changeset
    46
import java.util.concurrent.atomic.AtomicInteger;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
    47
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    48
import java.security.AccessControlContext;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    49
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47971
diff changeset
    50
import jdk.internal.access.SharedSecrets;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47971
diff changeset
    51
import jdk.internal.access.JavaSecurityAccess;
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
    52
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    54
 * {@code EventQueue} is a platform-independent class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that queues events, both from the underlying peer classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * and from trusted application classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * It encapsulates asynchronous event dispatch machinery which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * extracts events from the queue and dispatches them by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * {@link #dispatchEvent(AWTEvent) dispatchEvent(AWTEvent)} method
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    61
 * on this {@code EventQueue} with the event to be dispatched
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * as an argument.  The particular behavior of this machinery is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * implementation-dependent.  The only requirements are that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * which were actually enqueued to this queue (note that events
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    65
 * being posted to the {@code EventQueue} can be coalesced)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * are dispatched:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <dt> Sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   <dd> That is, it is not permitted that several events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *        this queue are dispatched simultaneously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   <dt> In the same order as they are enqueued.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    72
 *   <dd> That is, if {@code AWTEvent}&nbsp;A is enqueued
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    73
 *        to the {@code EventQueue} before
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    74
 *        {@code AWTEvent}&nbsp;B then event B will not be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *        dispatched before event A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Some browsers partition applets in different code bases into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * separate contexts, and establish walls between these contexts.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    80
 * In such a scenario, there will be one {@code EventQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * per context. Other browsers place all applets into the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * context, implying that there will be only a single, global
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
    83
 * {@code EventQueue} for all applets. This behavior is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * implementation-dependent.  Consult your browser's documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * For information on the threading issues of the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * machinery, see <a href="doc-files/AWTThreadIssues.html#Autoshutdown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * >AWT Threading Issues</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @author Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @since       1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public class EventQueue {
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 9035
diff changeset
    98
    private static final AtomicInteger threadInitNumber = new AtomicInteger(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int LOW_PRIORITY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int NORM_PRIORITY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int HIGH_PRIORITY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final int ULTIMATE_PRIORITY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static final int NUM_PRIORITIES = ULTIMATE_PRIORITY + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * We maintain one Queue for each priority that the EventQueue supports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * That is, the EventQueue object is actually implemented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * NUM_PRIORITIES queues and all Events on a particular internal Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * have identical priority. Events are pulled off the EventQueue starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * with the Queue of highest priority. We progress in decreasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * across all Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Queue[] queues = new Queue[NUM_PRIORITIES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The next EventQueue on the stack, or null if this EventQueue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * on the top of the stack.  If nextQueue is non-null, requests to post
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * an event are forwarded to nextQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private EventQueue nextQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The previous EventQueue on the stack, or null if this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * "base" EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private EventQueue previousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   130
    /*
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   131
     * 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
   132
     * all the EventQueues from the AppContext. Synchronization on any particular
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   133
     * 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
   134
     */
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   135
    private final Lock pushPopLock;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   136
    private final Condition pushPopCond;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   137
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   138
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   139
     * 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
   140
     push/pop is performed
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   141
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32675
diff changeset
   142
    private static final Runnable dummyRunnable = new Runnable() {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   143
        public void run() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   144
        }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   145
    };
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private EventDispatchThread dispatchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private final ThreadGroup threadGroup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private final ClassLoader classLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The time stamp of the last dispatched InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private long mostRecentEventTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
   159
    /*
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
   160
     * The time stamp of the last KeyEvent .
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
   161
     */
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
   162
    private long mostRecentKeyEventTime = System.currentTimeMillis();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * The modifiers field of the current event, if the current event is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
15994
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 13775
diff changeset
   168
    private WeakReference<AWTEvent> currentEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Non-zero if a thread is waiting in getNextEvent(int) for an event of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * a particular ID to be posted to the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 9035
diff changeset
   174
    private volatile int waitForID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   176
    /*
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   177
     * AppContext corresponding to the queue.
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   178
     */
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   179
    private final AppContext appContext;
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   180
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 9035
diff changeset
   181
    private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   183
    private FwDispatcher fwDispatcher;
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   184
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   185
    private static volatile PlatformLogger eventLog;
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   186
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   187
    private static final PlatformLogger getEventLog() {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   188
        if(eventLog == null) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   189
            eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   190
        }
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   191
        return eventLog;
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   192
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
47229
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   194
    private static boolean fxAppThreadIsDispatchThread;
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   195
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   196
    static {
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   197
        AWTAccessor.setEventQueueAccessor(
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   198
            new AWTAccessor.EventQueueAccessor() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   199
                public Thread getDispatchThread(EventQueue eventQueue) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   200
                    return eventQueue.getDispatchThread();
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   201
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   202
                public boolean isDispatchThreadImpl(EventQueue eventQueue) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   203
                    return eventQueue.isDispatchThreadImpl();
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   204
                }
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   205
                public void removeSourceEvents(EventQueue eventQueue,
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   206
                                               Object source,
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   207
                                               boolean removeAllEvents)
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   208
                {
13604
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   209
                    eventQueue.removeSourceEvents(source, removeAllEvents);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   210
                }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   211
                public boolean noEvents(EventQueue eventQueue) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   212
                    return eventQueue.noEvents();
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   213
                }
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   214
                public void wakeup(EventQueue eventQueue, boolean isShutdown) {
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   215
                    eventQueue.wakeup(isShutdown);
31089af1a447 7163201: Simplify toolkit internals references
bagiras
parents: 10583
diff changeset
   216
                }
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   217
                public void invokeAndWait(Object source, Runnable r)
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   218
                    throws InterruptedException, InvocationTargetException
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   219
                {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   220
                    EventQueue.invokeAndWait(source, r);
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
   221
                }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   222
                public void setFwDispatcher(EventQueue eventQueue,
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   223
                                            FwDispatcher dispatcher) {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   224
                    eventQueue.setFwDispatcher(dispatcher);
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   225
                }
25133
a327a33a1e0a 8041990: [macosx] Language specific keys does not work in applets when opened outside the browser
pchelko
parents: 23900
diff changeset
   226
a327a33a1e0a 8041990: [macosx] Language specific keys does not work in applets when opened outside the browser
pchelko
parents: 23900
diff changeset
   227
                @Override
a327a33a1e0a 8041990: [macosx] Language specific keys does not work in applets when opened outside the browser
pchelko
parents: 23900
diff changeset
   228
                public long getMostRecentEventTime(EventQueue eventQueue) {
a327a33a1e0a 8041990: [macosx] Language specific keys does not work in applets when opened outside the browser
pchelko
parents: 23900
diff changeset
   229
                    return eventQueue.getMostRecentEventTimeImpl();
a327a33a1e0a 8041990: [macosx] Language specific keys does not work in applets when opened outside the browser
pchelko
parents: 23900
diff changeset
   230
                }
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   231
            });
47229
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   232
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   233
            public Object run() {
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   234
                fxAppThreadIsDispatchThread =
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   235
                        "true".equals(System.getProperty("javafx.embed.singleThread"));
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   236
                return null;
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   237
            }
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   238
        });
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   239
    }
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1247
diff changeset
   240
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 25133
diff changeset
   241
    /**
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 25133
diff changeset
   242
     * Initializes a new instance of {@code EventQueue}.
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 25133
diff changeset
   243
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public EventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            queues[i] = new Queue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * NOTE: if you ever have to start the associated event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * thread at this point, be aware of the following problem:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * If this EventQueue instance is created in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * SunToolkit.createNewAppContext() the started dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * may call AppContext.getAppContext() before createNewAppContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * completes thus causing mess in thread group to appcontext mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   256
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   257
        appContext = AppContext.getAppContext();
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   258
        pushPopLock = (Lock)appContext.get(AppContext.EVENT_QUEUE_LOCK_KEY);
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   259
        pushPopCond = (Condition)appContext.get(AppContext.EVENT_QUEUE_COND_KEY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   263
     * Posts a 1.1-style event to the {@code EventQueue}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * If there is an existing event on the queue with the same ID
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   265
     * and event source, the source {@code Component}'s
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   266
     * {@code coalesceEvents} method will be called.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   268
     * @param theEvent an instance of {@code java.awt.AWTEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          or a subclass of it
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   270
     * @throws NullPointerException if {@code theEvent} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void postEvent(AWTEvent theEvent) {
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   273
        SunToolkit.flushPendingEvents(appContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        postEventPrivate(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   278
     * Posts a 1.1-style event to the {@code EventQueue}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * If there is an existing event on the queue with the same ID
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   280
     * and event source, the source {@code Component}'s
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   281
     * {@code coalesceEvents} method will be called.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   283
     * @param theEvent an instance of {@code java.awt.AWTEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
47971
75686e8da573 8190228: Remove redundant modifiers in java.desktop module.
ssadetsky
parents: 47829
diff changeset
   286
    private void postEventPrivate(AWTEvent theEvent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        theEvent.isPosted = true;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   288
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   289
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   290
            if (nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   291
                // 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
   292
                nextQueue.postEventPrivate(theEvent);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   293
                return;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   294
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   295
            if (dispatchThread == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (theEvent.getSource() == AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            postEvent(theEvent, getPriority(theEvent));
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   303
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   304
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    private static int getPriority(AWTEvent theEvent) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   309
        if (theEvent instanceof PeerEvent) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   310
            PeerEvent peerEvent = (PeerEvent)theEvent;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   311
            if ((peerEvent.getFlags() & PeerEvent.ULTIMATE_PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   312
                return ULTIMATE_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   313
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   314
            if ((peerEvent.getFlags() & PeerEvent.PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   315
                return HIGH_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   316
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   317
            if ((peerEvent.getFlags() & PeerEvent.LOW_PRIORITY_EVENT) != 0) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   318
                return LOW_PRIORITY;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   319
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int id = theEvent.getID();
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   322
        if ((id >= PaintEvent.PAINT_FIRST) && (id <= PaintEvent.PAINT_LAST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return LOW_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return NORM_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Posts the event to the internal Queue of specified priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * coalescing as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   332
     * @param theEvent an instance of {@code java.awt.AWTEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param priority  the desired priority of the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private void postEvent(AWTEvent theEvent, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (coalesceEvent(theEvent, priority)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        EventQueueItem newItem = new EventQueueItem(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        cacheEQItem(newItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        boolean notifyID = (theEvent.getID() == this.waitForID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (queues[priority].head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            boolean shouldNotify = noEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            queues[priority].head = queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (shouldNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (theEvent.getSource() != AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   355
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } else if (notifyID) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   357
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // The event was not coalesced or has non-Component source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // Insert it at the end of the appropriate Queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            queues[priority].tail.next = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (notifyID) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   365
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private boolean coalescePaintEvent(PaintEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        ComponentPeer sourcePeer = ((Component)e.getSource()).peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (sourcePeer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            sourcePeer.coalescePaintEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            PaintEvent merged = mergePaintEvents(e, (PaintEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (merged != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                cache[index].event = merged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private PaintEvent mergePaintEvents(PaintEvent a, PaintEvent b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Rectangle aRect = a.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        Rectangle bRect = b.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (bRect.contains(aRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (aRect.contains(bRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private boolean coalesceMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private boolean coalescePeerEvent(PeerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            e = e.coalesceEvents((PeerEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                cache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Should avoid of calling this method by any means
47020
2c55106dc37b 8186851: fix misspellings of "dependent" and "independent" in the JDK repo
smarks
parents: 45648
diff changeset
   436
     * as it's working time is dependent on EQ length.
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   437
     * In the worst case this method alone can slow down the entire application
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * 10 times by stalling the Event processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Only here by backward compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private boolean coalesceOtherEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        Component source = (Component)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        for (EventQueueItem entry = queues[priority].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            entry != null; entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // Give Component.coalesceEvents a chance
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   448
            if (entry.event.getSource() == source && entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                AWTEvent coalescedEvent = source.coalesceEvents(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    entry.event, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (coalescedEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    entry.event = coalescedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private boolean coalesceEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (!(e.getSource() instanceof Component)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (e instanceof PeerEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return coalescePeerEvent((PeerEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // The worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (((Component)e.getSource()).isCoalescingEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            && coalesceOtherEvent(e, priority))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (e instanceof PaintEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return coalescePaintEvent((PaintEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if (e instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return coalesceMouseEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private void cacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                source.eventCache = new EventQueueItem[CACHE_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            source.eventCache[index] = entry;
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
    private void uncacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            source.eventCache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    private static final int PAINT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private static final int UPDATE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static final int MOVE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private static final int DRAG = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private static final int PEER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private static final int CACHE_LENGTH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    private static int eventToCacheIndex(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        case PaintEvent.PAINT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return PAINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        case PaintEvent.UPDATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            return UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        case MouseEvent.MOUSE_DRAGGED:
13541
5626307c2f86 7177144: [macosx] Drag and drop not working (regression in 7u6)
kizune
parents: 10583
diff changeset
   520
            // Return -1 for SunDropTargetEvent since they are usually synchronous
5626307c2f86 7177144: [macosx] Drag and drop not working (regression in 7u6)
kizune
parents: 10583
diff changeset
   521
            // and we don't want to skip them by coalescing with MouseEvent or other drag events
5626307c2f86 7177144: [macosx] Drag and drop not working (regression in 7u6)
kizune
parents: 10583
diff changeset
   522
            return e instanceof SunDropTargetEvent ? -1 : DRAG;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return e instanceof PeerEvent ? PEER : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns whether an event is pending on any of the separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return whether an event is pending on any of the separate Queues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private boolean noEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (queues[i].head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   544
     * Removes an event from the {@code EventQueue} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * returns it.  This method will block until an event has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * been posted by another thread.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   547
     * @return the next {@code AWTEvent}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @exception InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *            if any thread has interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public AWTEvent getNextEvent() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
             */
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   558
            SunToolkit.flushPendingEvents(appContext);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   559
            pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   560
            try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   561
                AWTEvent event = getNextEventPrivate();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   562
                if (event != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   563
                    return event;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                AWTAutoShutdown.getInstance().notifyThreadFree(dispatchThread);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   566
                pushPopCond.await();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   567
            } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   568
                pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   573
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   574
     * 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
   575
     */
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   576
    AWTEvent getNextEventPrivate() throws InterruptedException {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   577
        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
   578
            if (queues[i].head != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   579
                EventQueueItem entry = queues[i].head;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   580
                queues[i].head = entry.next;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   581
                if (entry.next == null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   582
                    queues[i].tail = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   583
                }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   584
                uncacheEQItem(entry);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   585
                return entry.event;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   586
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   587
        }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   588
        return null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   589
    }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   590
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    AWTEvent getNextEvent(int id) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
             */
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
   598
            SunToolkit.flushPendingEvents(appContext);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   599
            pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   600
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    for (EventQueueItem entry = queues[i].head, prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                         entry != null; prev = entry, entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    {
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   605
                        if (entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                            if (queues[i].tail == entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                            uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            return entry.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   619
                waitForID = id;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   620
                pushPopCond.await();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   621
                waitForID = 0;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   622
            } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   623
                pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   629
     * Returns the first event on the {@code EventQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * without removing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return the first event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   633
    public AWTEvent peekEvent() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   634
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   635
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   636
            for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   637
                if (queues[i].head != null) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   638
                    return queues[i].head.event;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   639
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   641
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   642
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Returns the first event with the specified id, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param id the id of the type of event desired
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   651
     * @return the first event of the specified id or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *    if there is no such event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   654
    public AWTEvent peekEvent(int id) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   655
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   656
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   657
            for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   658
                EventQueueItem q = queues[i].head;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   659
                for (; q != null; q = q.next) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   660
                    if (q.event.getID() == id) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   661
                        return q.event;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   662
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   665
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   666
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   672
    private static final JavaSecurityAccess javaSecurityAccess =
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   673
        SharedSecrets.getJavaSecurityAccess();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   674
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * Dispatches an event. The manner in which the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * dispatched depends upon the type of the event and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * type of the event's source object:
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 21263
diff changeset
   679
     *
45648
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   680
     * <table class="striped">
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   681
     * <caption>Event types, source types, and dispatch methods</caption>
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   682
     * <thead>
47498
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   683
     *   <tr>
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   684
     *     <th scope="col">Event Type
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   685
     *     <th scope="col">Source Type
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   686
     *     <th scope="col">Dispatched To
45648
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   687
     * </thead>
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   688
     * <tbody>
47498
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   689
     *   <tr>
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   690
     *     <th scope="row">ActiveEvent
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   691
     *     <td>Any
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   692
     *     <td>event.dispatch()
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   693
     *   <tr>
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   694
     *     <th scope="row">Other
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   695
     *     <td>Component
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   696
     *     <td>source.dispatchEvent(AWTEvent)
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   697
     *   <tr>
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   698
     *     <th scope="row">Other
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   699
     *     <td>MenuComponent
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   700
     *     <td>source.dispatchEvent(AWTEvent)
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   701
     *   <tr>
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   702
     *     <th scope="row">Other
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   703
     *     <td>Other
aa0344e6f39b 8184219: Fix accessibility of tables in the java.desktop module
serb
parents: 47229
diff changeset
   704
     *     <td>No action (ignored)
45648
87c997b74bb8 8180326: Update the tables in java.desktop to be HTML-5 friendly
serb
parents: 35667
diff changeset
   705
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * </table>
23717
6457189cd574 8039259: Tidy warnings cleanup for java.awt
yan
parents: 23293
diff changeset
   707
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   708
     * @param event an instance of {@code java.awt.AWTEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *          or a subclass of it
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   710
     * @throws NullPointerException if {@code event} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   713
    protected void dispatchEvent(final AWTEvent event) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   714
        final Object src = event.getSource();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   715
        final PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   716
            public Void run() {
20142
43a46d7366ab 8024839: [Unified Swing/Fx threading] don't schedule an event dispatching from the event dispatch thread
ant
parents: 18275
diff changeset
   717
                // In case fwDispatcher is installed and we're already on the
43a46d7366ab 8024839: [Unified Swing/Fx threading] don't schedule an event dispatching from the event dispatch thread
ant
parents: 18275
diff changeset
   718
                // dispatch thread (e.g. performing DefaultKeyboardFocusManager.sendMessage),
43a46d7366ab 8024839: [Unified Swing/Fx threading] don't schedule an event dispatching from the event dispatch thread
ant
parents: 18275
diff changeset
   719
                // dispatch the event straight away.
43a46d7366ab 8024839: [Unified Swing/Fx threading] don't schedule an event dispatching from the event dispatch thread
ant
parents: 18275
diff changeset
   720
                if (fwDispatcher == null || isDispatchThreadImpl()) {
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   721
                    dispatchEventImpl(event, src);
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   722
                } else {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   723
                    fwDispatcher.scheduleDispatch(new Runnable() {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   724
                        @Override
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   725
                        public void run() {
47192
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
   726
                            if (dispatchThread.filterAndCheckEvent(event)) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
   727
                                dispatchEventImpl(event, src);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
   728
                            }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   729
                        }
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   730
                    });
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   731
                }
8816
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   732
                return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   733
            }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   734
        };
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   735
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   736
        final AccessControlContext stack = AccessController.getContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   737
        final AccessControlContext srcAcc = getAccessControlContextFrom(src);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   738
        final AccessControlContext eventAcc = event.getAccessControlContext();
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   739
        if (srcAcc == null) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   740
            javaSecurityAccess.doIntersectionPrivilege(action, stack, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   741
        } else {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   742
            javaSecurityAccess.doIntersectionPrivilege(
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   743
                new PrivilegedAction<Void>() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   744
                    public Void run() {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   745
                        javaSecurityAccess.doIntersectionPrivilege(action, eventAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   746
                        return null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   747
                    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   748
                }, stack, srcAcc);
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   749
        }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   750
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   751
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   752
    private static AccessControlContext getAccessControlContextFrom(Object src) {
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   753
        return src instanceof Component ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   754
            ((Component)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   755
            src instanceof MenuComponent ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   756
                ((MenuComponent)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   757
                src instanceof TrayIcon ?
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   758
                    ((TrayIcon)src).getAccessControlContext() :
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   759
                    null;
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   760
    }
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   761
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   762
    /**
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   763
     * Called from dispatchEvent() under a correct AccessControlContext
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   764
     */
29f983feda95 6907662: System clipboard should ensure access restrictions
denis
parents: 7668
diff changeset
   765
    private void dispatchEventImpl(final AWTEvent event, final Object src) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        event.isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (event instanceof ActiveEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            // This could become the sole method of dispatching in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            setCurrentEventAndMostRecentTimeImpl(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            ((ActiveEvent)event).dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        } else if (src instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            ((Component)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            event.dispatched();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        } else if (src instanceof MenuComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            ((MenuComponent)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } else if (src instanceof TrayIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            ((TrayIcon)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } else if (src instanceof AWTAutoShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (noEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                dispatchThread.stopDispatching();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        } else {
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   783
            if (getEventLog().isLoggable(PlatformLogger.Level.FINE)) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   784
                getEventLog().fine("Unable to dispatch event: " + event);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   785
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Returns the timestamp of the most recent event that had a timestamp, and
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   791
     * that was dispatched from the {@code EventQueue} associated with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * calling thread. If an event with a timestamp is currently being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * dispatched, its timestamp will be returned. If no events have yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * been dispatched, the EventQueue's initialization time will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * returned instead.In the current version of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   796
     * the JDK, only {@code InputEvent}s,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   797
     * {@code ActionEvent}s, and {@code InvocationEvent}s have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * timestamps; however, future versions of the JDK may add timestamps to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * additional event types. Note that this method should only be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * from an application's {@link #isDispatchThread event dispatching thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * invoked from another thread, the current system time (as reported by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   803
     * {@code System.currentTimeMillis()}) will be returned instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   805
     * @return the timestamp of the last {@code InputEvent},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   806
     *         {@code ActionEvent}, or {@code InvocationEvent} to be
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   807
     *         dispatched, or {@code System.currentTimeMillis()} if this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *         method is invoked on a thread other than an event dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *         thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @see java.awt.event.InputEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @see java.awt.event.ActionEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @see java.awt.event.InvocationEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @see #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    public static long getMostRecentEventTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        return Toolkit.getEventQueue().getMostRecentEventTimeImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   820
    private long getMostRecentEventTimeImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   821
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   822
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   823
            return (Thread.currentThread() == dispatchThread)
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   824
                ? mostRecentEventTime
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   825
                : System.currentTimeMillis();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   826
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   827
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   828
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return most recent event time on all threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   834
    long getMostRecentEventTimeEx() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   835
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   836
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   837
            return mostRecentEventTime;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   838
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   839
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   840
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26749
diff changeset
   844
     * Returns the event currently being dispatched by the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   845
     * {@code EventQueue} associated with the calling thread. This is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * useful if a method needs access to the event, but was not designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * receive a reference to it as an argument. Note that this method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * only be invoked from an application's event dispatching thread. If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * method is invoked from another thread, null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @return the event currently being dispatched, or null if this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *         invoked on a thread other than an event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    public static AWTEvent getCurrentEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return Toolkit.getEventQueue().getCurrentEventImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   858
    private AWTEvent getCurrentEventImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   859
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   860
        try {
47829
51010df5ecb1 8190456: sanity/client/SwingSet/src/ComboBoxDemoTest.java failed with NPE from java.awt.EventQueue.getCurrentEventImpl()
akolarkunnu
parents: 47498
diff changeset
   861
            if (Thread.currentThread() == dispatchThread
51010df5ecb1 8190456: sanity/client/SwingSet/src/ComboBoxDemoTest.java failed with NPE from java.awt.EventQueue.getCurrentEventImpl()
akolarkunnu
parents: 47498
diff changeset
   862
                    || fxAppThreadIsDispatchThread) {
47229
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   863
                return (currentEvent != null)
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   864
                        ? currentEvent.get()
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   865
                        : null;
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
   866
            }
47829
51010df5ecb1 8190456: sanity/client/SwingSet/src/ComboBoxDemoTest.java failed with NPE from java.awt.EventQueue.getCurrentEventImpl()
akolarkunnu
parents: 47498
diff changeset
   867
            return null;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   868
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   869
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   870
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   874
     * Replaces the existing {@code EventQueue} with the specified one.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   875
     * Any pending events are transferred to the new {@code EventQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * for processing by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   878
     * @param newEventQueue an {@code EventQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *          (or subclass thereof) instance to be use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @see      java.awt.EventQueue#pop
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   881
     * @throws NullPointerException if {@code newEventQueue} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     */
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   884
    public void push(EventQueue newEventQueue) {
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   885
        if (getEventLog().isLoggable(PlatformLogger.Level.FINE)) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   886
            getEventLog().fine("EventQueue.push(" + newEventQueue + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   889
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   890
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   891
            EventQueue topQueue = this;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   892
            while (topQueue.nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   893
                topQueue = topQueue.nextQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   894
            }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   895
            if (topQueue.fwDispatcher != null) {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   896
                throw new RuntimeException("push() to queue with fwDispatcher");
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
   897
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   898
            if ((topQueue.dispatchThread != null) &&
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   899
                (topQueue.dispatchThread.getEventQueue() == this))
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   900
            {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   901
                newEventQueue.dispatchThread = topQueue.dispatchThread;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   902
                topQueue.dispatchThread.setEventQueue(newEventQueue);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   903
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            // Transfer all events forward to new EventQueue.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   906
            while (topQueue.peekEvent() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   908
                    // Use getNextEventPrivate() as it doesn't call flushPendingEvents()
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   909
                    newEventQueue.postEventPrivate(topQueue.getNextEventPrivate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                } catch (InterruptedException ie) {
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   911
                    if (getEventLog().isLoggable(PlatformLogger.Level.FINE)) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   912
                        getEventLog().fine("Interrupted push", ie);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
32675
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   917
            if (topQueue.dispatchThread != null) {
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   918
                // Wake up EDT waiting in getNextEvent(), so it can
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   919
                // pick up a new EventQueue. Post the waking event before
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   920
                // topQueue.nextQueue is assigned, otherwise the event would
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   921
                // go newEventQueue
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   922
                topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable));
1b2f76f1053d 8081485: EDT auto shutdown is broken in case of new event queue usage
anashaty
parents: 28994
diff changeset
   923
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   925
            newEventQueue.previousQueue = topQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   926
            topQueue.nextQueue = newEventQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   927
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   928
            if (appContext.get(AppContext.EVENT_QUEUE_KEY) == topQueue) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   929
                appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   930
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   932
            pushPopCond.signalAll();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   933
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   934
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   939
     * Stops dispatching events using this {@code EventQueue}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Any pending events are transferred to the previous
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   941
     * {@code EventQueue} for processing.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Warning: To avoid deadlock, do not declare this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * synchronized in a subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @exception EmptyStackException if no previous push was made
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
   947
     *  on this {@code EventQueue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @see      java.awt.EventQueue#push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    protected void pop() throws EmptyStackException {
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   952
        if (getEventLog().isLoggable(PlatformLogger.Level.FINE)) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   953
            getEventLog().fine("EventQueue.pop(" + this + ")");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   956
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   957
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   958
            EventQueue topQueue = this;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   959
            while (topQueue.nextQueue != null) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   960
                topQueue = topQueue.nextQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   962
            EventQueue prevQueue = topQueue.previousQueue;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   963
            if (prevQueue == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                throw new EmptyStackException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   966
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   967
            topQueue.previousQueue = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   968
            prevQueue.nextQueue = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            // Transfer all events back to previous EventQueue.
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   971
            while (topQueue.peekEvent() != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   973
                    prevQueue.postEventPrivate(topQueue.getNextEventPrivate());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                } catch (InterruptedException ie) {
28994
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   975
                    if (getEventLog().isLoggable(PlatformLogger.Level.FINE)) {
9c28c226d526 8065709: Deadlock in awt/logging apparently introduced by 8019623
mcherkas
parents: 28059
diff changeset
   976
                        getEventLog().fine("Interrupted pop", ie);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   980
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   981
            if ((topQueue.dispatchThread != null) &&
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   982
                (topQueue.dispatchThread.getEventQueue() == this))
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   983
            {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   984
                prevQueue.dispatchThread = topQueue.dispatchThread;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   985
                topQueue.dispatchThread.setEventQueue(prevQueue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   988
            if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   989
                appContext.put(AppContext.EVENT_QUEUE_KEY, prevQueue);
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   990
            }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   991
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   992
            // 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
   993
            // pick up a new EventQueue
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   994
            topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable));
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   995
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   996
            pushPopCond.signalAll();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   997
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
   998
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1003
     * 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
  1004
     * 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
  1005
     * {@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
  1006
     * 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
  1007
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1008
     * @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
  1009
     *                       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
  1010
     *                       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
  1011
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1012
     * @see SecondaryLoop#enter
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1013
     * @see SecondaryLoop#exit
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1014
     *
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1015
     * @since 1.7
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1016
     */
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1017
    public SecondaryLoop createSecondaryLoop() {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1018
        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
  1019
    }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1020
47192
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1021
    private class FwSecondaryLoopWrapper implements SecondaryLoop {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1022
        final private SecondaryLoop loop;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1023
        final private EventFilter filter;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1024
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1025
        public FwSecondaryLoopWrapper(SecondaryLoop loop, EventFilter filter) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1026
            this.loop = loop;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1027
            this.filter = filter;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1028
        }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1029
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1030
        @Override
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1031
        public boolean enter() {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1032
            if (filter != null) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1033
                dispatchThread.addEventFilter(filter);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1034
            }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1035
            return loop.enter();
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1036
        }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1037
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1038
        @Override
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1039
        public boolean exit() {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1040
            if (filter != null) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1041
                dispatchThread.removeEventFilter(filter);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1042
            }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1043
            return loop.exit();
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1044
        }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1045
    }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1046
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1047
    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
  1048
        pushPopLock.lock();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1049
        try {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1050
            if (nextQueue != null) {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1051
                // 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
  1052
                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
  1053
            }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1054
            if (fwDispatcher != null) {
47192
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 47183
diff changeset
  1055
                return new FwSecondaryLoopWrapper(fwDispatcher.createSecondaryLoop(), filter);
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1056
            }
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1057
            if (dispatchThread == null) {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1058
                initDispatchThread();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1059
            }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1060
            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
  1061
        } finally {
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1062
            pushPopLock.unlock();
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1063
        }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1064
    }
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1065
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
  1066
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Returns true if the calling thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * dispatch thread. Use this method to ensure that a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * task is being executed (or not being) there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Note: use the {@link #invokeLater} or {@link #invokeAndWait}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * methods to execute a task in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @return true if running in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public static boolean isDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        EventQueue eq = Toolkit.getEventQueue();
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1087
        return eq.isDispatchThreadImpl();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1088
    }
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1089
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1090
    final boolean isDispatchThreadImpl() {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1091
        EventQueue eq = this;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1092
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1093
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1094
            EventQueue next = eq.nextQueue;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1095
            while (next != null) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1096
                eq = next;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1097
                next = eq.nextQueue;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1098
            }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1099
            if (eq.fwDispatcher != null) {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1100
                return eq.fwDispatcher.isDispatchThread();
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1101
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1102
            return (Thread.currentThread() == eq.dispatchThread);
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1103
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1104
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    final void initDispatchThread() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1109
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1110
        try {
3969
6e77be3973ea 6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents: 3084
diff changeset
  1111
            if (dispatchThread == null && !threadGroup.isDestroyed() && !appContext.isDisposed()) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1112
                dispatchThread = AccessController.doPrivileged(
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1113
                    new PrivilegedAction<EventDispatchThread>() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1114
                        public EventDispatchThread run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                            EventDispatchThread t =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                                new EventDispatchThread(threadGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                                                        name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                                                        EventQueue.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                            t.setContextClassLoader(classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                            t.setPriority(Thread.NORM_PRIORITY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                            t.setDaemon(false);
23900
fd98305f0d19 8031477: [macosx] Loading AWT native library fails
pchelko
parents: 23717
diff changeset
  1122
                            AWTAutoShutdown.getInstance().notifyThreadBusy(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                        }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1125
                    }
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1126
                );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                dispatchThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1129
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1130
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 21246
diff changeset
  1134
    final void detachDispatchThread(EventDispatchThread edt) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1135
        /*
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  1136
         * Minimize discard possibility for non-posted events
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  1137
         */
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
  1138
        SunToolkit.flushPendingEvents(appContext);
13775
3ab536cab1d1 7186109: Simplify lock machinery for PostEventQueue & EventQueue
bagiras
parents: 13656
diff changeset
  1139
        /*
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1140
         * This synchronized block is to secure that the event dispatch
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1141
         * 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
  1142
         * associated event queue. It is important because we notify
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1143
         * 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
  1144
         * to its queue, so the EventQueue.dispatchThread reference must
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1145
         * be valid at that point.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1146
         */
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1147
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1148
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1149
            if (edt == dispatchThread) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1150
                dispatchThread = null;
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1151
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
  1152
            AWTAutoShutdown.getInstance().notifyThreadFree(edt);
23293
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1153
            /*
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1154
             * Event was posted after EDT events pumping had stopped, so start
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1155
             * another EDT to handle this event
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1156
             */
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1157
            if (peekEvent() != null) {
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1158
                initDispatchThread();
94f894482549 8031694: [macosx] TwentyThousandTest test intermittently hangs
bagiras
parents: 21957
diff changeset
  1159
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1160
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1161
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1162
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /*
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1166
     * Gets the {@code EventDispatchThread} for this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1167
     * {@code EventQueue}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @return the event dispatch thread associated with this event queue
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1169
     *         or {@code null} if this event queue doesn't have a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *         working thread associated with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @see    java.awt.EventQueue#initDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @see    java.awt.EventQueue#detachDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    final EventDispatchThread getDispatchThread() {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1175
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1176
        try {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1177
            return dispatchThread;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1178
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1179
            pushPopLock.unlock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1180
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Removes any pending events for the specified source object.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1185
     * If removeAllEvents parameter is {@code true} then all
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * events for the specified source object are removed, if it
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1187
     * is {@code false} then {@code SequencedEvent}, {@code SentEvent},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1188
     * {@code FocusEvent}, {@code WindowEvent}, {@code KeyEvent},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1189
     * and {@code InputMethodEvent} are kept in the queue, but all other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * events are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * This method is normally called by the source's
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1193
     * {@code removeNotify} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    final void removeSourceEvents(Object source, boolean removeAllEvents) {
18231
2948d734293d 8011695: [tck-red] Application can not be run, the Security Warning dialog is gray.
leonidr
parents: 16126
diff changeset
  1196
        SunToolkit.flushPendingEvents(appContext);
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1197
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1198
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                EventQueueItem entry = queues[i].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                EventQueueItem prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                while (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                    if ((entry.event.getSource() == source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                        && (removeAllEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                            || ! (entry.event instanceof SequencedEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                                  || entry.event instanceof SentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                  || entry.event instanceof FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                                  || entry.event instanceof WindowEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                  || entry.event instanceof KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                  || entry.event instanceof InputMethodEvent)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                        if (entry.event instanceof SequencedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                            ((SequencedEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                        if (entry.event instanceof SentEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                            ((SentEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                        }
21246
9d1752954b0e 8025588: [macosx] Frozen AppKit thread in 7u40
pchelko
parents: 20451
diff changeset
  1218
                        if (entry.event instanceof InvocationEvent) {
9d1752954b0e 8025588: [macosx] Frozen AppKit thread in 7u40
pchelko
parents: 20451
diff changeset
  1219
                            AWTAccessor.getInvocationEventAccessor()
9d1752954b0e 8025588: [macosx] Frozen AppKit thread in 7u40
pchelko
parents: 20451
diff changeset
  1220
                                    .dispose((InvocationEvent)entry.event);
9d1752954b0e 8025588: [macosx] Frozen AppKit thread in 7u40
pchelko
parents: 20451
diff changeset
  1221
                        }
47183
03c1b9f44ba2 8186263: The SunDropTargetEvent sometimes is not dispatched
serb
parents: 45648
diff changeset
  1222
                        if (entry.event instanceof SunDropTargetEvent) {
03c1b9f44ba2 8186263: The SunDropTargetEvent sometimes is not dispatched
serb
parents: 45648
diff changeset
  1223
                            ((SunDropTargetEvent)entry.event).dispose();
03c1b9f44ba2 8186263: The SunDropTargetEvent sometimes is not dispatched
serb
parents: 45648
diff changeset
  1224
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                            queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                            prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                        uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                        prev = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1238
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1239
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1243
    synchronized long getMostRecentKeyEventTime() {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1244
        pushPopLock.lock();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1245
        try {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1246
            return mostRecentKeyEventTime;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1247
        } finally {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1248
            pushPopLock.unlock();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1249
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1250
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1251
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    static void setCurrentEventAndMostRecentTime(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1255
    private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1256
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1257
        try {
47229
7d4896d180e7 8156042: Modifiers of swing ActionEvent does not work when "-Djavafx.embed.singleThread=true"
psadhukhan
parents: 47216
diff changeset
  1258
            if (!fxAppThreadIsDispatchThread && Thread.currentThread() != dispatchThread) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1259
                return;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1260
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
15994
5c8a3d840366 8007295: Reduce number of warnings in awt classes
mcherkas
parents: 13775
diff changeset
  1262
            currentEvent = new WeakReference<>(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1264
            // This series of 'instanceof' checks should be replaced with a
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1265
            // polymorphic type (for example, an interface which declares a
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1266
            // getWhen() method). However, this would require us to make such
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1267
            // 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
  1268
            // have been frowned upon. So for now, we hack.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1269
            //
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1270
            // 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
  1271
            // will no longer be an issue.
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1272
            long mostRecentEventTime2 = Long.MIN_VALUE;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1273
            if (e instanceof InputEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1274
                InputEvent ie = (InputEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1275
                mostRecentEventTime2 = ie.getWhen();
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1276
                if (e instanceof KeyEvent) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1277
                    mostRecentKeyEventTime = ie.getWhen();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 13541
diff changeset
  1278
                }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1279
            } else if (e instanceof InputMethodEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1280
                InputMethodEvent ime = (InputMethodEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1281
                mostRecentEventTime2 = ime.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1282
            } else if (e instanceof ActionEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1283
                ActionEvent ae = (ActionEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1284
                mostRecentEventTime2 = ae.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1285
            } else if (e instanceof InvocationEvent) {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1286
                InvocationEvent ie = (InvocationEvent)e;
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1287
                mostRecentEventTime2 = ie.getWhen();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1288
            }
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1289
            mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1290
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1291
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1296
     * Causes {@code runnable} to have its {@code run}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1301
     * @param runnable  the {@code Runnable} whose {@code run}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     *                  asynchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    public static void invokeLater(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1317
     * Causes {@code runnable} to have its {@code run}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * The call blocks until this has happened.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * will throw an Error if called from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * {@link #isDispatchThread event dispatcher thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1325
     * @param runnable  the {@code Runnable} whose {@code run}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *                  synchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @exception       InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *                  interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * @exception       InvocationTargetException  if an throwable is thrown
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32874
diff changeset
  1333
     *                  when running {@code runnable}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    public static void invokeAndWait(Runnable runnable)
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1340
        throws InterruptedException, InvocationTargetException
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1341
    {
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1342
        invokeAndWait(Toolkit.getDefaultToolkit(), runnable);
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1343
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1345
    static void invokeAndWait(Object source, Runnable runnable)
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1346
        throws InterruptedException, InvocationTargetException
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1347
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        class AWTInvocationLock {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        Object lock = new AWTInvocationLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        InvocationEvent event =
16092
129d7d8a7399 7192977: Issue in toolkit thread
bagiras
parents: 13775
diff changeset
  1356
            new InvocationEvent(source, runnable, lock, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            Toolkit.getEventQueue().postEvent(event);
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1360
            while (!event.isDispatched()) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1361
                lock.wait();
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 3972
diff changeset
  1362
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        Throwable eventThrowable = event.getThrowable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        if (eventThrowable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            throw new InvocationTargetException(eventThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Called from PostEventQueue.postEvent to notify that a new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * appeared. First it proceeds to the EventQueue on the top of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * stack, then notifies the associated dispatch thread if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * or starts a new one otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    private void wakeup(boolean isShutdown) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1378
        pushPopLock.lock();
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1379
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                // Forward call to the top of EventQueue stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                nextQueue.wakeup(isShutdown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            } else if (dispatchThread != null) {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1384
                pushPopCond.signalAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            } else if (!isShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1388
        } finally {
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 4264
diff changeset
  1389
            pushPopLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    }
17899
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1392
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1393
    // The method is used by AWTAccessor for javafx/AWT single threaded mode.
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1394
    private void setFwDispatcher(FwDispatcher dispatcher) {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1395
        if (nextQueue != null) {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1396
            nextQueue.setFwDispatcher(dispatcher);
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1397
        } else {
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1398
            fwDispatcher = dispatcher;
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1399
        }
d3a864bdee7d 8015477: Support single threaded AWT/FX mode.
pchelko
parents: 16126
diff changeset
  1400
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
 * The Queue object holds pointers to the beginning and end of one internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
 * queue. An EventQueue object is composed of multiple internal Queues, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
 * for each priority supported by the EventQueue. All Events on a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
 * internal Queue have identical priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
class Queue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    EventQueueItem head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    EventQueueItem tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
}