From 6bd78a44338ad11ff03a2ac554e6fd5bd5ed8418 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Sun, 7 Feb 2010 16:43:36 +0100 Subject: [PATCH] Remove race condition in clock switching The move number is now incremented inside SwitchClock, when no timer interrupt is scheduled, so the interrupt cannot occur at the wrong side of the increment, and decrement the wrong clock. --- backend.c | 19 ++++++++++--------- 1 files changed, 10 insertions(+), 9 deletions(-) diff --git a/backend.c b/backend.c index 2f7ee57..ecf338c 100644 --- a/backend.c +++ b/backend.c @@ -189,7 +189,7 @@ void ParseGameHistory P((char *game)); void ParseBoard12 P((char *string)); void KeepAlive P((void)); void StartClocks P((void)); -void SwitchClocks P((void)); +void SwitchClocks P((int nr)); void StopClocks P((void)); void ResetClocks P((void)); char *PGNDate P((void)); @@ -3048,10 +3048,10 @@ read_from_ics(isr, closure, data, count, error) looking_at(buf, &i, "It is not your move")) { /* Illegal move */ if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved - currentMove = --forwardMostMove; + currentMove = forwardMostMove-1; DisplayMove(currentMove - 1); /* before DMError */ DrawPosition(FALSE, boards[currentMove]); - SwitchClocks(); + SwitchClocks(forwardMostMove-1); // [HGM] race DisplayBothClocks(); } DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg @@ -6657,9 +6657,9 @@ if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats. gameMode = EditGame; ModeHighlight(); } - currentMove = --forwardMostMove; + currentMove = forwardMostMove-1; DisplayMove(currentMove-1); /* before DisplayMoveError */ - SwitchClocks(); + SwitchClocks(forwardMostMove-1); // [HGM] race DisplayBothClocks(); sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"), parseList[currentMove], cps->which); @@ -7737,8 +7737,8 @@ MakeMove(fromX, fromY, toX, toY, promoChar) {int i; for(i=0; i= 0) lastTickLength = 0; blackTimeRemaining -= lastTickLength; /* [HGM] PGNtime: save time for PGN file if engine did not give it */ @@ -13390,6 +13390,7 @@ SwitchClocks() } flagged = CheckFlags(); } + forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled CheckTimeControl(); if (flagged || !appData.clockMode) return; -- 1.7.0.4