diff options
author | Darkvater <Darkvater@openttd.org> | 2005-09-02 16:05:59 +0000 |
---|---|---|
committer | Darkvater <Darkvater@openttd.org> | 2005-09-02 16:05:59 +0000 |
commit | a948fcb6055b5aaf3086913a96e623296d62ef37 (patch) | |
tree | 770995fa006874ecf7f11983a589bd239a3e85a0 /openttd.c | |
parent | acf442102a19ef8cb27f6dbc22668e39d4d59af4 (diff) | |
download | openttd-a948fcb6055b5aaf3086913a96e623296d62ef37.tar.xz |
(svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Diffstat (limited to 'openttd.c')
-rw-r--r-- | openttd.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -542,6 +542,38 @@ int ttd_main(int argc, char* argv[]) return 0; } +/** Mutex so that only one thread can communicate with the main program + * at any given time */ +static ThreadMsg _message = 0; + +static inline void OTTD_ReleaseMutex(void) {_message = 0;} +static inline ThreadMsg OTTD_PollThreadEvent(void) {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) +{ + while (_message != 0) CSleep(10); + + _message = msg; +} + + +/** Handle the user-messages sent to us + * @param message message sent + */ +void ProcessSentMessage(ThreadMsg message) +{ + switch (message) { + case MSG_OTTD_SAVETHREAD_START: SaveFileStart(); break; + 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) { @@ -914,6 +946,10 @@ static void HandleKeyScrolling(void) void GameLoop(void) { int m; + ThreadMsg message; + + + if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message); // autosave game? if (_do_autosave) { |