Rework the trip history window layout.
[openttd-joker.git] / src / thread / thread_morphos.cpp
blob779a9700065312ae1753959a1bf9c7b2492fb5fe
1 /* $Id: thread_morphos.cpp 17339 2009-09-01 10:07:22Z rubidium $ */
3 /*
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/>.
8 */
10 /** @file thread_morphos.cpp MorphOS implementation of Threads. */
12 #include "../stdafx.h"
13 #include "thread.h"
14 #include "../debug.h"
15 #include "../core/alloc_func.hpp"
16 #include <stdlib.h>
17 #include <unistd.h>
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"
28 /**
29 * avoid name clashes with MorphOS API functions
31 #undef Exit
32 #undef Wait
35 /**
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
49 /**
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);
58 #else
59 #define KPutStr(x)
60 #endif
63 /**
64 * MorphOS version for ThreadObject.
66 class ThreadObject_MorphOS : public ThreadObject {
67 private:
68 APTR m_thr; ///< System thread identifier.
69 struct MsgPort *m_replyport;
70 struct OTTDThreadStartupMessage m_msg;
71 bool self_destruct;
73 public:
74 /**
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)
80 struct Task *parent;
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 */
90 m_msg.func = proc;
91 m_msg.arg = param;
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,
106 NP_Priority, 5UL,
107 NP_Name, (IPTR)"OpenTTD Thread",
108 NP_PPCStackSize, 131072UL,
109 TAG_DONE);
111 m_thr = (APTR) child;
113 if (child != NULL) {
114 KPutStr("[OpenTTD] Child process launched.\n");
115 } else {
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 */
131 assert(IsCurrent());
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();
140 return true;
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);
154 GetMsg(m_replyport);
155 DeleteMsgPort(m_replyport);
156 m_thr = 0;
159 /* virtual */ bool IsCurrent()
161 return FindTask(NULL) == m_thr;
164 private:
166 * On thread creation, this function is called, which calls the real startup
167 * function. This to get back into the correct instance again.
169 static void Proxy()
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) {
180 try {
181 msg->func(msg->arg);
182 } catch(OTTDThreadExitSignal e) {
183 KPutStr("[Child] Returned to main()\n");
184 } catch(...) {
185 NOT_REACHED();
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;
200 return true;