(svn r2810) Threads may now return information when they terminate using a void*.
authortron
Fri, 05 Aug 2005 11:53:48 +0000
changeset 2286 acb76b379e72
parent 2285 3193cbd1ba88
child 2287 2e556bae5fbb
(svn r2810) Threads may now return information when they terminate using a void*.
Also add the new files to the MSVC project files.
openttd.dsp
openttd.vcproj
saveload.c
thread.c
thread.h
--- a/openttd.dsp	Fri Aug 05 09:15:41 2005 +0000
+++ b/openttd.dsp	Fri Aug 05 11:53:48 2005 +0000
@@ -402,6 +402,10 @@
 # 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 @@
 # End Source File
 # Begin Source File
 
+SOURCE=.\thread.h
+# End Source File
+# Begin Source File
+
 SOURCE=.\tile.h
 # End Source File
 # Begin Source File
--- a/openttd.vcproj	Fri Aug 05 09:15:41 2005 +0000
+++ b/openttd.vcproj	Fri Aug 05 11:53:48 2005 +0000
@@ -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
--- a/saveload.c	Fri Aug 05 09:15:41 2005 +0000
+++ b/saveload.c	Fri Aug 05 11:53:48 2005 +0000
@@ -1235,7 +1235,7 @@
 /** 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 @@
 		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 @@
 	fclose(_sl.fh);
 
 	SaveFileDone();
+	return NULL;
 }
 
 
--- a/thread.c	Fri Aug 05 09:15:41 2005 +0000
+++ b/thread.c	Fri Aug 05 11:53:48 2005 +0000
@@ -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 @@
 
 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 @@
 	}
 }
 
-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 @@
 
 	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 @@
 	}
 }
 
-void OTTDJoinThread(Thread* t)
+void* OTTDJoinThread(Thread* t)
 {
-	if (t == NULL) return;
+	void* ret;
 
-	pthread_join(t->thread, NULL);
+	if (t == NULL) return NULL;
+
+	pthread_join(t->thread, &ret);
 	free(t);
+	return ret;
 }
 
 
@@ -80,8 +98,18 @@
 
 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 @@
 
 	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 @@
 	}
 }
 
-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
--- a/thread.h	Fri Aug 05 09:15:41 2005 +0000
+++ b/thread.h	Fri Aug 05 11:53:48 2005 +0000
@@ -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