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