1
0
Fork 0

(svn r2939) Fix racing condition when using threaded saving (last one I hope).

When game is saved and you save again you get an error message (not when autosaving) and it's aborted. When a game is loaded in the meanwhile execution pauses until saving thread finishes.
release/0.4.5
Darkvater 2005-09-11 14:17:21 +00:00
parent 08e0769967
commit b0ddaae4af
1 changed files with 15 additions and 16 deletions

View File

@ -1296,7 +1296,7 @@ static void* SaveFileToDisk(void *arg)
static byte *tmp = NULL; static byte *tmp = NULL;
uint32 hdr[2]; uint32 hdr[2];
if (save_thread != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_START); if (arg != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_START);
tmp = _sl.buf; tmp = _sl.buf;
@ -1307,8 +1307,9 @@ static void* SaveFileToDisk(void *arg)
_sl.buf = tmp; _sl.buf = tmp;
_sl.excpt_uninit(); _sl.excpt_uninit();
ShowInfoF("Save game failed: %s.", _sl.excpt_msg); fprintf(stderr, "Save game failed: %s.", _sl.excpt_msg);
OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR); if (arg != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
else SaveFileError();
return NULL; return NULL;
} }
@ -1343,7 +1344,7 @@ static void* SaveFileToDisk(void *arg)
GetSavegameFormat("memory")->uninit_write(); // clean the memorypool GetSavegameFormat("memory")->uninit_write(); // clean the memorypool
fclose(_sl.fh); fclose(_sl.fh);
if (save_thread != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE); if (arg != NULL) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE);
return NULL; return NULL;
} }
@ -1367,15 +1368,13 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode)
const SaveLoadFormat *fmt; const SaveLoadFormat *fmt;
uint version; uint version;
/* An instance of saving is already active, so wait until it is done */ /* An instance of saving is already active, so don't go saving again */
if (_ts.saveinprogress) { if (_ts.saveinprogress && mode == SL_SAVE) {
// if not an autosave, but a user action, show error message
if (!_do_autosave) ShowErrorMessage(_error_message, STR_SAVE_STILL_IN_PROGRESS, 0, 0); if (!_do_autosave) ShowErrorMessage(_error_message, STR_SAVE_STILL_IN_PROGRESS, 0, 0);
WaitTillSaved(); return SL_OK;
// nonsense to do an autosave while we were still saving our game, so skip it
if (_do_autosave) return SL_OK;
} else {
WaitTillSaved();
} }
WaitTillSaved();
/* Load a TTDLX or TTDPatch game */ /* Load a TTDLX or TTDPatch game */
if (mode == SL_OLD_LOAD) { if (mode == SL_OLD_LOAD) {
@ -1441,7 +1440,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode)
/* Write to file */ /* Write to file */
if (_network_server || if (_network_server ||
(save_thread = OTTDCreateThread(&SaveFileToDisk, NULL)) == NULL) { (save_thread = OTTDCreateThread(&SaveFileToDisk, (void*)"")) == NULL) {
DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode..."); DEBUG(misc, 1) ("cannot create savegame thread, reverting to single-threaded mode...");
SaveFileToDisk(NULL); SaveFileToDisk(NULL);
} }