made InputSyncronizedBase.stop() indifferent to the thread that it is ran from.

Introducing game thread concept - that might be used (or might be not) to limit threads devoted to AI thinking with one.
This commit is contained in:
Maxmtg
2013-05-28 04:37:11 +00:00
parent 8e51258419
commit be5536d11a
5 changed files with 24 additions and 38 deletions

View File

@@ -35,8 +35,8 @@ public class FThreads {
private FThreads() { } // no instances supposed
private final static ExecutorService cachedPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
private static ExecutorService getCachedPool() { return cachedPool; }
private final static ExecutorService gameThreadPool = Executors.newCachedThreadPool(new WorkerThreadFactory("Game"));
private static ExecutorService getGameThreadPool() { return gameThreadPool; }
private final static ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(2, new WorkerThreadFactory("Delayed"));
private static ScheduledExecutorService getScheduledPool() { return scheduledPool; }
@@ -111,8 +111,8 @@ public class FThreads {
}
public static void invokeInNewThread(Runnable toRun) {
getCachedPool().execute(toRun);
public static void invokeInGameThread(Runnable toRun) {
getGameThreadPool().execute(toRun);
}
/**
@@ -171,5 +171,9 @@ public class FThreads {
public static String debugGetStackTraceItem(int depth) {
return debugGetStackTraceItem(depth, false);
}
public static boolean isGameThread() {
return Thread.currentThread().getName().startsWith("Game");
}
}

View File

@@ -186,11 +186,7 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
public void run() {
HumanPlay.playSpellAbility(chosen.getActivatingPlayer(), chosen);
onManaAbilityPlayed(chosen);
if( isAlredyPaid() ) {
done();
stopNonEdt();
} else
FThreads.invokeInEdtLater(new Runnable() { @Override public void run(){ updateMessage(); }});
onStateChanged();
}
};
game.getInputQueue().invokeGameAction(proc);
@@ -256,12 +252,15 @@ public abstract class InputPayManaBase extends InputSyncronizedBase implements I
public void showMessage() {
if ( isFinished() ) return;
ButtonUtil.enableOnlyCancel();
onStateChanged();
}
protected void onStateChanged() {
if( isAlredyPaid() ) {
done();
stop();
} else
updateMessage();
} else
FThreads.invokeInEdtNowOrLater(new Runnable() { @Override public void run(){ updateMessage(); }});
}
protected void onManaAbilityPaid() {} // some inputs overload it

View File

@@ -81,13 +81,7 @@ public class InputPayManaExecuteCommands extends InputPayManaBase {
if (player.canPayLife(this.phyLifeToLose + 2) && manaCost.payPhyrexian()) {
this.phyLifeToLose += 2;
}
if (this.manaCost.isPaid()) {
this.done();
this.stop();
} else {
this.showMessage();
}
onStateChanged();
}
}

View File

@@ -200,11 +200,10 @@ public class InputQueue extends MyObservable implements java.io.Serializable {
}
public void invokeGameAction(final Runnable proc) {
if(FThreads.isEDT()) {
FThreads.invokeInNewThread(proc);
} else { // this branch is experimental
if( FThreads.isGameThread() ) {
proc.run();
}
} else
FThreads.invokeInGameThread(proc);
}
} // InputControl

View File

@@ -28,9 +28,11 @@ public abstract class InputSyncronizedBase extends InputBase implements InputSyn
protected final void stop() {
setFinished();
FThreads.invokeInNewThread(new Runnable() {
// ensure input won't accept any user actions.
FThreads.invokeInEdtNowOrLater(new Runnable() { @Override public void run() { setFinished(); } });
// this will update input proxy, so there might be anything happening in the thread
getQueue().invokeGameAction( new Runnable() {
@Override
public void run() {
// this will update input proxy, so there might be anything happening in the thread
@@ -39,18 +41,6 @@ public abstract class InputSyncronizedBase extends InputBase implements InputSyn
}
});
}
// This version does not need to be ran from EDT.
protected final void stopNonEdt() {
FThreads.assertExecutedByEdt(false);
// ensure no clicks from EDT will be accepted
FThreads.invokeInEdtLater(new Runnable() { @Override public void run() { setFinished(); } });
// this will update input proxy, so there might be anything happening in the thread
getQueue().removeInput(InputSyncronizedBase.this);
cdlDone.countDown();
}
@Override
public final void selectButtonCancel() {