tron@2285: /* $Id$ */ tron@2285: tron@2285: #include "stdafx.h" tron@2285: #include "thread.h" tron@2285: #include tron@2285: tron@2285: #if defined(__AMIGA__) || defined(__MORPHOS__) tron@2285: Thread* OTTDCreateThread(ThreadFunc function, void* arg) { return NULL; } truelight@2290: void* OTTDJoinThread(Thread* t) { return NULL; } tron@2285: tron@2285: tron@2285: #elif defined(__OS2__) tron@2285: tron@2285: #define INCL_DOS tron@2285: #include tron@2285: #include tron@2285: tron@2285: struct Thread { tron@2285: TID thread; tron@2287: ThreadFunc func; tron@2286: void* arg; tron@2286: void* ret; tron@2285: }; tron@2285: tron@2286: static void Proxy(void* arg) tron@2286: { tron@2286: Thread* t = arg; tron@2286: t->ret = t->func(t->arg); tron@2286: } tron@2286: tron@2285: Thread* OTTDCreateThread(ThreadFunc function, void* arg) tron@2285: { tron@2285: Thread* 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: tron@2286: void* OTTDJoinThread(Thread* 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: tron@2285: tron@2285: #elif defined(UNIX) tron@2285: tron@2285: #include tron@2285: tron@2285: struct Thread { tron@2285: pthread_t thread; tron@2285: }; tron@2285: tron@2285: Thread* OTTDCreateThread(ThreadFunc function, void* arg) tron@2285: { tron@2285: Thread* 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: tron@2286: void* OTTDJoinThread(Thread* 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: tron@2285: tron@2285: #elif defined(WIN32) tron@2285: tron@2285: #include tron@2285: tron@2285: struct Thread { tron@2285: HANDLE thread; tron@2287: ThreadFunc func; tron@2286: void* arg; tron@2286: void* ret; tron@2285: }; tron@2285: tron@2286: static DWORD WINAPI Proxy(LPVOID arg) tron@2286: { tron@2286: Thread* t = arg; tron@2286: t->ret = t->func(t->arg); tron@2286: return 0; tron@2286: } tron@2286: tron@2285: Thread* OTTDCreateThread(ThreadFunc function, void* arg) tron@2285: { tron@2285: Thread* 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: tron@2286: void* OTTDJoinThread(Thread* 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: } tron@2285: #endif