1 /* $Id: thread_morphos.cpp 17339 2009-09-01 10:07:22Z rubidium $ */
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file thread_morphos.cpp MorphOS implementation of Threads. */
12 #include "../stdafx.h"
15 #include "../core/alloc_func.hpp"
19 #include <exec/types.h>
20 #include <exec/rawfmt.h>
21 #include <dos/dostags.h>
23 #include <proto/dos.h>
24 #include <proto/exec.h>
26 #include "../safeguards.h"
29 * avoid name clashes with MorphOS API functions
36 * NOTE: this code heavily depends on latest libnix updates. So make
37 * sure you link with new stuff which supports semaphore locking of
38 * the IO resources, else it will just go foobar.
42 struct OTTDThreadStartupMessage
{
43 struct Message msg
; ///< standard exec.library message (MUST be the first thing in the message struct!)
44 OTTDThreadFunc func
; ///< function the thread will execute
45 void *arg
; ///< functions arguments for the thread function
50 * Default OpenTTD STDIO/ERR debug output is not very useful for this, so we
51 * utilize serial/ramdebug instead.
53 #ifndef NO_DEBUG_MESSAGES
54 void KPutStr(CONST_STRPTR format
)
56 RawDoFmt(format
, NULL
, (void (*)())RAWFMTFUNC_SERIAL
, NULL
);
64 * MorphOS version for ThreadObject.
66 class ThreadObject_MorphOS
: public ThreadObject
{
68 APTR m_thr
; ///< System thread identifier.
69 struct MsgPort
*m_replyport
;
70 struct OTTDThreadStartupMessage m_msg
;
75 * Create a sub process and start it, calling proc(param).
77 ThreadObject_MorphOS(OTTDThreadFunc proc
, void *param
, self_destruct
) :
78 m_thr(0), self_destruct(self_destruct
)
82 KPutStr("[OpenTTD] Create thread...\n");
84 parent
= FindTask(NULL
);
86 /* Make sure main thread runs with sane priority */
87 SetTaskPri(parent
, 0);
89 /* Things we'll pass down to the child by utilizing NP_StartupMsg */
93 m_replyport
= CreateMsgPort();
95 if (m_replyport
!= NULL
) {
96 struct Process
*child
;
98 m_msg
.msg
.mn_Node
.ln_Type
= NT_MESSAGE
;
99 m_msg
.msg
.mn_ReplyPort
= m_replyport
;
100 m_msg
.msg
.mn_Length
= sizeof(struct OTTDThreadStartupMessage
);
102 child
= CreateNewProcTags(
103 NP_CodeType
, CODETYPE_PPC
,
104 NP_Entry
, ThreadObject_MorphOS::Proxy
,
105 NP_StartupMsg
, (IPTR
)&m_msg
,
107 NP_Name
, (IPTR
)"OpenTTD Thread",
108 NP_PPCStackSize
, 131072UL,
111 m_thr
= (APTR
) child
;
114 KPutStr("[OpenTTD] Child process launched.\n");
116 KPutStr("[OpenTTD] Couldn't create child process. (constructors never fail, yeah!)\n");
117 DeleteMsgPort(m_replyport
);
122 /* virtual */ ~ThreadObject_MorphOS()
126 /* virtual */ bool Exit()
128 struct OTTDThreadStartupMessage
*msg
;
130 /* You can only exit yourself */
133 KPutStr("[Child] Aborting...\n");
135 if (NewGetTaskAttrs(NULL
, &msg
, sizeof(struct OTTDThreadStartupMessage
*), TASKINFOTYPE_STARTUPMSG
, TAG_DONE
) && msg
!= NULL
) {
136 /* For now we terminate by throwing an error, gives much cleaner cleanup */
137 throw OTTDThreadExitSignal();
143 /* virtual */ void Join()
145 struct OTTDThreadStartupMessage
*reply
;
147 /* You cannot join yourself */
148 assert(!IsCurrent());
150 KPutStr("[OpenTTD] Join threads...\n");
151 KPutStr("[OpenTTD] Wait for child to quit...\n");
152 WaitPort(m_replyport
);
155 DeleteMsgPort(m_replyport
);
159 /* virtual */ bool IsCurrent()
161 return FindTask(NULL
) == m_thr
;
166 * On thread creation, this function is called, which calls the real startup
167 * function. This to get back into the correct instance again.
171 struct Task
*child
= FindTask(NULL
);
172 struct OTTDThreadStartupMessage
*msg
;
174 /* Make sure, we don't block the parent. */
175 SetTaskPri(child
, -5);
177 KPutStr("[Child] Progressing...\n");
179 if (NewGetTaskAttrs(NULL
, &msg
, sizeof(struct OTTDThreadStartupMessage
*), TASKINFOTYPE_STARTUPMSG
, TAG_DONE
) && msg
!= NULL
) {
182 } catch(OTTDThreadExitSignal e
) {
183 KPutStr("[Child] Returned to main()\n");
189 /* Quit the child, exec.library will reply the startup msg internally. */
190 KPutStr("[Child] Done.\n");
192 if (self_destruct
) delete this;
196 /* static */ bool ThreadObject::New(OTTDThreadFunc proc
, void *param
, ThreadObject
**thread
, const char *name
)
198 ThreadObject
*to
= new ThreadObject_MorphOS(proc
, param
, thread
== NULL
);
199 if (thread
!= NULL
) *thread
= to
;