diff options
Diffstat (limited to 'thread.c')
-rw-r--r-- | thread.c | 58 |
1 files changed, 45 insertions, 13 deletions
@@ -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 |