summaryrefslogtreecommitdiff
path: root/src/thread.cpp
blob: 61b6ccabbebfff2bf45d58d8b5ec8bf381395385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* $Id$ */

/** @file thread.cpp */

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

#if defined(__AMIGA__) || 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) && !defined(MORPHOS)

#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);
}


#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();
}

#endif