2005-08-05 09:15:41 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#if defined(__AMIGA__) || defined(__MORPHOS__)
|
|
|
|
Thread* OTTDCreateThread(ThreadFunc function, void* arg) { return NULL; }
|
2005-08-06 14:58:06 +00:00
|
|
|
void* OTTDJoinThread(Thread* t) { return NULL; }
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
|
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
#include <process.h>
|
|
|
|
|
|
|
|
struct Thread {
|
|
|
|
TID thread;
|
2005-08-05 20:18:08 +00:00
|
|
|
ThreadFunc func;
|
2005-08-05 11:53:48 +00:00
|
|
|
void* arg;
|
|
|
|
void* ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
};
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
static void Proxy(void* arg)
|
|
|
|
{
|
|
|
|
Thread* t = arg;
|
|
|
|
t->ret = t->func(t->arg);
|
|
|
|
}
|
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
Thread* OTTDCreateThread(ThreadFunc function, void* arg)
|
|
|
|
{
|
|
|
|
Thread* t = malloc(sizeof(*t));
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
t->func = function;
|
|
|
|
t->arg = arg;
|
|
|
|
t->thread = _beginthread(Proxy, NULL, 32768, t);
|
2005-08-05 09:15:41 +00:00
|
|
|
if (t->thread != -1) {
|
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
free(t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
void* OTTDJoinThread(Thread* t)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2005-08-05 11:53:48 +00:00
|
|
|
void* ret;
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
DosWaitThread(&t->thread, DCWW_WAIT);
|
2005-08-05 11:53:48 +00:00
|
|
|
ret = t->ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
free(t);
|
2005-08-05 11:53:48 +00:00
|
|
|
return ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined(UNIX)
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
struct Thread {
|
|
|
|
pthread_t thread;
|
|
|
|
};
|
|
|
|
|
|
|
|
Thread* OTTDCreateThread(ThreadFunc function, void* arg)
|
|
|
|
{
|
|
|
|
Thread* t = malloc(sizeof(*t));
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
if (pthread_create(&t->thread, NULL, function, arg) == 0) {
|
2005-08-05 09:15:41 +00:00
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
free(t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
void* OTTDJoinThread(Thread* t)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2005-08-05 11:53:48 +00:00
|
|
|
void* ret;
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
pthread_join(t->thread, &ret);
|
2005-08-05 09:15:41 +00:00
|
|
|
free(t);
|
2005-08-05 11:53:48 +00:00
|
|
|
return ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#elif defined(WIN32)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
struct Thread {
|
|
|
|
HANDLE thread;
|
2005-08-05 20:18:08 +00:00
|
|
|
ThreadFunc func;
|
2005-08-05 11:53:48 +00:00
|
|
|
void* arg;
|
|
|
|
void* ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
};
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
static DWORD WINAPI Proxy(LPVOID arg)
|
|
|
|
{
|
|
|
|
Thread* t = arg;
|
|
|
|
t->ret = t->func(t->arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
Thread* OTTDCreateThread(ThreadFunc function, void* arg)
|
|
|
|
{
|
|
|
|
Thread* t = malloc(sizeof(*t));
|
|
|
|
DWORD dwThreadId;
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
t->func = function;
|
|
|
|
t->arg = arg;
|
2005-08-05 20:18:08 +00:00
|
|
|
t->thread = CreateThread(NULL, 0, Proxy, t, 0, &dwThreadId);
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
if (t->thread != NULL) {
|
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
free(t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-05 11:53:48 +00:00
|
|
|
void* OTTDJoinThread(Thread* t)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2005-08-05 11:53:48 +00:00
|
|
|
void* ret;
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
WaitForSingleObject(t->thread, INFINITE);
|
|
|
|
CloseHandle(t->thread);
|
2005-08-05 11:53:48 +00:00
|
|
|
ret = t->ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
free(t);
|
2005-08-05 11:53:48 +00:00
|
|
|
return ret;
|
2005-08-05 09:15:41 +00:00
|
|
|
}
|
|
|
|
#endif
|