Files @ r6250:8f26e7e58ba0
Branch filter:

Location: cpp/openttd-patchpack/source/src/thread.cpp

miham
(svn r9054) -Update: WebTranslator2 update to 2007-03-07 18:39:14
catalan - 2 fixed by arnaullv (2)
croatian - 95 fixed, 34 changed by Ydobon (129)
czech - 1 fixed, 3 deleted, 109 changed by Hadez (113)
danish - 4 fixed by ThomasA (4)
dutch - 2 fixed by habell (2)
esperanto - 5 fixed by LaPingvino (5)
french - 2 fixed by glx (2)
german - 1 fixed by Neonox (1)
hungarian - 1 fixed by miham (1)
italian - 1 fixed by sidew (1)
japanese - 14 fixed by ickoonite (14)
korean - 9 fixed by Nios (9)
norwegian_nynorsk - 1 fixed by Eikje3 (1)
polish - 2 fixed by meush (2)
simplified_chinese - 5 fixed by Fishingsnow (5)
slovenian - 1 fixed by Necrolyte (1)
spanish - 5 fixed by eusebio (5)
swedish - 5 fixed by daishan (5)
traditional_chinese - 7 fixed by sam0737 (7)
ukrainian - 1 fixed by znikoz (1)
/* $Id$ */

#include "stdafx.h"
#include "thread.h"
#include <stdlib.h>
#include "helpers.hpp"

#if defined(__AMIGA__) || defined(__MORPHOS__) || defined(PSP) || defined(NO_THREADS)
OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; }
void *OTTDJoinThread(OTTDThread *t) { return NULL; }
void OTTDExitThread() { NOT_REACHED(); };

#elif defined(__OS2__)

#define INCL_DOS
#include <os2.h>
#include <process.h>

struct OTTDThread {
	TID thread;
	OTTDThreadFunc func;
	void* arg;
	void* ret;
};

static void Proxy(void* arg)
{
	OTTDThread* t = (OTTDThread*)arg;
	t->ret = t->func(t->arg);
}

OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
{
	OTTDThread* t = MallocT<OTTDThread>(1);

	if (t == NULL) return NULL;

	t->func = function;
	t->arg  = arg;
	t->thread = _beginthread(Proxy, NULL, 32768, t);
	if (t->thread != (TID)-1) {
		return t;
	} else {
		free(t);
		return NULL;
	}
}

void* OTTDJoinThread(OTTDThread* t)
{
	void* ret;

	if (t == NULL) return NULL;

	DosWaitThread(&t->thread, DCWW_WAIT);
	ret = t->ret;
	free(t);
	return ret;
}

void OTTDExitThread()
{
	_endthread();
}

#elif defined(UNIX)

#include <pthread.h>

struct OTTDThread {
	pthread_t thread;
};

OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
{
	OTTDThread* t = MallocT<OTTDThread>(1);

	if (t == NULL) return NULL;

	if (pthread_create(&t->thread, NULL, function, arg) == 0) {
		return t;
	} else {
		free(t);
		return NULL;
	}
}

void* OTTDJoinThread(OTTDThread* t)
{
	void* ret;

	if (t == NULL) return NULL;

	pthread_join(t->thread, &ret);
	free(t);
	return ret;
}

void OTTDExitThread()
{
	pthread_exit(NULL);
}

#elif defined(WIN32)

#include <windows.h>

struct OTTDThread {
	HANDLE thread;
	OTTDThreadFunc func;
	void* arg;
	void* ret;
};

static DWORD WINAPI Proxy(LPVOID arg)
{
	OTTDThread* t = (OTTDThread*)arg;
	t->ret = t->func(t->arg);
	return 0;
}

OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
{
	OTTDThread* t = MallocT<OTTDThread>(1);
	DWORD dwThreadId;

	if (t == NULL) return NULL;

	t->func = function;
	t->arg  = arg;
	t->thread = CreateThread(NULL, 0, Proxy, t, 0, &dwThreadId);

	if (t->thread != NULL) {
		return t;
	} else {
		free(t);
		return NULL;
	}
}

void* OTTDJoinThread(OTTDThread* t)
{
	void* ret;

	if (t == NULL) return NULL;

	WaitForSingleObject(t->thread, INFINITE);
	CloseHandle(t->thread);
	ret = t->ret;
	free(t);
	return ret;
}

void OTTDExitThread()
{
	ExitThread(0);
}
#endif