提交 6732b96c 编写于 作者: A art

4913324: Deadlock when using two event queues

Reviewed-by: anthony, ant, dcherepanov
上级 6c6a996e
......@@ -104,11 +104,8 @@ class EventDispatchThread extends Thread {
} else {
stopEvent.dispatch();
}
synchronized (theQueue) {
if (theQueue.getDispatchThread() == this) {
theQueue.detachDispatchThread();
}
}
theQueue.detachDispatchThread(this, false);
}
public void stopDispatching() {
......@@ -142,35 +139,7 @@ class EventDispatchThread extends Thread {
}
});
} finally {
/*
* This synchronized block is to secure that the event dispatch
* thread won't die in the middle of posting a new event to the
* associated event queue. It is important because we notify
* that the event dispatch thread is busy after posting a new event
* to its queue, so the EventQueue.dispatchThread reference must
* be valid at that point.
*/
synchronized (theQueue) {
if (theQueue.getDispatchThread() == this) {
theQueue.detachDispatchThread();
}
/*
* Event dispatch thread dies in case of an uncaught exception.
* A new event dispatch thread for this queue will be started
* only if a new event is posted to it. In case if no more
* events are posted after this thread died all events that
* currently are in the queue will never be dispatched.
*/
/*
* Fix for 4648733. Check both the associated java event
* queue and the PostEventQueue.
*/
if (theQueue.peekEvent() != null ||
!SunToolkit.isPostEventQueueEmpty()) {
theQueue.initDispatchThread();
}
AWTAutoShutdown.getInstance().notifyThreadFree(this);
}
theQueue.detachDispatchThread(this, true);
}
}
......
......@@ -248,14 +248,14 @@ public final class AWTAccessor {
* An accessor for the EventQueue class
*/
public interface EventQueueAccessor {
/*
* Gets the next event queue.
*/
EventQueue getNextQueue(EventQueue eventQueue);
/*
* Gets the event dispatch thread.
*/
Thread getDispatchThread(EventQueue eventQueue);
/*
* Checks if the current thread is EDT for the given EQ.
*/
public boolean isDispatchThreadImpl(EventQueue eventQueue);
}
/*
......
......@@ -43,6 +43,9 @@ import java.util.HashSet;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import sun.util.logging.PlatformLogger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* The AppContext is a table referenced by ThreadGroup which stores
......@@ -132,10 +135,17 @@ public final class AppContext {
/* Since the contents of an AppContext are unique to each Java
* session, this class should never be serialized. */
/* The key to put()/get() the Java EventQueue into/from the AppContext.
/*
* The key to put()/get() the Java EventQueue into/from the AppContext.
*/
public static final Object EVENT_QUEUE_KEY = new StringBuffer("EventQueue");
/*
* The keys to store EventQueue push/pop lock and condition.
*/
public final static Object EVENT_QUEUE_LOCK_KEY = new StringBuilder("EventQueue.Lock");
public final static Object EVENT_QUEUE_COND_KEY = new StringBuilder("EventQueue.Condition");
/* A map of AppContexts, referenced by ThreadGroup.
*/
private static final Map<ThreadGroup, AppContext> threadGroup2appContext =
......@@ -244,6 +254,13 @@ public final class AppContext {
return Thread.currentThread().getContextClassLoader();
}
});
// Initialize push/pop lock and its condition to be used by all the
// EventQueues within this AppContext
Lock eventQueuePushPopLock = new ReentrantLock();
put(EVENT_QUEUE_LOCK_KEY, eventQueuePushPopLock);
Condition eventQueuePushPopCond = eventQueuePushPopLock.newCondition();
put(EVENT_QUEUE_COND_KEY, eventQueuePushPopCond);
}
private static final ThreadLocal<AppContext> threadAppContext =
......
......@@ -722,13 +722,7 @@ public abstract class SunToolkit extends Toolkit
EventQueue eq = (EventQueue)appContext.get(AppContext.EVENT_QUEUE_KEY);
AWTAccessor.EventQueueAccessor accessor = AWTAccessor.getEventQueueAccessor();
EventQueue next = accessor.getNextQueue(eq);
while (next != null) {
eq = next;
next = accessor.getNextQueue(eq);
}
return (Thread.currentThread() == accessor.getDispatchThread(eq));
return accessor.isDispatchThreadImpl(eq);
}
public Dimension getScreenSize() {
......
/*
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
@test
@bug 4913324
@author Oleg Sukhodolsky: area=eventqueue
@run main/timeout=30 PushPopTest
*/
import java.awt.*;
import java.awt.event.*;
import java.util.EmptyStackException;
import sun.awt.SunToolkit;
public class PushPopTest {
public static Frame frame;
public static void main(String[] args) {
frame = new Frame("");
frame.pack();
Runnable dummy = new Runnable() {
public void run() {
System.err.println("Dummy is here.");
}
};
EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
MyEventQueue1 eq1 = new MyEventQueue1();
MyEventQueue2 eq2 = new MyEventQueue2();
EventQueue.invokeLater(dummy);
seq.push(eq1);
EventQueue.invokeLater(dummy);
eq1.push(eq2);
EventQueue.invokeLater(dummy);
Runnable runnable = new Runnable() {
public void run() {
System.err.println("Dummy from SunToolkit");
}
};
InvocationEvent ie = new InvocationEvent(eq2, runnable, null, false);
System.err.println(ie);
SunToolkit.postEvent(SunToolkit.targetToAppContext(frame), ie);
eq1.pop();
frame.dispose();
}
}
class MyEventQueue1 extends EventQueue {
public void pop() throws EmptyStackException {
super.pop();
}
}
class MyEventQueue2 extends EventQueue {
protected void pop() throws EmptyStackException {
System.err.println("pop2()");
Thread.dumpStack();
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
Runnable runnable = new Runnable() {
public void run() {
System.err.println("Dummy from here");
}
};
InvocationEvent ie = new InvocationEvent(MyEventQueue2.this, runnable, null, false);
SunToolkit.postEvent(SunToolkit.targetToAppContext(PushPopTest.frame), ie);
postEvent(ie);
}
});
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException ie) {
ie.printStackTrace();
}
super.pop();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册