Fix threading issues with wait callbacks

This commit is contained in:
drdev
2014-04-15 04:32:31 +00:00
parent 8f95c98a3d
commit 5471cd32a4
5 changed files with 55 additions and 20 deletions

1
.gitattributes vendored
View File

@@ -1186,6 +1186,7 @@ forge-gui-mobile/src/forge/toolbox/ListChooser.java -text
forge-gui-mobile/src/forge/util/LayoutHelper.java -text
forge-gui-mobile/src/forge/util/PhysicsObject.java -text
forge-gui-mobile/src/forge/util/Utils.java -text
forge-gui-mobile/src/forge/util/WaitRunnable.java -text
forge-gui/.classpath -text
forge-gui/.project -text
forge-gui/.settings/org.eclipse.core.resources.prefs -text

View File

@@ -5,8 +5,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.lang3.tuple.Pair;
import com.badlogic.gdx.Gdx;
@@ -46,6 +44,7 @@ import forge.toolbox.FOptionPane;
import forge.toolbox.GuiChoose;
import forge.util.ThreadUtil;
import forge.util.WaitCallback;
import forge.util.WaitRunnable;
public class GuiMobile implements IGuiBase {
@Override
@@ -59,20 +58,12 @@ public class GuiMobile implements IGuiBase {
proc.run();
}
else {
final CountDownLatch cdl = new CountDownLatch(1);
Gdx.app.postRunnable(new Runnable() {
new WaitRunnable() {
@Override
public void run() {
proc.run();
cdl.countDown();
}
});
try {
cdl.await();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}.invokeAndWait();
}
}

View File

@@ -1,6 +1,5 @@
package forge.toolbox;
import forge.FThreads;
import forge.game.card.Card;
import forge.util.Callback;
@@ -48,11 +47,6 @@ public class GuiDialog {
}
public static void message(final String message, final String title) {
FThreads.invokeInEdtAndWait(new Runnable() {
@Override
public void run() {
FOptionPane.showMessageDialog(message, title, null);
}
});
FOptionPane.showMessageDialog(message, title, null);
}
}

View File

@@ -0,0 +1,33 @@
package forge.util;
import com.badlogic.gdx.Gdx;
import forge.FThreads;
public abstract class WaitRunnable implements Runnable {
public class Lock {
}
private final Lock lock = new Lock();
public void invokeAndWait() {
FThreads.assertExecutedByEdt(false);
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
WaitRunnable.this.run();
synchronized(lock) {
lock.notify();
}
}
});
try {
synchronized(lock) {
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -3,16 +3,32 @@ package forge.util;
import forge.FThreads;
public abstract class WaitCallback<T> extends Callback<T> implements Runnable {
public class Lock {
}
private final Lock lock = new Lock();
private T result;
@Override
public final void run(T result0) {
result = result0;
synchronized(lock) {
lock.notify();
}
}
public final T invokeAndWait() {
FThreads.assertExecutedByEdt(false); //not supported if on UI thread
FThreads.invokeInEdtAndWait(this);
FThreads.invokeInEdtLater(this);
try {
synchronized(lock) {
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}