author | truebrain |
Mon, 30 Jun 2008 21:31:23 +0000 | |
branch | noai |
changeset 11111 | 1b984dab8cec |
parent 11095 | a0da321a39b3 |
permissions | -rw-r--r-- |
10455 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file thread_morphos.cpp MorphOS implementation of Threads. */ |
|
4 |
||
5 |
#include "stdafx.h" |
|
6 |
#include "thread.h" |
|
7 |
#include "debug.h" |
|
8 |
#include "core/alloc_func.hpp" |
|
9 |
#include <stdlib.h> |
|
10 |
#include <unistd.h> |
|
11 |
||
12 |
#include <exec/types.h> |
|
13 |
#include <exec/rawfmt.h> |
|
14 |
#include <dos/dostags.h> |
|
15 |
||
16 |
#include <proto/dos.h> |
|
17 |
#include <proto/exec.h> |
|
18 |
||
19 |
/** |
|
20 |
* avoid name clashes with MorphOS API functions |
|
21 |
*/ |
|
22 |
#undef Exit |
|
23 |
#undef Wait |
|
24 |
||
25 |
||
26 |
/** |
|
27 |
* NOTE: this code heavily depends on latest libnix updates. So make |
|
28 |
* sure you link with new stuff which supports semaphore locking of |
|
29 |
* the IO resources, else it will just go foobar. |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
struct OTTDThreadStartupMessage { |
|
34 |
struct Message msg; ///< standard exec.library message (MUST be the first thing in the message struct!) |
|
35 |
OTTDThreadFunc func; ///< function the thread will execute |
|
36 |
void *arg; ///< functions arguments for the thread function |
|
37 |
}; |
|
38 |
||
39 |
||
40 |
/** |
|
41 |
* Default OpenTTD STDIO/ERR debug output is not very useful for this, so we |
|
42 |
* utilize serial/ramdebug instead. |
|
43 |
*/ |
|
44 |
#ifndef NO_DEBUG_MESSAGES |
|
45 |
void KPutStr(CONST_STRPTR format) |
|
46 |
{ |
|
47 |
RawDoFmt(format, NULL, (void (*)())RAWFMTFUNC_SERIAL, NULL); |
|
48 |
} |
|
49 |
#else |
|
50 |
#define KPutStr(x) |
|
51 |
#endif |
|
52 |
||
53 |
||
54 |
/** |
|
55 |
* MorphOS version for ThreadObject. |
|
56 |
*/ |
|
57 |
class ThreadObject_MorphOS : public ThreadObject { |
|
58 |
private: |
|
59 |
APTR m_thr; ///< System thread identifier. |
|
60 |
struct MsgPort *m_replyport; |
|
61 |
struct OTTDThreadStartupMessage m_msg; |
|
62 |
||
63 |
public: |
|
64 |
/** |
|
65 |
* Create a sub process and start it, calling proc(param). |
|
66 |
*/ |
|
67 |
ThreadObject_MorphOS(OTTDThreadFunc proc, void *param) : m_thr(0) |
|
68 |
{ |
|
69 |
struct Task *parent; |
|
70 |
||
71 |
KPutStr("[OpenTTD] Create thread...\n"); |
|
72 |
||
73 |
parent = FindTask(NULL); |
|
74 |
||
75 |
/* Make sure main thread runs with sane priority */ |
|
76 |
SetTaskPri(parent, 0); |
|
77 |
||
78 |
/* Things we'll pass down to the child by utilizing NP_StartupMsg */ |
|
79 |
m_msg.func = proc; |
|
80 |
m_msg.arg = param; |
|
81 |
||
82 |
m_replyport = CreateMsgPort(); |
|
83 |
||
84 |
if (m_replyport != NULL) { |
|
85 |
struct Process *child; |
|
86 |
||
87 |
m_msg.msg.mn_Node.ln_Type = NT_MESSAGE; |
|
88 |
m_msg.msg.mn_ReplyPort = m_replyport; |
|
89 |
m_msg.msg.mn_Length = sizeof(struct OTTDThreadStartupMessage); |
|
90 |
||
91 |
child = CreateNewProcTags( |
|
92 |
NP_CodeType, CODETYPE_PPC, |
|
93 |
NP_Entry, ThreadObject_MorphOS::Proxy, |
|
94 |
NP_StartupMsg, (IPTR)&m_msg, |
|
95 |
NP_Priority, 5UL, |
|
96 |
NP_Name, (IPTR)"OpenTTD Thread", |
|
97 |
NP_PPCStackSize, 131072UL, |
|
98 |
TAG_DONE); |
|
99 |
||
100 |
m_thr = (APTR) child; |
|
101 |
||
102 |
if (child != NULL) { |
|
103 |
KPutStr("[OpenTTD] Child process launched.\n"); |
|
104 |
} else { |
|
105 |
KPutStr("[OpenTTD] Couldn't create child process. (constructors never fail, yeah!)\n"); |
|
106 |
DeleteMsgPort(m_replyport); |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Create a thread and attach current thread to it. |
|
113 |
*/ |
|
114 |
ThreadObject_MorphOS() : m_thr(0) |
|
115 |
{ |
|
116 |
m_thr = FindTask(NULL); |
|
117 |
} |
|
118 |
||
119 |
/* virtual */ ~ThreadObject_MorphOS() |
|
120 |
{ |
|
121 |
} |
|
122 |
||
123 |
/* virtual */ bool IsRunning() |
|
124 |
{ |
|
125 |
return m_thr != 0; |
|
126 |
} |
|
127 |
||
128 |
/* virtual */ bool WaitForStop() |
|
129 |
{ |
|
130 |
/* You can't wait on yourself */ |
|
131 |
assert(!IsCurrent()); |
|
132 |
/* If the thread is not running, waiting is over */ |
|
133 |
if (!IsRunning()) return true; |
|
134 |
||
135 |
WaitPort(m_replyport); |
|
136 |
||
137 |
GetMsg(m_replyport); |
|
138 |
DeleteMsgPort(m_replyport); |
|
139 |
||
140 |
return true; |
|
141 |
} |
|
142 |
||
143 |
/* virtual */ bool Exit() |
|
144 |
{ |
|
145 |
struct OTTDThreadStartupMessage *msg; |
|
146 |
||
147 |
/* You can only exit yourself */ |
|
148 |
assert(IsCurrent()); |
|
149 |
/* If the thread is not running, we are already closed */ |
|
150 |
if (!IsRunning()) return false; |
|
151 |
||
152 |
KPutStr("[Child] Aborting...\n"); |
|
153 |
||
154 |
if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) { |
|
155 |
/* For now we terminate by throwing an error, gives much cleaner cleanup */ |
|
11095
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
156 |
throw OTTDThreadExitSignal(); |
10455 | 157 |
} |
158 |
||
159 |
return true; |
|
160 |
} |
|
161 |
||
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
162 |
/* virtual */ void Join() |
10455 | 163 |
{ |
164 |
struct OTTDThreadStartupMessage *reply; |
|
165 |
||
166 |
/* You cannot join yourself */ |
|
167 |
assert(!IsCurrent()); |
|
168 |
||
169 |
KPutStr("[OpenTTD] Join threads...\n"); |
|
170 |
KPutStr("[OpenTTD] Wait for child to quit...\n"); |
|
171 |
WaitPort(m_replyport); |
|
172 |
||
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
173 |
GetMsg(m_replyport); |
10455 | 174 |
DeleteMsgPort(m_replyport); |
175 |
m_thr = 0; |
|
176 |
} |
|
177 |
||
178 |
/* virtual */ bool IsCurrent() |
|
179 |
{ |
|
180 |
return FindTask(NULL) == m_thr; |
|
181 |
} |
|
182 |
||
183 |
/* virtual */ uint GetId() |
|
184 |
{ |
|
185 |
return (uint)m_thr; |
|
186 |
} |
|
187 |
||
188 |
private: |
|
189 |
/** |
|
190 |
* On thread creation, this function is called, which calls the real startup |
|
191 |
* function. This to get back into the correct instance again. |
|
192 |
*/ |
|
193 |
static void Proxy(void) |
|
194 |
{ |
|
195 |
struct Task *child = FindTask(NULL); |
|
196 |
struct OTTDThreadStartupMessage *msg; |
|
197 |
||
198 |
/* Make sure, we don't block the parent. */ |
|
199 |
SetTaskPri(child, -5); |
|
200 |
||
201 |
KPutStr("[Child] Progressing...\n"); |
|
202 |
||
203 |
if (NewGetTaskAttrs(NULL, &msg, sizeof(struct OTTDThreadStartupMessage *), TASKINFOTYPE_STARTUPMSG, TAG_DONE) && msg != NULL) { |
|
204 |
try { |
|
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
205 |
msg->func(msg->arg); |
11095
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
206 |
} catch(OTTDThreadExitSignal e) { |
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
207 |
KPutStr("[Child] Returned to main()\n"); |
10455 | 208 |
} catch(...) { |
11095
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
209 |
NOT_REACHED(); |
10455 | 210 |
} |
211 |
} |
|
212 |
||
213 |
/* Quit the child, exec.library will reply the startup msg internally. */ |
|
214 |
KPutStr("[Child] Done.\n"); |
|
215 |
} |
|
216 |
}; |
|
217 |
||
218 |
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param) |
|
219 |
{ |
|
220 |
return new ThreadObject_MorphOS(proc, param); |
|
221 |
} |
|
222 |
||
223 |
/* static */ ThreadObject *ThreadObject::AttachCurrent() |
|
224 |
{ |
|
225 |
return new ThreadObject_MorphOS(); |
|
226 |
} |
|
227 |
||
228 |
/* static */ uint ThreadObject::CurrentId() |
|
229 |
{ |
|
230 |
return (uint) FindTask(NULL); |
|
231 |
} |
|
232 |
||
233 |
||
234 |
/** |
|
235 |
* MorphOS version of ThreadSemaphore. |
|
236 |
*/ |
|
237 |
class ThreadSemaphore_MorphOS : public ThreadSemaphore { |
|
238 |
private: |
|
239 |
struct SignalSemaphore m_sem; |
|
240 |
||
241 |
public: |
|
242 |
ThreadSemaphore_MorphOS() |
|
243 |
{ |
|
244 |
InitSemaphore(&m_sem); |
|
245 |
} |
|
246 |
||
247 |
/* virtual */ ~ThreadSemaphore_MorphOS() |
|
248 |
{ |
|
249 |
||
250 |
} |
|
251 |
||
252 |
/* virtual */ void Set() |
|
253 |
{ |
|
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
254 |
/* Check if semaphore count is really important there. */ |
10455 | 255 |
ReleaseSemaphore(&m_sem); |
256 |
} |
|
257 |
||
258 |
/* virtual */ void Wait() |
|
259 |
{ |
|
260 |
ObtainSemaphore(&m_sem); |
|
261 |
} |
|
262 |
}; |
|
263 |
||
264 |
/* static */ ThreadSemaphore *ThreadSemaphore::New() |
|
265 |
{ |
|
266 |
return new ThreadSemaphore_MorphOS(); |
|
267 |
} |