mirror of https://github.com/OpenTTD/OpenTTD
(svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
parent
e0722751c6
commit
161b588631
|
@ -92,6 +92,7 @@ void DoPaletteAnimations();
|
|||
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 @@ void HandleExitGameRequest()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/** 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 @@ static void HandleKeyScrolling()
|
|||
|
||||
void GameLoop()
|
||||
{
|
||||
ThreadMsg message;
|
||||
|
||||
if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message);
|
||||
ProcessAsyncSaveFinish();
|
||||
|
||||
/* autosave game? */
|
||||
if (_do_autosave) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/* $Id$ */
|
||||
|
||||
/** @file openttd.h */
|
||||
|
||||
#ifndef OPENTTD_H
|
||||
|
@ -108,17 +109,6 @@ enum {
|
|||
};
|
||||
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;
|
||||
|
|
|
@ -87,6 +87,32 @@ static void NORETURN SlError(StringID string, const char *extra_msg = NULL)
|
|||
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 @@ static inline SaveOrLoadResult AbortSaveLoad()
|
|||
/** 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 @@ void SaveFileStart()
|
|||
|
||||
/** 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 @@ const char *GetSaveLoadErrorString()
|
|||
}
|
||||
|
||||
/** 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 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
|
|||
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 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
|
|||
fprintf(stderr, GetSaveLoadErrorString());
|
||||
|
||||
if (threaded) {
|
||||
OTTD_SendThreadMessage(MSG_OTTD_SAVETHREAD_ERROR);
|
||||
SetAsyncSaveFinish(SaveFileError);
|
||||
} else {
|
||||
SaveFileError();
|
||||
}
|
||||
|
|
|
@ -319,7 +319,4 @@ void SlArray(void *array, uint length, VarType conv);
|
|||
void SlObject(void *object, const SaveLoad *sld);
|
||||
bool SlObjectMember(void *object, const SaveLoad *sld);
|
||||
|
||||
void SaveFileStart();
|
||||
void SaveFileDone();
|
||||
void SaveFileError();
|
||||
#endif /* SAVELOAD_H */
|
||||
|
|
Loading…
Reference in New Issue