summaryrefslogtreecommitdiff
path: root/openttd.c
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2005-09-02 16:05:59 +0000
committerDarkvater <Darkvater@openttd.org>2005-09-02 16:05:59 +0000
commita948fcb6055b5aaf3086913a96e623296d62ef37 (patch)
tree770995fa006874ecf7f11983a589bd239a3e85a0 /openttd.c
parentacf442102a19ef8cb27f6dbc22668e39d4d59af4 (diff)
downloadopenttd-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.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/openttd.c b/openttd.c
index 32837d142..997950a24 100644
--- a/openttd.c
+++ b/openttd.c
@@ -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) {