Mercurial > hg > openttd
changeset 8989:4ecc117ebb22 draft
(svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sat, 19 Apr 2008 10:18:38 +0000 |
parents | 85e362a3caf5 |
children | c0b047cd829c |
files | src/openttd.cpp src/openttd.h src/saveload.cpp src/saveload.h |
diffstat | 4 files changed, 34 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -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) {
--- a/src/openttd.h +++ b/src/openttd.h @@ -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;
--- a/src/saveload.cpp +++ b/src/saveload.cpp @@ -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(); }