# HG changeset patch # User rubidium # Date 1208600318 0 # Node ID 8d87c2f342cbc671ca8515b5d7937daad6c8f363 # Parent b41d7d8c873b251efd382776ff2d6bede93bdd25 (svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp. diff -r b41d7d8c873b -r 8d87c2f342cb src/openttd.cpp --- a/src/openttd.cpp Sat Apr 19 08:34:03 2008 +0000 +++ b/src/openttd.cpp Sat Apr 19 10:18:38 2008 +0000 @@ -92,6 +92,7 @@ void MusicLoop(); void ResetMusic(); void ResetOldNames(); +void ProcessAsyncSaveFinish(); extern void SetDifficultyLevel(int mode, GameOptions *gm_opt); extern Player* DoStartupNewPlayer(bool is_ai); @@ -645,39 +646,6 @@ } } - -/** Mutex so that only one thread can communicate with the main program - * at any given time */ -static ThreadMsg _message = MSG_OTTD_NO_MESSAGE; - -static inline void OTTD_ReleaseMutex() {_message = MSG_OTTD_NO_MESSAGE;} -static inline ThreadMsg OTTD_PollThreadEvent() {return _message;} - -/** Called by running thread to execute some action in the main game. - * It will stall as long as the mutex is not freed (handled) by the game */ -void OTTD_SendThreadMessage(ThreadMsg msg) -{ - if (_exit_game) return; - while (_message != MSG_OTTD_NO_MESSAGE) CSleep(10); - - _message = msg; -} - - -/** Handle the user-messages sent to us - * @param message message sent - */ -static void ProcessSentMessage(ThreadMsg message) -{ - switch (message) { - case MSG_OTTD_SAVETHREAD_DONE: SaveFileDone(); break; - case MSG_OTTD_SAVETHREAD_ERROR: SaveFileError(); break; - default: NOT_REACHED(); - } - - OTTD_ReleaseMutex(); // release mutex so that other threads, messages can be handled -} - static void ShowScreenshotResult(bool b) { if (b) { @@ -1095,9 +1063,7 @@ void GameLoop() { - ThreadMsg message; - - if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message); + ProcessAsyncSaveFinish(); /* autosave game? */ if (_do_autosave) { diff -r b41d7d8c873b -r 8d87c2f342cb src/openttd.h --- a/src/openttd.h Sat Apr 19 08:34:03 2008 +0000 +++ b/src/openttd.h Sat Apr 19 10:18:38 2008 +0000 @@ -1,4 +1,5 @@ /* $Id$ */ + /** @file openttd.h */ #ifndef OPENTTD_H @@ -108,17 +109,6 @@ }; extern byte _no_scroll; -/** To have a concurrently running thread interface with the main program, use - * the OTTD_SendThreadMessage() function. Actions to perform upon the message are handled - * in the ProcessSentMessage() function */ -enum ThreadMsg { - MSG_OTTD_NO_MESSAGE, - MSG_OTTD_SAVETHREAD_DONE, - MSG_OTTD_SAVETHREAD_ERROR, -}; - -void OTTD_SendThreadMessage(ThreadMsg msg); - extern byte _game_mode; extern bool _exit_game; extern int8 _pause_game; diff -r b41d7d8c873b -r 8d87c2f342cb src/saveload.cpp --- a/src/saveload.cpp Sat Apr 19 08:34:03 2008 +0000 +++ b/src/saveload.cpp Sat Apr 19 10:18:38 2008 +0000 @@ -87,6 +87,32 @@ throw std::exception(); } +typedef void (*AsyncSaveFinishProc)(); +static AsyncSaveFinishProc _async_save_finish = NULL; + +/** + * Called by save thread to tell we finished saving. + */ +static void SetAsyncSaveFinish(AsyncSaveFinishProc proc) +{ + if (_exit_game) return; + while (_async_save_finish != NULL) CSleep(10); + + _async_save_finish = proc; +} + +/** + * Handle async save finishes. + */ +void ProcessAsyncSaveFinish() +{ + if (_async_save_finish == NULL) return; + + _async_save_finish(); + + _async_save_finish = NULL; +} + /** * Fill the input buffer by reading from the file with the given reader */ @@ -1457,7 +1483,7 @@ /** Update the gui accordingly when starting saving * and set locks on saveload. Also turn off fast-forward cause with that * saving takes Aaaaages */ -void SaveFileStart() +static void SaveFileStart() { _ts.ff_state = _fast_forward; _fast_forward = 0; @@ -1469,7 +1495,7 @@ /** Update the gui accordingly when saving is done and release locks * on saveload */ -void SaveFileDone() +static void SaveFileDone() { _fast_forward = _ts.ff_state; if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE); @@ -1496,7 +1522,7 @@ } /** Show a gui message when saving has failed */ -void SaveFileError() +static void SaveFileError() { SetDParamStr(0, GetSaveLoadErrorString()); ShowErrorMessage(STR_012D, STR_NULL, 0, 0); @@ -1545,7 +1571,7 @@ GetSavegameFormat("memory")->uninit_write(); // clean the memorypool fclose(_sl.fh); - if (threaded) OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_DONE); + if (threaded) SetAsyncSaveFinish(SaveFileDone); return SL_OK; } @@ -1557,7 +1583,7 @@ fprintf(stderr, GetSaveLoadErrorString()); if (threaded) { - OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR); + SetAsyncSaveFinish(SaveFileError); } else { SaveFileError(); } diff -r b41d7d8c873b -r 8d87c2f342cb src/saveload.h --- a/src/saveload.h Sat Apr 19 08:34:03 2008 +0000 +++ b/src/saveload.h Sat Apr 19 10:18:38 2008 +0000 @@ -319,7 +319,4 @@ void SlObject(void *object, const SaveLoad *sld); bool SlObjectMember(void *object, const SaveLoad *sld); -void SaveFileStart(); -void SaveFileDone(); -void SaveFileError(); #endif /* SAVELOAD_H */