making isGameOver synchronized,

isValid in InputProxy.java is also synchronized instead of being volatile
This commit is contained in:
Maxmtg
2013-03-08 16:10:35 +00:00
parent 744687ce88
commit d34e00bd00
6 changed files with 52 additions and 49 deletions

1
.gitattributes vendored
View File

@@ -14145,6 +14145,7 @@ src/main/java/forge/game/GameActionPlay.java -text
src/main/java/forge/game/GameActionUtil.java svneol=native#text/plain
src/main/java/forge/game/GameEndReason.java -text
src/main/java/forge/game/GameFormat.java -text
src/main/java/forge/game/GameInputUpdatesThread.java -text
src/main/java/forge/game/GameLossReason.java -text
src/main/java/forge/game/GameNew.java -text
src/main/java/forge/game/GameOutcome.java -text

View File

@@ -0,0 +1,41 @@
package forge.game;
import forge.error.BugReporter;
import forge.gui.match.controllers.CMessage;
/**
* TODO: Write javadoc for this type.
*
*/
public final class GameInputUpdatesThread extends Thread {
private final MatchController match;
private final GameState game;
private boolean wasChangedRecently;
/**
* TODO: Write javadoc for Constructor.
* @param match
* @param game
*/
public GameInputUpdatesThread(MatchController match, GameState game) {
this.match = match;
this.game = game;
}
public void run(){
while(!game.isGameOver()) {
boolean needsNewInput = CMessage.SINGLETON_INSTANCE.getInputControl().isValid() == false;
if ( needsNewInput ) {
match.getInput().setNewInput(game);
wasChangedRecently = true;
}
try {
Thread.sleep(wasChangedRecently ? 2 : 40);
wasChangedRecently = false;
} catch (InterruptedException e) {
BugReporter.reportException(e);
break;
}
}
}
}

View File

@@ -26,7 +26,6 @@ import forge.card.trigger.TriggerType;
import forge.deck.Deck;
import forge.deck.CardPool;
import forge.deck.DeckSection;
import forge.error.BugReporter;
import forge.game.event.FlipCoinEvent;
import forge.game.phase.PhaseHandler;
import forge.game.player.AIPlayer;
@@ -34,7 +33,6 @@ import forge.game.player.LobbyPlayer;
import forge.game.player.Player;
import forge.game.zone.PlayerZone;
import forge.game.zone.ZoneType;
import forge.gui.match.controllers.CMessage;
import forge.gui.match.views.VAntes;
import forge.item.CardPrinted;
import forge.item.IPaperCard;
@@ -49,43 +47,6 @@ import forge.util.MyRandom;
*/
public class GameNew {
/**
* TODO: Write javadoc for this type.
*
*/
public static final class GameInputUpdatesThread extends Thread {
private final MatchController match;
private final GameState game;
private boolean wasChangedRecently;
/**
* TODO: Write javadoc for Constructor.
* @param match
* @param game
*/
public GameInputUpdatesThread(MatchController match, GameState game) {
this.match = match;
this.game = game;
}
public void run(){
while(!game.isGameOver()) {
boolean needsNewInput = CMessage.SINGLETON_INSTANCE.getInputControl().isValid() == false;
if ( needsNewInput ) {
match.getInput().setNewInput(game);
wasChangedRecently = true;
}
try {
Thread.sleep(wasChangedRecently ? 2 : 40);
wasChangedRecently = false;
} catch (InterruptedException e) {
BugReporter.reportException(e);
break;
}
}
}
}
public static final ForgePreferences preferences = Singletons.getModel().getPreferences();
private static void preparePlayerLibrary(Player player, final ZoneType zoneType, CardPool secion, boolean canRandomFoil, Random generator) {
@@ -235,10 +196,6 @@ public class GameNew {
for (final Player p1 : game.getPlayers()) {
p1.drawCards(p1.getMaxHandSize());
}
Thread thGame = new GameInputUpdatesThread(match, game);
thGame.setName("Game input updater");
thGame.start();
}
// ultimate of Karn the Liberated

View File

@@ -287,7 +287,7 @@ public class GameState {
/**
* @return the gameOver
*/
public boolean isGameOver() {
public synchronized boolean isGameOver() {
return gameOver;
}
@@ -295,7 +295,7 @@ public class GameState {
* @param reason
* @param go the gameOver to set
*/
public void setGameOver(GameEndReason reason) {
public synchronized void setGameOver(GameEndReason reason) {
this.gameOver = true;
for (Player p : roIngamePlayers) {
p.onGameOver();

View File

@@ -160,6 +160,10 @@ public class MatchController {
final boolean canRandomFoil = Singletons.getModel().getPreferences().getPrefBoolean(FPref.UI_RANDOM_FOIL) && gameType == GameType.Constructed;
GameNew.newGame(this, startConditions, currentGame, canRandomFoil);
Thread thGame = new GameInputUpdatesThread(this, currentGame);
thGame.setName("Game input updater");
thGame.start();
// TODO restore this functionality!!!
//VMatchUI.SINGLETON_INSTANCE.getViewDevMode().getDocument().setVisible(Preferences.DEV_MODE);
for (final VField field : VMatchUI.SINGLETON_INSTANCE.getFieldViews()) {

View File

@@ -38,10 +38,10 @@ public class InputProxy extends MyObservable implements Observer {
/** The input. */
private Input input;
private volatile boolean valid = false;
private boolean valid = false;
@Override
public final void update(final Observable observable, final Object obj) {
public final synchronized void update(final Observable observable, final Object obj) {
valid = false;
}
/**
@@ -52,7 +52,7 @@ public class InputProxy extends MyObservable implements Observer {
* @param in
* a {@link forge.control.input.Input} object.
*/
public final void setInput(final Input in) {
public final synchronized void setInput(final Input in) {
valid = true;
this.input = in;
this.input.showMessage(); // this call may invalidate the input by the time it returns
@@ -114,7 +114,7 @@ public class InputProxy extends MyObservable implements Observer {
}
public boolean isValid() {
public synchronized boolean isValid() {
return valid;
}
}