src/thread_morphos.cpp
author bjarni
Thu, 19 Jun 2008 17:54:23 +0000
changeset 9561 f236daaaf93a
parent 9479 c8ab793e4595
permissions -rw-r--r--
(svn r13584) -Fix: [OSX] Fixed issue where 10.5 failed to switch to fullscreen
This is done by selecting the 32bpp-anim blitter by default as it seems Apple removed some 8bpp support
Since this is done at runtime the same binary will still select 8bpp on 10.3 and 10.4
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     1
/* $Id$ */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     2
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     3
/** @file thread_morphos.cpp MorphOS implementation of Threads. */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     4
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     5
#include "stdafx.h"
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     6
#include "thread.h"
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     7
#include "debug.h"
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     8
#include "core/alloc_func.hpp"
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
     9
#include <stdlib.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    10
#include <unistd.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    11
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    12
#include <exec/types.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    13
#include <exec/rawfmt.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    14
#include <dos/dostags.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    15
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    16
#include <proto/dos.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    17
#include <proto/exec.h>
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    18
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    19
/**
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    20
 *  avoid name clashes with MorphOS API functions
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    21
 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    22
#undef Exit
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    23
#undef Wait
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    24
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    25
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    26
/**
37a280682fcf (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
37a280682fcf (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
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    29
 *        the IO resources, else it will just go foobar.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    30
 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    31
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    32
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    33
struct OTTDThreadStartupMessage {
37a280682fcf (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!)
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    35
	OTTDThreadFunc func; ///< function the thread will execute
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    36
	void *arg;           ///< functions arguments for the thread function
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    37
};
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    38
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    39
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    40
/**
37a280682fcf (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
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    42
 *  utilize serial/ramdebug instead.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    43
 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    44
#ifndef NO_DEBUG_MESSAGES
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    45
void KPutStr(CONST_STRPTR format)
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    46
{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    47
	RawDoFmt(format, NULL, (void (*)())RAWFMTFUNC_SERIAL, NULL);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    48
}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    49
#else
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    50
#define KPutStr(x)
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    51
#endif
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    52
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    53
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    54
/**
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    55
 * MorphOS version for ThreadObject.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    56
 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    57
class ThreadObject_MorphOS : public ThreadObject {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    58
private:
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    59
	APTR m_thr;                  ///< System thread identifier.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    60
	struct MsgPort *m_replyport;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    61
	struct OTTDThreadStartupMessage m_msg;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    62
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    63
public:
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    64
	/**
37a280682fcf (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).
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    66
	 */
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
    67
	ThreadObject_MorphOS(OTTDThreadFunc proc, void *param) : m_thr(0)
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    68
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    69
		struct Task *parent;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    70
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    71
		KPutStr("[OpenTTD] Create thread...\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    72
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    73
		parent = FindTask(NULL);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    74
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    75
		/* Make sure main thread runs with sane priority */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    76
		SetTaskPri(parent, 0);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    77
37a280682fcf (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 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    79
		m_msg.func = proc;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    80
		m_msg.arg  = param;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    81
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    82
		m_replyport = CreateMsgPort();
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    83
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    84
		if (m_replyport != NULL) {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    85
			struct Process *child;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    86
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    87
			m_msg.msg.mn_Node.ln_Type = NT_MESSAGE;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    88
			m_msg.msg.mn_ReplyPort    = m_replyport;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    89
			m_msg.msg.mn_Length       = sizeof(struct OTTDThreadStartupMessage);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    90
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    91
			child = CreateNewProcTags(
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    92
				NP_CodeType,     CODETYPE_PPC,
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    93
				NP_Entry,        ThreadObject_MorphOS::Proxy,
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    94
				NP_StartupMsg,   (IPTR)&m_msg,
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    95
				NP_Priority,     5UL,
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    96
				NP_Name,         (IPTR)"OpenTTD Thread",
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    97
				NP_PPCStackSize, 131072UL,
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    98
				TAG_DONE);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    99
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   100
			m_thr = (APTR) child;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   101
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   102
			if (child != NULL) {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   103
				KPutStr("[OpenTTD] Child process launched.\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   104
			} else {
37a280682fcf (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");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   106
				DeleteMsgPort(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   107
			}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   108
		}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   109
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   110
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   111
	/**
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   112
	 * Create a thread and attach current thread to it.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   113
	 */
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   114
	ThreadObject_MorphOS() : m_thr(0)
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   115
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   116
		m_thr = FindTask(NULL);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   117
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   118
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   119
	/* virtual */ ~ThreadObject_MorphOS()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   120
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   121
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   122
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   123
	/* virtual */ bool IsRunning()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   124
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   125
		return m_thr != 0;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   126
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   127
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   128
	/* virtual */ bool WaitForStop()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   129
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   130
		/* You can't wait on yourself */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   131
		assert(!IsCurrent());
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   132
		/* If the thread is not running, waiting is over */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   133
		if (!IsRunning()) return true;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   134
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   135
		WaitPort(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   136
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   137
		GetMsg(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   138
		DeleteMsgPort(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   139
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   140
		return true;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   141
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   142
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   143
	/* virtual */ bool Exit()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   144
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   145
		struct OTTDThreadStartupMessage *msg;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   146
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   147
		/* You can only exit yourself */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   148
		assert(IsCurrent());
37a280682fcf (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 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   150
		if (!IsRunning()) return false;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   151
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   152
		KPutStr("[Child] Aborting...\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   153
37a280682fcf (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) {
37a280682fcf (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 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   156
			throw 0;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   157
		}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   158
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   159
		return true;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   160
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   161
9476
902f9cf6373f (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 9120
diff changeset
   162
	/* virtual */ void Join()
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   163
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   164
		struct OTTDThreadStartupMessage *reply;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   165
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   166
		/* You cannot join yourself */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   167
		assert(!IsCurrent());
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   168
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   169
		KPutStr("[OpenTTD] Join threads...\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   170
		KPutStr("[OpenTTD] Wait for child to quit...\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   171
		WaitPort(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   172
9476
902f9cf6373f (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 9120
diff changeset
   173
		GetMsg(m_replyport);
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   174
		DeleteMsgPort(m_replyport);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   175
		m_thr = 0;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   176
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   177
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   178
	/* virtual */ bool IsCurrent()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   179
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   180
		return FindTask(NULL) == m_thr;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   181
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   182
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   183
	/* virtual */ uint GetId()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   184
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   185
		return (uint)m_thr;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   186
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   187
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   188
private:
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   189
	/**
37a280682fcf (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
37a280682fcf (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.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   192
	 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   193
	static void Proxy(void)
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   194
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   195
		struct Task *child = FindTask(NULL);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   196
		struct OTTDThreadStartupMessage *msg;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   197
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   198
		/* Make sure, we don't block the parent. */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   199
		SetTaskPri(child, -5);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   200
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   201
		KPutStr("[Child] Progressing...\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   202
37a280682fcf (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) {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   204
			try {
9476
902f9cf6373f (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 9120
diff changeset
   205
				msg->func(msg->arg);
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   206
			} catch(...) {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   207
				KPutStr("[Child] Returned to main()\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   208
			}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   209
		}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   210
37a280682fcf (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. */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   212
		KPutStr("[Child] Done.\n");
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   213
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   214
};
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   215
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   216
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   217
{
9479
c8ab793e4595 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 9477
diff changeset
   218
	return new ThreadObject_MorphOS(proc, param);
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   219
}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   220
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   221
/* static */ ThreadObject *ThreadObject::AttachCurrent()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   222
{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   223
	return new ThreadObject_MorphOS();
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   224
}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   225
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   226
/* static */ uint ThreadObject::CurrentId()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   227
{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   228
	return (uint) FindTask(NULL);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   229
}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   230
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   231
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   232
/**
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   233
 * MorphOS version of ThreadSemaphore.
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   234
 */
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   235
class ThreadSemaphore_MorphOS : public ThreadSemaphore {
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   236
private:
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   237
	struct SignalSemaphore m_sem;
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   238
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   239
public:
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   240
	ThreadSemaphore_MorphOS()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   241
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   242
		InitSemaphore(&m_sem);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   243
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   244
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   245
	/* virtual */ ~ThreadSemaphore_MorphOS()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   246
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   247
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   248
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   249
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   250
	/* virtual */ void Set()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   251
	{
9476
902f9cf6373f (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 9120
diff changeset
   252
		/* Check if semaphore count is really important there. */
9120
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   253
		ReleaseSemaphore(&m_sem);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   254
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   255
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   256
	/* virtual */ void Wait()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   257
	{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   258
		ObtainSemaphore(&m_sem);
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   259
	}
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   260
};
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   261
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   262
/* static */ ThreadSemaphore *ThreadSemaphore::New()
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   263
{
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   264
	return new ThreadSemaphore_MorphOS();
37a280682fcf (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   265
}