mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 12:48:00 +00:00
Fix threading issues with wait callbacks
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -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/LayoutHelper.java -text
|
||||||
forge-gui-mobile/src/forge/util/PhysicsObject.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/Utils.java -text
|
||||||
|
forge-gui-mobile/src/forge/util/WaitRunnable.java -text
|
||||||
forge-gui/.classpath -text
|
forge-gui/.classpath -text
|
||||||
forge-gui/.project -text
|
forge-gui/.project -text
|
||||||
forge-gui/.settings/org.eclipse.core.resources.prefs -text
|
forge-gui/.settings/org.eclipse.core.resources.prefs -text
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import java.util.Collection;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
@@ -46,6 +44,7 @@ import forge.toolbox.FOptionPane;
|
|||||||
import forge.toolbox.GuiChoose;
|
import forge.toolbox.GuiChoose;
|
||||||
import forge.util.ThreadUtil;
|
import forge.util.ThreadUtil;
|
||||||
import forge.util.WaitCallback;
|
import forge.util.WaitCallback;
|
||||||
|
import forge.util.WaitRunnable;
|
||||||
|
|
||||||
public class GuiMobile implements IGuiBase {
|
public class GuiMobile implements IGuiBase {
|
||||||
@Override
|
@Override
|
||||||
@@ -59,20 +58,12 @@ public class GuiMobile implements IGuiBase {
|
|||||||
proc.run();
|
proc.run();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
final CountDownLatch cdl = new CountDownLatch(1);
|
new WaitRunnable() {
|
||||||
Gdx.app.postRunnable(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
proc.run();
|
proc.run();
|
||||||
cdl.countDown();
|
|
||||||
}
|
}
|
||||||
});
|
}.invokeAndWait();
|
||||||
try {
|
|
||||||
cdl.await();
|
|
||||||
}
|
|
||||||
catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package forge.toolbox;
|
package forge.toolbox;
|
||||||
|
|
||||||
import forge.FThreads;
|
|
||||||
import forge.game.card.Card;
|
import forge.game.card.Card;
|
||||||
import forge.util.Callback;
|
import forge.util.Callback;
|
||||||
|
|
||||||
@@ -48,11 +47,6 @@ public class GuiDialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void message(final String message, final String title) {
|
public static void message(final String message, final String title) {
|
||||||
FThreads.invokeInEdtAndWait(new Runnable() {
|
FOptionPane.showMessageDialog(message, title, null);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
FOptionPane.showMessageDialog(message, title, null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
forge-gui-mobile/src/forge/util/WaitRunnable.java
Normal file
33
forge-gui-mobile/src/forge/util/WaitRunnable.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,16 +3,32 @@ package forge.util;
|
|||||||
import forge.FThreads;
|
import forge.FThreads;
|
||||||
|
|
||||||
public abstract class WaitCallback<T> extends Callback<T> implements Runnable {
|
public abstract class WaitCallback<T> extends Callback<T> implements Runnable {
|
||||||
|
public class Lock {
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Lock lock = new Lock();
|
||||||
|
|
||||||
private T result;
|
private T result;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void run(T result0) {
|
public final void run(T result0) {
|
||||||
result = result0;
|
result = result0;
|
||||||
|
synchronized(lock) {
|
||||||
|
lock.notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final T invokeAndWait() {
|
public final T invokeAndWait() {
|
||||||
FThreads.assertExecutedByEdt(false); //not supported if on UI thread
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user