summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-04-19 10:18:38 +0000
committerrubidium <rubidium@openttd.org>2008-04-19 10:18:38 +0000
commit161b58863144b9677ce8d8cdce0f23be33ddab49 (patch)
tree4be252134284abf1ac2d62c17a3d71b160a34a2d /src
parente0722751c6515dadfba4944ed35f3fa6f6bcd751 (diff)
downloadopenttd-161b58863144b9677ce8d8cdce0f23be33ddab49.tar.xz
(svn r12784) -Codechange: handle the asynchronious save 'handlers' in saveload.cpp instead of openttd.cpp.
Diffstat (limited to 'src')
-rw-r--r--src/openttd.cpp38
-rw-r--r--src/openttd.h12
-rw-r--r--src/saveload.cpp36
-rw-r--r--src/saveload.h3
4 files changed, 34 insertions, 55 deletions
diff --git a/src/openttd.cpp b/src/openttd.cpp
index ae7b0ced5..bc12123df 100644
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -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) {
diff --git a/src/openttd.h b/src/openttd.h
index f860aba3e..1be4e6707 100644
--- a/src/openttd.h
+++ b/src/openttd.h
@@ -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;
diff --git a/src/saveload.cpp b/src/saveload.cpp
index 61d0ed29b..85e9a7ae4 100644
--- a/src/saveload.cpp
+++ b/src/saveload.cpp
@@ -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();
}
diff --git a/src/saveload.h b/src/saveload.h
index 6dbf91f47..102c9a04f 100644
--- a/src/saveload.h
+++ b/src/saveload.h
@@ -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 */