summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortron <tron@openttd.org>2005-08-05 11:53:48 +0000
committertron <tron@openttd.org>2005-08-05 11:53:48 +0000
commitbd48d67c28ba878fed75df6b276f92b6fab8ed01 (patch)
tree156ba2b5231e6c0ab0f9fe6830a7b60ec10af251
parentf315c95fa1221441106f1e6e4f4a0f7849ccb4b4 (diff)
downloadopenttd-bd48d67c28ba878fed75df6b276f92b6fab8ed01.tar.xz
(svn r2810) Threads may now return information when they terminate using a void*.
Also add the new files to the MSVC project files.
-rw-r--r--openttd.dsp8
-rw-r--r--openttd.vcproj6
-rw-r--r--saveload.c5
-rw-r--r--thread.c58
-rw-r--r--thread.h4
5 files changed, 64 insertions, 17 deletions
diff --git a/openttd.dsp b/openttd.dsp
index 127d362c0..01cd3731a 100644
--- a/openttd.dsp
+++ b/openttd.dsp
@@ -402,6 +402,10 @@ SOURCE=.\texteff.c
# End Source File
# Begin Source File
+SOURCE=.\thread.c
+# End Source File
+# Begin Source File
+
SOURCE=.\tile.c
# End Source File
# Begin Source File
@@ -650,6 +654,10 @@ SOURCE=.\strings.h
# End Source File
# Begin Source File
+SOURCE=.\thread.h
+# End Source File
+# Begin Source File
+
SOURCE=.\tile.h
# End Source File
# Begin Source File
diff --git a/openttd.vcproj b/openttd.vcproj
index 997054288..ec8279e2e 100644
--- a/openttd.vcproj
+++ b/openttd.vcproj
@@ -402,6 +402,9 @@
RelativePath="texteff.c">
</File>
<File
+ RelativePath=".\thread.c">
+ </File>
+ <File
RelativePath=".\tile.c">
</File>
<File
@@ -613,6 +616,9 @@
RelativePath=".\string.h">
</File>
<File
+ RelativePath=".\thread.h">
+ </File>
+ <File
RelativePath=".\tile.h">
</File>
<File
diff --git a/saveload.c b/saveload.c
index 0c33d8c83..a4ef8c80d 100644
--- a/saveload.c
+++ b/saveload.c
@@ -1235,7 +1235,7 @@ static inline void SaveFileDone(void)
/** We have written the whole game into memory, _save_pool, now find
* and appropiate compressor and start writing to file.
*/
-static void SaveFileToDisk(void* arg)
+static void* SaveFileToDisk(void* arg)
{
const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format);
/* XXX - backup _sl.buf cause it is used internally by the writer
@@ -1258,7 +1258,7 @@ static void SaveFileToDisk(void* arg)
ShowErrorMessage(STR_4007_GAME_SAVE_FAILED, STR_NULL, 0, 0);
SaveFileDone();
- return;
+ return NULL;
}
/* We have written our stuff to memory, now write it to file! */
@@ -1293,6 +1293,7 @@ static void SaveFileToDisk(void* arg)
fclose(_sl.fh);
SaveFileDone();
+ return NULL;
}
diff --git a/thread.c b/thread.c
index 6c70ebdb2..3258830d6 100644
--- a/thread.c
+++ b/thread.c
@@ -6,7 +6,7 @@
#if defined(__AMIGA__) || defined(__MORPHOS__)
Thread* OTTDCreateThread(ThreadFunc function, void* arg) { return NULL; }
-void OTTDJoinThread(Thread*) {}
+void* OTTDJoinThread(Thread*) { return NULL; }
#elif defined(__OS2__)
@@ -17,15 +17,26 @@ void OTTDJoinThread(Thread*) {}
struct Thread {
TID thread;
+ ThradFunc func;
+ void* arg;
+ void* ret;
};
+static void Proxy(void* arg)
+{
+ Thread* t = arg;
+ t->ret = t->func(t->arg);
+}
+
Thread* OTTDCreateThread(ThreadFunc function, void* arg)
{
Thread* t = malloc(sizeof(*t));
if (t == NULL) return NULL;
- t->thread = _beginthread(function, NULL, 32768, arg);
+ t->func = function;
+ t->arg = arg;
+ t->thread = _beginthread(Proxy, NULL, 32768, t);
if (t->thread != -1) {
return t;
} else {
@@ -34,12 +45,16 @@ Thread* OTTDCreateThread(ThreadFunc function, void* arg)
}
}
-void OTTDJoinThread(Thread* t)
+void* OTTDJoinThread(Thread* t)
{
- if (t == NULL) return;
+ void* ret;
+
+ if (t == NULL) return NULL;
DosWaitThread(&t->thread, DCWW_WAIT);
+ ret = t->ret;
free(t);
+ return ret;
}
@@ -57,7 +72,7 @@ Thread* OTTDCreateThread(ThreadFunc function, void* arg)
if (t == NULL) return NULL;
- if (pthread_create(&t->thread, NULL, (void* (*)(void*))function, arg) == 0) {
+ if (pthread_create(&t->thread, NULL, function, arg) == 0) {
return t;
} else {
free(t);
@@ -65,12 +80,15 @@ Thread* OTTDCreateThread(ThreadFunc function, void* arg)
}
}
-void OTTDJoinThread(Thread* t)
+void* OTTDJoinThread(Thread* t)
{
- if (t == NULL) return;
+ void* ret;
+
+ if (t == NULL) return NULL;
- pthread_join(t->thread, NULL);
+ pthread_join(t->thread, &ret);
free(t);
+ return ret;
}
@@ -80,8 +98,18 @@ void OTTDJoinThread(Thread* t)
struct Thread {
HANDLE thread;
+ ThradFunc func;
+ void* arg;
+ void* ret;
};
+static DWORD WINAPI Proxy(LPVOID arg)
+{
+ Thread* t = arg;
+ t->ret = t->func(t->arg);
+ return 0;
+}
+
Thread* OTTDCreateThread(ThreadFunc function, void* arg)
{
Thread* t = malloc(sizeof(*t));
@@ -89,9 +117,9 @@ Thread* OTTDCreateThread(ThreadFunc function, void* arg)
if (t == NULL) return NULL;
- t->thread = CreateThread(
- NULL, 0, (LPTHREAD_START_ROUTINE)function, arg, 0, &dwThreadId
- );
+ t->func = function;
+ t->arg = arg;
+ t->thread = CreateThread(NULL, 0, Proxy, arg, 0, &dwThreadId);
if (t->thread != NULL) {
return t;
@@ -101,12 +129,16 @@ Thread* OTTDCreateThread(ThreadFunc function, void* arg)
}
}
-void OTTDJoinThread(Thread* t)
+void* OTTDJoinThread(Thread* t)
{
- if (t == NULL) return;
+ void* ret;
+
+ if (t == NULL) return NULL;
WaitForSingleObject(t->thread, INFINITE);
CloseHandle(t->thread);
+ ret = t->ret;
free(t);
+ return ret;
}
#endif
diff --git a/thread.h b/thread.h
index 1f4878106..73cea8c57 100644
--- a/thread.h
+++ b/thread.h
@@ -11,9 +11,9 @@
typedef struct Thread Thread;
-typedef void (*ThreadFunc)(void*);
+typedef void* (*ThreadFunc)(void*);
Thread* OTTDCreateThread(ThreadFunc, void*);
-void OTTDJoinThread(Thread*);
+void* OTTDJoinThread(Thread*);
#endif