Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8196100: javax/swing/text/JTextComponent/5074573/bug5074573.java fails #1424

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ protected void initializeDesktopProperties() {

@Override
protected boolean syncNativeQueue(long timeout) {
if (timeout <= 0) {
return false;
}
if (SunDragSourceContextPeer.isDragDropInProgress()
|| EventQueue.isDispatchThread()) {
// The java code started the DnD, but the native drag may still not
Expand Down
120 changes: 80 additions & 40 deletions src/java.desktop/share/classes/sun/awt/SunToolkit.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. 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
Expand All @@ -25,7 +25,40 @@

package sun.awt;

import java.awt.*;
import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Component;
import java.awt.Container;
import java.awt.DefaultKeyboardFocusManager;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.Label;
import java.awt.MenuComponent;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
import java.awt.SystemTray;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.Window;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
Expand All @@ -35,10 +68,10 @@
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.awt.image.MultiResolutionImage;
import java.awt.image.Raster;
import java.awt.peer.FramePeer;
import java.awt.peer.KeyboardFocusManagerPeer;
import java.awt.peer.MouseInfoPeer;
import java.awt.peer.SystemTrayPeer;
import java.awt.peer.TrayIconPeer;
import java.io.File;
Expand All @@ -55,14 +88,14 @@
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import sun.awt.im.InputContext;
import sun.awt.image.ByteArrayImageSource;
import sun.awt.image.FileImageSource;
import sun.awt.image.ImageRepresentation;
import java.awt.image.MultiResolutionImage;
import sun.awt.image.MultiResolutionToolkitImage;
import sun.awt.image.ToolkitImage;
import sun.awt.image.URLImageSource;
Expand All @@ -72,7 +105,13 @@
import sun.security.action.GetPropertyAction;
import sun.util.logging.PlatformLogger;

import static java.awt.RenderingHints.*;
import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_GASP;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON;

public abstract class SunToolkit extends Toolkit
implements ComponentFactory, InputMethodSupport, KeyboardFocusManagerPeerProvider {
Expand Down Expand Up @@ -1377,10 +1416,6 @@ public OperationTimedOut() {
}
}

@SuppressWarnings("serial")
public static class InfiniteLoop extends RuntimeException {
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the rationale behind this exception as that in case of really stuck loop we could identify it. Why are we throwing it away? Now when we exceed the maximum allowed number of iterations we are assuming that the native event queue synchronization happened while it might not be the case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was the initial goal, but currently, it does not work well if some of the components use animation and periodically repainted we hit this exception.

@SuppressWarnings("serial")
public static class IllegalThreadException extends RuntimeException {
public IllegalThreadException(String msg) {
Expand All @@ -1391,14 +1426,14 @@ public IllegalThreadException() {
}

public static final int DEFAULT_WAIT_TIME = 10000;
private static final int MAX_ITERS = 20;
private static final int MIN_ITERS = 0;
private static final int MINIMAL_EDELAY = 0;
private static final int MAX_ITERS = 100;
private static final int MIN_ITERS = 1;
private static final int MINIMAL_DELAY = 5;

/**
* Parameterless version of realsync which uses default timout (see DEFAUL_WAIT_TIME).
*/
public void realSync() throws OperationTimedOut, InfiniteLoop {
public void realSync() throws OperationTimedOut {
realSync(DEFAULT_WAIT_TIME);
}

Expand Down Expand Up @@ -1447,13 +1482,21 @@ public void realSync() throws OperationTimedOut, InfiniteLoop {
*
* @param timeout the maximum time to wait in milliseconds, negative means "forever".
*/
public void realSync(final long timeout) throws OperationTimedOut, InfiniteLoop
{
public void realSync(final long timeout) throws OperationTimedOut {
if (EventQueue.isDispatchThread()) {
throw new IllegalThreadException("The SunToolkit.realSync() method cannot be used on the event dispatch thread (EDT).");
}
try {
// We should wait unconditionally for the first event on EDT
EventQueue.invokeAndWait(() -> {/*dummy implementation*/});
} catch (InterruptedException | InvocationTargetException ignored) {
}
int bigLoop = 0;
long end = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) + timeout;
do {
if (timeout(end) < 0) {
return;
}
// Let's do sync first
sync();

Expand All @@ -1464,15 +1507,12 @@ public void realSync(final long timeout) throws OperationTimedOut, InfiniteLoop
// to dispatch.
int iters = 0;
while (iters < MIN_ITERS) {
syncNativeQueue(timeout);
syncNativeQueue(timeout(end));
iters++;
}
while (syncNativeQueue(timeout) && iters < MAX_ITERS) {
while (syncNativeQueue(timeout(end)) && iters < MAX_ITERS) {
iters++;
}
if (iters >= MAX_ITERS) {
throw new InfiniteLoop();
}

// native requests were dispatched by X/Window Manager or Windows
// Moreover, we processed them all on Toolkit thread
Expand All @@ -1483,21 +1523,23 @@ public void realSync(final long timeout) throws OperationTimedOut, InfiniteLoop
// waitForIdle, we may end up with full EventQueue
iters = 0;
while (iters < MIN_ITERS) {
waitForIdle(timeout);
waitForIdle(timeout(end));
iters++;
}
while (waitForIdle(timeout) && iters < MAX_ITERS) {
while (waitForIdle(end) && iters < MAX_ITERS) {
iters++;
}
if (iters >= MAX_ITERS) {
throw new InfiniteLoop();
}

bigLoop++;
// Again, for Java events, it was simple to check for new Java
// events by checking event queue, but what if Java events
// resulted in native requests? Therefor, check native events again.
} while ((syncNativeQueue(timeout) || waitForIdle(timeout)) && bigLoop < MAX_ITERS);
} while ((syncNativeQueue(timeout(end)) || waitForIdle(end))
&& bigLoop < MAX_ITERS);
}

private long timeout(long end){
return end - TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
}

/**
Expand All @@ -1508,10 +1550,8 @@ public void realSync(final long timeout) throws OperationTimedOut, InfiniteLoop
* {@code true} if some events were processed,
* {@code false} otherwise.
*/
protected abstract boolean syncNativeQueue(final long timeout);
protected abstract boolean syncNativeQueue(long timeout);

private boolean eventDispatched;
private boolean queueEmpty;
private final Object waitLock = new Object();

private boolean isEQEmpty() {
Expand All @@ -1527,13 +1567,13 @@ private boolean isEQEmpty() {
* necessary, {@code false} otherwise.
*/
@SuppressWarnings("serial")
protected final boolean waitForIdle(final long timeout) {
private final boolean waitForIdle(final long end) {
flushPendingEvents();
final boolean queueWasEmpty;
final AtomicBoolean queueEmpty = new AtomicBoolean();
final AtomicBoolean eventDispatched = new AtomicBoolean();
synchronized (waitLock) {
queueWasEmpty = isEQEmpty();
queueEmpty = false;
eventDispatched = false;
postEvent(AppContext.getAppContext(),
new PeerEvent(getSystemEventQueueImpl(), null, PeerEvent.LOW_PRIORITY_EVENT) {
@Override
Expand All @@ -1545,32 +1585,32 @@ public void dispatch() {
// flush Java events again.
int iters = 0;
while (iters < MIN_ITERS) {
syncNativeQueue(timeout);
syncNativeQueue(timeout(end));
iters++;
}
while (syncNativeQueue(timeout) && iters < MAX_ITERS) {
while (syncNativeQueue(timeout(end)) && iters < MAX_ITERS) {
iters++;
}
flushPendingEvents();

synchronized(waitLock) {
queueEmpty = isEQEmpty();
eventDispatched = true;
queueEmpty.set(isEQEmpty());
eventDispatched.set(true);
waitLock.notifyAll();
}
}
});
try {
while (!eventDispatched) {
waitLock.wait();
while (!eventDispatched.get() && timeout(end) > 0) {
waitLock.wait(timeout(end));
}
} catch (InterruptedException ie) {
return false;
}
}

try {
Thread.sleep(MINIMAL_EDELAY);
Thread.sleep(MINIMAL_DELAY);
} catch (InterruptedException ie) {
throw new RuntimeException("Interrupted");
}
Expand All @@ -1579,7 +1619,7 @@ public void dispatch() {

// Lock to force write-cache flush for queueEmpty.
synchronized (waitLock) {
return !(queueEmpty && isEQEmpty() && queueWasEmpty);
return !(queueEmpty.get() && isEQEmpty() && queueWasEmpty);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,9 @@ public static long getEventNumber() {
*/
@Override
protected boolean syncNativeQueue(final long timeout) {
if (timeout <= 0) {
return false;
}
XBaseWindow win = XBaseWindow.getXAWTRootWindow();

if (oops_waiter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ public void showOrHideTouchKeyboard(Component comp, AWTEvent e) {
///////////////////////////////////////////////////////////////////////////

@Override
public native boolean syncNativeQueue(final long timeout);
public native boolean syncNativeQueue(long timeout);

@Override
public boolean isDesktopSupported() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,9 @@ Java_sun_awt_windows_WToolkit_hideTouchKeyboard(JNIEnv *env, jobject self)
JNIEXPORT jboolean JNICALL
Java_sun_awt_windows_WToolkit_syncNativeQueue(JNIEnv *env, jobject self, jlong timeout)
{
if (timeout <= 0) {
return JNI_FALSE;
}
AwtToolkit & tk = AwtToolkit::GetInstance();
DWORD eventNumber = tk.eventNumber;
tk.PostMessage(WM_SYNC_WAIT, 0, 0);
Expand Down
1 change: 0 additions & 1 deletion test/jdk/ProblemList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ javax/swing/SwingUtilities/TestBadBreak/TestBadBreak.java 8160720 generic-all
javax/swing/plaf/basic/Test6984643.java 8198340 windows-all
javax/swing/text/DefaultCaret/HidingSelection/HidingSelectionTest.java 8194048 windows-all
javax/swing/text/DefaultCaret/HidingSelection/MultiSelectionTest.java 8213562 linux-all
javax/swing/text/JTextComponent/5074573/bug5074573.java 8196100 windows-all
javax/swing/JFileChooser/6798062/bug6798062.java 8146446 windows-all
javax/swing/JComboBox/8182031/ComboPopupTest.java 8196465 linux-all,macosx-all
javax/swing/JFileChooser/6738668/bug6738668.java 8194946 generic-all
Expand Down
48 changes: 48 additions & 0 deletions test/jdk/java/awt/Robot/FlushCurrentEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.awt.EventQueue;
import java.awt.Robot;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @test
* @key headful
* @bug 8196100
* @summary Checks that current event is flushed by the Robot.waitForIdle()
*/
public final class FlushCurrentEvent {

public static void main(String[] args) throws Exception {
Robot robot = new Robot();
AtomicBoolean done = new AtomicBoolean();
EventQueue.invokeLater(() -> {
robot.delay(15000);
done.set(true);
});
robot.waitForIdle();
if (!done.get()) {
throw new RuntimeException("Current event was not flushed");
}
}
}
Loading