2005-08-05 09:15:41 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-04 03:21:14 +00:00
|
|
|
/** @file thread.cpp */
|
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include <stdlib.h>
|
2007-01-10 18:56:51 +00:00
|
|
|
#include "helpers.hpp"
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2007-05-01 15:25:00 +00:00
|
|
|
#if defined(__AMIGA__) || defined(PSP) || defined(NO_THREADS)
|
2006-08-20 13:48:04 +00:00
|
|
|
OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; }
|
|
|
|
void *OTTDJoinThread(OTTDThread *t) { return NULL; }
|
2007-03-07 11:47:46 +00:00
|
|
|
void OTTDExitThread() { NOT_REACHED(); };
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
|
|
|
|
#define INCL_DOS
|
|
|
|
#include <os2.h>
|
|
|
|
#include <process.h>
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
struct OTTDThread {
|
2005-08-05 09:15:41 +00:00
|
|
|
TID thread;
|
2006-08-19 09:23:48 +00:00
|
|
|
OTTDThreadFunc 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)
|
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
OTTDThread* t = (OTTDThread*)arg;
|
2005-08-05 11:53:48 +00:00
|
|
|
t->ret = t->func(t->arg);
|
|
|
|
}
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2007-01-11 17:29:39 +00:00
|
|
|
OTTDThread* t = MallocT<OTTDThread>(1);
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
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);
|
2007-01-10 18:56:51 +00:00
|
|
|
if (t->thread != (TID)-1) {
|
2005-08-05 09:15:41 +00:00
|
|
|
return t;
|
|
|
|
} else {
|
|
|
|
free(t);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
void* OTTDJoinThread(OTTDThread* 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
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OTTDExitThread()
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
{
|
|
|
|
_endthread();
|
|
|
|
}
|
2005-08-05 09:15:41 +00:00
|
|
|
|
2007-05-01 15:25:00 +00:00
|
|
|
#elif defined(UNIX) && !defined(MORPHOS)
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
struct OTTDThread {
|
2005-08-05 09:15:41 +00:00
|
|
|
pthread_t thread;
|
|
|
|
};
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2007-01-11 17:29:39 +00:00
|
|
|
OTTDThread* t = MallocT<OTTDThread>(1);
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
void* OTTDJoinThread(OTTDThread* 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
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OTTDExitThread()
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
{
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
2005-08-05 09:15:41 +00:00
|
|
|
|
|
|
|
#elif defined(WIN32)
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
struct OTTDThread {
|
2005-08-05 09:15:41 +00:00
|
|
|
HANDLE thread;
|
2006-08-19 09:23:48 +00:00
|
|
|
OTTDThreadFunc 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)
|
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
OTTDThread* t = (OTTDThread*)arg;
|
2005-08-05 11:53:48 +00:00
|
|
|
t->ret = t->func(t->arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
|
2005-08-05 09:15:41 +00:00
|
|
|
{
|
2007-01-11 17:29:39 +00:00
|
|
|
OTTDThread* t = MallocT<OTTDThread>(1);
|
2005-08-05 09:15:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-19 09:23:48 +00:00
|
|
|
void* OTTDJoinThread(OTTDThread* 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
|
|
|
}
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OTTDExitThread()
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
{
|
|
|
|
ExitThread(0);
|
|
|
|
}
|
2007-05-01 15:25:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#elif defined(MORPHOS)
|
|
|
|
|
|
|
|
#include <exec/types.h>
|
|
|
|
#include <exec/rawfmt.h>
|
|
|
|
#include <dos/dostags.h>
|
|
|
|
|
|
|
|
#include <proto/dos.h>
|
|
|
|
#include <proto/exec.h>
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
/* NOTE: this code heavily depends on latest libnix updates. So make
|
|
|
|
* sure you link with new stuff which supports semaphore locking of
|
|
|
|
* the IO resources, else it will just go foobar. */
|
|
|
|
|
|
|
|
struct OTTDThreadStartupMessage {
|
|
|
|
struct Message msg; ///< standard exec.library message (MUST be the first thing in the message struct!)
|
|
|
|
OTTDThreadFunc func; ///< function the thread will execute
|
|
|
|
void *arg; ///< functions arguments for the thread function
|
|
|
|
void *ret; ///< return value of the thread function
|
|
|
|
jmp_buf jumpstore; ///< storage for the setjump state
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OTTDThread {
|
|
|
|
struct MsgPort *replyport;
|
|
|
|
struct OTTDThreadStartupMessage msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default OpenTTD STDIO/ERR debug output is not very useful for this, so we
|
|
|
|
* utilize serial/ramdebug instead.
|
|
|
|
*/
|
|
|
|
#ifndef NO_DEBUG_MESSAGES
|
|
|
|
void KPutStr(CONST_STRPTR format)
|
|
|
|
{
|
|
|
|
RawDoFmt(format, NULL, (void (*)())RAWFMTFUNC_SERIAL, NULL);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define KPutStr(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void Proxy(void)
|
|
|
|
{
|
|
|
|
struct Task *child = FindTask(NULL);
|
|
|
|
struct OTTDThreadStartupMessage *msg;
|
|
|
|
|
|
|
|
/* Make sure, we don't block the parent. */
|
|
|
|
SetTaskPri(child, -5);
|
|
|
|
|
|
|
|
KPutStr("[Child] Progressing...\n");
|
|
|
|
|
|
|
|
if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) {
|
|
|
|
/* Make use of setjmp() here, so this point can be reached again from inside
|
|
|
|
* OTTDExitThread() which can be called from anythere inside msg->func.
|
|
|
|
* It's a bit ugly and in worst case it leaks some memory. */
|
|
|
|
if (setjmp(msg->jumpstore) == 0) {
|
|
|
|
msg->ret = msg->func(msg->arg);
|
|
|
|
} else {
|
|
|
|
KPutStr("[Child] Returned to main()\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Quit the child, exec.library will reply the startup msg internally. */
|
|
|
|
KPutStr("[Child] Done.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void *arg)
|
|
|
|
{
|
|
|
|
OTTDThread *t;
|
|
|
|
struct Task *parent;
|
|
|
|
|
|
|
|
KPutStr("[OpenTTD] Create thread...\n");
|
|
|
|
|
|
|
|
t = (struct OTTDThread *)AllocVecTaskPooled(sizeof(struct OTTDThread));
|
|
|
|
if (t == NULL) return NULL;
|
|
|
|
|
|
|
|
parent = FindTask(NULL);
|
|
|
|
|
|
|
|
/* Make sure main thread runs with sane priority */
|
|
|
|
SetTaskPri(parent, 0);
|
|
|
|
|
|
|
|
/* Things we'll pass down to the child by utilizing NP_StartupMsg */
|
|
|
|
t->msg.func = function;
|
|
|
|
t->msg.arg = arg;
|
|
|
|
t->msg.ret = NULL;
|
|
|
|
|
|
|
|
t->replyport = CreateMsgPort();
|
|
|
|
|
|
|
|
if (t->replyport != NULL) {
|
|
|
|
struct Process *child;
|
|
|
|
|
|
|
|
t->msg.msg.mn_Node.ln_Type = NT_MESSAGE;
|
|
|
|
t->msg.msg.mn_ReplyPort = t->replyport;
|
|
|
|
t->msg.msg.mn_Length = sizeof(struct OTTDThreadStartupMessage);
|
|
|
|
|
|
|
|
child = CreateNewProcTags(
|
|
|
|
NP_CodeType, CODETYPE_PPC,
|
|
|
|
NP_Entry, Proxy,
|
|
|
|
NP_StartupMsg, (ULONG)&t->msg,
|
|
|
|
NP_Priority, 5UL,
|
|
|
|
NP_Name, (ULONG)"OpenTTD Thread",
|
|
|
|
NP_PPCStackSize, 131072UL,
|
|
|
|
TAG_DONE);
|
|
|
|
|
|
|
|
if (child != NULL) {
|
|
|
|
KPutStr("[OpenTTD] Child process launched.\n");
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
DeleteMsgPort(t->replyport);
|
|
|
|
}
|
|
|
|
FreeVecTaskPooled(t);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* OTTDJoinThread(OTTDThread *t)
|
|
|
|
{
|
|
|
|
struct OTTDThreadStartupMessage *reply;
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
KPutStr("[OpenTTD] Join threads...\n");
|
|
|
|
|
|
|
|
if (t == NULL) return NULL;
|
|
|
|
|
|
|
|
KPutStr("[OpenTTD] Wait for child to quit...\n");
|
|
|
|
WaitPort(t->replyport);
|
|
|
|
|
|
|
|
reply = (struct OTTDThreadStartupMessage *)GetMsg(t->replyport);
|
|
|
|
ret = reply->ret;
|
|
|
|
|
|
|
|
DeleteMsgPort(t->replyport);
|
|
|
|
FreeVecTaskPooled(t);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OTTDExitThread()
|
|
|
|
{
|
|
|
|
struct OTTDThreadStartupMessage *msg;
|
|
|
|
|
|
|
|
KPutStr("[Child] Aborting...\n");
|
|
|
|
|
|
|
|
if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) {
|
|
|
|
KPutStr("[Child] Jumping back...\n");
|
|
|
|
longjmp(msg->jumpstore, 0xBEAFCAFE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
#endif
|