src/thread.cpp
author peter1138
Tue, 22 Jan 2008 07:27:06 +0000
changeset 8374 7a1b6c89cb89
parent 8130 d2eb7d04f6e1
permissions -rw-r--r--
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     1
/* $Id$ */
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     2
6422
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6247
diff changeset
     3
/** @file thread.cpp */
6679df1c05ba (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents: 6247
diff changeset
     4
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     5
#include "stdafx.h"
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     6
#include "thread.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 6557
diff changeset
     7
#include "core/alloc_func.hpp"
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     8
#include <stdlib.h>
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     9
6557
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
    10
#if defined(__AMIGA__) || defined(PSP) || defined(NO_THREADS)
4324
9682c016b892 (svn r5978) -Add: allow a switch in Makefile.config to disable threads in OpenTTD (no matter what system you are on). Only useful for testing.
truelight
parents: 4300
diff changeset
    11
OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; }
9682c016b892 (svn r5978) -Add: allow a switch in Makefile.config to disable threads in OpenTTD (no matter what system you are on). Only useful for testing.
truelight
parents: 4300
diff changeset
    12
void *OTTDJoinThread(OTTDThread *t) { return NULL; }
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5979
diff changeset
    13
void OTTDExitThread() { NOT_REACHED(); };
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    14
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    15
#elif defined(__OS2__)
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    16
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    17
#define INCL_DOS
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    18
#include <os2.h>
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    19
#include <process.h>
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    20
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    21
struct OTTDThread {
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    22
	TID thread;
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    23
	OTTDThreadFunc func;
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    24
	void* arg;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    25
	void* ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    26
};
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    27
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    28
static void Proxy(void* arg)
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    29
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    30
	OTTDThread* t = (OTTDThread*)arg;
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    31
	t->ret = t->func(t->arg);
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    32
}
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    33
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    34
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    35
{
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    36
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    37
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    38
	if (t == NULL) return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    39
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    40
	t->func = function;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    41
	t->arg  = arg;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    42
	t->thread = _beginthread(Proxy, NULL, 32768, t);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    43
	if (t->thread != (TID)-1) {
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    44
		return t;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    45
	} else {
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    46
		free(t);
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    47
		return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    48
	}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    49
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    50
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    51
void* OTTDJoinThread(OTTDThread* t)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    52
{
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    53
	void* ret;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    54
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    55
	if (t == NULL) return NULL;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    56
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    57
	DosWaitThread(&t->thread, DCWW_WAIT);
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    58
	ret = t->ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    59
	free(t);
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    60
	return ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    61
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    62
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5979
diff changeset
    63
void OTTDExitThread()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    64
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    65
	_endthread();
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    66
}
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    67
6557
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
    68
#elif defined(UNIX) && !defined(MORPHOS)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    69
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    70
#include <pthread.h>
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    71
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    72
struct OTTDThread {
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    73
	pthread_t thread;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    74
};
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    75
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    76
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    77
{
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    78
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    79
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    80
	if (t == NULL) return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    81
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    82
	if (pthread_create(&t->thread, NULL, function, arg) == 0) {
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    83
		return t;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    84
	} else {
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    85
		free(t);
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    86
		return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    87
	}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    88
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    89
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    90
void* OTTDJoinThread(OTTDThread* t)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    91
{
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    92
	void* ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    93
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    94
	if (t == NULL) return NULL;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    95
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    96
	pthread_join(t->thread, &ret);
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    97
	free(t);
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    98
	return ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    99
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   100
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5979
diff changeset
   101
void OTTDExitThread()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   102
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   103
	pthread_exit(NULL);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   104
}
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   105
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   106
#elif defined(WIN32)
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   107
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   108
#include <windows.h>
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   109
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   110
struct OTTDThread {
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   111
	HANDLE thread;
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   112
	OTTDThreadFunc func;
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   113
	void* arg;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   114
	void* ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   115
};
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   116
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   117
static DWORD WINAPI Proxy(LPVOID arg)
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   118
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   119
	OTTDThread* t = (OTTDThread*)arg;
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   120
	t->ret = t->func(t->arg);
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   121
	return 0;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   122
}
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   123
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   124
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   125
{
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   126
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   127
	DWORD dwThreadId;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   128
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   129
	if (t == NULL) return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   130
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   131
	t->func = function;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   132
	t->arg  = arg;
2287
3e541bce2410 (svn r2811) Fix typos in r2810
tron
parents: 2286
diff changeset
   133
	t->thread = CreateThread(NULL, 0, Proxy, t, 0, &dwThreadId);
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   134
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   135
	if (t->thread != NULL) {
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   136
		return t;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   137
	} else {
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   138
		free(t);
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   139
		return NULL;
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   140
	}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   141
}
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   142
4298
b926a6eaaa70 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   143
void* OTTDJoinThread(OTTDThread* t)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   144
{
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   145
	void* ret;
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   146
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   147
	if (t == NULL) return NULL;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   148
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   149
	WaitForSingleObject(t->thread, INFINITE);
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   150
	CloseHandle(t->thread);
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   151
	ret = t->ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   152
	free(t);
2286
733dbf6b4545 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   153
	return ret;
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   154
}
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   155
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5979
diff changeset
   156
void OTTDExitThread()
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   157
{
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   158
	ExitThread(0);
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   159
}
6557
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   160
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   161
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   162
#elif defined(MORPHOS)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   163
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   164
#include <exec/types.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   165
#include <exec/rawfmt.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   166
#include <dos/dostags.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   167
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   168
#include <proto/dos.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   169
#include <proto/exec.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   170
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   171
#include <setjmp.h>
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   172
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   173
/* NOTE: this code heavily depends on latest libnix updates. So make
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   174
 *        sure you link with new stuff which supports semaphore locking of
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   175
 *        the IO resources, else it will just go foobar. */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   176
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   177
struct OTTDThreadStartupMessage {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   178
	struct Message msg;  ///< standard exec.library message (MUST be the first thing in the message struct!)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   179
	OTTDThreadFunc func; ///< function the thread will execute
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   180
	void *arg;           ///< functions arguments for the thread function
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   181
	void *ret;           ///< return value of the thread function
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   182
	jmp_buf jumpstore;   ///< storage for the setjump state
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   183
};
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   184
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   185
struct OTTDThread {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   186
	struct MsgPort *replyport;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   187
	struct OTTDThreadStartupMessage msg;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   188
};
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   189
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   190
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   191
/**
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   192
 *  Default OpenTTD STDIO/ERR debug output is not very useful for this, so we
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   193
 *  utilize serial/ramdebug instead.
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   194
 */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   195
#ifndef NO_DEBUG_MESSAGES
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   196
void KPutStr(CONST_STRPTR format)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   197
{
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   198
	RawDoFmt(format, NULL, (void (*)())RAWFMTFUNC_SERIAL, NULL);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   199
}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   200
#else
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   201
#define KPutStr(x)
2285
410dedcf46d1 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   202
#endif
6557
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   203
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   204
static void Proxy(void)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   205
{
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   206
	struct Task *child = FindTask(NULL);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   207
	struct OTTDThreadStartupMessage *msg;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   208
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   209
	/* Make sure, we don't block the parent. */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   210
	SetTaskPri(child, -5);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   211
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   212
	KPutStr("[Child] Progressing...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   213
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   214
	if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   215
		/* Make use of setjmp() here, so this point can be reached again from inside
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   216
		 *  OTTDExitThread() which can be called from anythere inside msg->func.
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   217
		 *  It's a bit ugly and in worst case it leaks some memory. */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   218
		if (setjmp(msg->jumpstore) == 0) {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   219
			msg->ret = msg->func(msg->arg);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   220
		} else {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   221
			KPutStr("[Child] Returned to main()\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   222
		}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   223
	}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   224
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   225
	/*  Quit the child, exec.library will reply the startup msg internally. */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   226
	KPutStr("[Child] Done.\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   227
}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   228
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   229
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void *arg)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   230
{
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   231
	OTTDThread *t;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   232
	struct Task *parent;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   233
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   234
	KPutStr("[OpenTTD] Create thread...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   235
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   236
	t = (struct OTTDThread *)AllocVecTaskPooled(sizeof(struct OTTDThread));
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   237
	if (t == NULL) return NULL;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   238
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   239
	parent = FindTask(NULL);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   240
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   241
	/* Make sure main thread runs with sane priority */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   242
	SetTaskPri(parent, 0);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   243
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   244
	/* Things we'll pass down to the child by utilizing NP_StartupMsg */
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   245
	t->msg.func = function;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   246
	t->msg.arg  = arg;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   247
	t->msg.ret  = NULL;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   248
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   249
	t->replyport = CreateMsgPort();
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   250
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   251
	if (t->replyport != NULL) {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   252
		struct Process *child;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   253
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   254
		t->msg.msg.mn_Node.ln_Type = NT_MESSAGE;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   255
		t->msg.msg.mn_ReplyPort    = t->replyport;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   256
		t->msg.msg.mn_Length       = sizeof(struct OTTDThreadStartupMessage);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   257
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   258
		child = CreateNewProcTags(
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   259
			NP_CodeType,     CODETYPE_PPC,
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   260
			NP_Entry,        Proxy,
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   261
			NP_StartupMsg,   (ULONG)&t->msg,
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   262
			NP_Priority,     5UL,
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   263
			NP_Name,         (ULONG)"OpenTTD Thread",
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   264
			NP_PPCStackSize, 131072UL,
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   265
			TAG_DONE);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   266
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   267
		if (child != NULL) {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   268
			KPutStr("[OpenTTD] Child process launched.\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   269
			return t;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   270
		}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   271
		DeleteMsgPort(t->replyport);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   272
	}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   273
	FreeVecTaskPooled(t);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   274
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   275
	return NULL;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   276
}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   277
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   278
void* OTTDJoinThread(OTTDThread *t)
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   279
{
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   280
	struct OTTDThreadStartupMessage *reply;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   281
	void *ret;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   282
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   283
	KPutStr("[OpenTTD] Join threads...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   284
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   285
	if (t == NULL) return NULL;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   286
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   287
	KPutStr("[OpenTTD] Wait for child to quit...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   288
	WaitPort(t->replyport);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   289
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   290
	reply = (struct OTTDThreadStartupMessage *)GetMsg(t->replyport);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   291
	ret   = reply->ret;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   292
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   293
	DeleteMsgPort(t->replyport);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   294
	FreeVecTaskPooled(t);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   295
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   296
	return ret;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   297
}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   298
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   299
void OTTDExitThread()
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   300
{
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   301
	struct OTTDThreadStartupMessage *msg;
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   302
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   303
	KPutStr("[Child] Aborting...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   304
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   305
	if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) {
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   306
		KPutStr("[Child] Jumping back...\n");
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   307
		longjmp(msg->jumpstore, 0xBEAFCAFE);
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   308
	}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   309
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   310
	NOT_REACHED();
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   311
}
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   312
36dcf7f8a84c (svn r9759) -Add: added threading code for MorphOS (tokai)
truelight
parents: 6422
diff changeset
   313
#endif