tron@2285: /* $Id$ */ tron@2285: tron@2285: #include "stdafx.h" tron@2285: #include "thread.h" tron@2285: #include tron@2285: truelight@4324: #if defined(__AMIGA__) || defined(__MORPHOS__) || defined(NO_THREADS) truelight@4324: OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; } truelight@4324: void *OTTDJoinThread(OTTDThread *t) { return NULL; } truelight@4324: void OTTDExitThread(void) { NOT_REACHED(); }; tron@2285: tron@2285: #elif defined(__OS2__) tron@2285: tron@2285: #define INCL_DOS tron@2285: #include tron@2285: #include tron@2285: truelight@4298: struct OTTDThread { tron@2285: TID thread; truelight@4298: OTTDThreadFunc func; tron@2286: void* arg; tron@2286: void* ret; tron@2285: }; tron@2285: tron@2286: static void Proxy(void* arg) tron@2286: { truelight@4298: OTTDThread* t = arg; tron@2286: t->ret = t->func(t->arg); tron@2286: } tron@2286: truelight@4298: OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg) tron@2285: { truelight@4298: OTTDThread* t = malloc(sizeof(*t)); tron@2285: tron@2285: if (t == NULL) return NULL; tron@2285: tron@2286: t->func = function; tron@2286: t->arg = arg; tron@2286: t->thread = _beginthread(Proxy, NULL, 32768, t); tron@2285: if (t->thread != -1) { tron@2285: return t; tron@2285: } else { tron@2285: free(t); tron@2285: return NULL; tron@2285: } tron@2285: } tron@2285: truelight@4298: void* OTTDJoinThread(OTTDThread* t) tron@2285: { tron@2286: void* ret; tron@2286: tron@2286: if (t == NULL) return NULL; tron@2285: tron@2285: DosWaitThread(&t->thread, DCWW_WAIT); tron@2286: ret = t->ret; tron@2285: free(t); tron@2286: return ret; tron@2285: } tron@2285: truelight@4300: void OTTDExitThread(void) truelight@4300: { truelight@4300: _endthread(); truelight@4300: } tron@2285: tron@2285: #elif defined(UNIX) tron@2285: tron@2285: #include tron@2285: truelight@4298: struct OTTDThread { tron@2285: pthread_t thread; tron@2285: }; tron@2285: truelight@4298: OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg) tron@2285: { truelight@4298: OTTDThread* t = malloc(sizeof(*t)); tron@2285: tron@2285: if (t == NULL) return NULL; tron@2285: tron@2286: if (pthread_create(&t->thread, NULL, function, arg) == 0) { tron@2285: return t; tron@2285: } else { tron@2285: free(t); tron@2285: return NULL; tron@2285: } tron@2285: } tron@2285: truelight@4298: void* OTTDJoinThread(OTTDThread* t) tron@2285: { tron@2286: void* ret; tron@2285: tron@2286: if (t == NULL) return NULL; tron@2286: tron@2286: pthread_join(t->thread, &ret); tron@2285: free(t); tron@2286: return ret; tron@2285: } tron@2285: truelight@4300: void OTTDExitThread(void) truelight@4300: { truelight@4300: pthread_exit(NULL); truelight@4300: } tron@2285: tron@2285: #elif defined(WIN32) tron@2285: tron@2285: #include tron@2285: truelight@4298: struct OTTDThread { tron@2285: HANDLE thread; truelight@4298: OTTDThreadFunc func; tron@2286: void* arg; tron@2286: void* ret; tron@2285: }; tron@2285: tron@2286: static DWORD WINAPI Proxy(LPVOID arg) tron@2286: { truelight@4298: OTTDThread* t = arg; tron@2286: t->ret = t->func(t->arg); tron@2286: return 0; tron@2286: } tron@2286: truelight@4298: OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg) tron@2285: { truelight@4298: OTTDThread* t = malloc(sizeof(*t)); tron@2285: DWORD dwThreadId; tron@2285: tron@2285: if (t == NULL) return NULL; tron@2285: tron@2286: t->func = function; tron@2286: t->arg = arg; tron@2287: t->thread = CreateThread(NULL, 0, Proxy, t, 0, &dwThreadId); tron@2285: tron@2285: if (t->thread != NULL) { tron@2285: return t; tron@2285: } else { tron@2285: free(t); tron@2285: return NULL; tron@2285: } tron@2285: } tron@2285: truelight@4298: void* OTTDJoinThread(OTTDThread* t) tron@2285: { tron@2286: void* ret; tron@2286: tron@2286: if (t == NULL) return NULL; tron@2285: tron@2285: WaitForSingleObject(t->thread, INFINITE); tron@2285: CloseHandle(t->thread); tron@2286: ret = t->ret; tron@2285: free(t); tron@2286: return ret; tron@2285: } truelight@4300: truelight@4300: void OTTDExitThread(void) truelight@4300: { truelight@4300: ExitThread(0); truelight@4300: } tron@2285: #endif