src/thread_morphos.cpp
author rubidium
Sun, 18 May 2008 12:17:52 +0000
changeset 10614 04d1add9e12d
parent 10438 b000673d2e44
child 10860 ae89867701fe
permissions -rw-r--r--
(svn r13158) -Codechange: make a class of the GameOptionsWindow.
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
	void *ret;           ///< return value of the thread function
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
/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    42
 *  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
    43
 *  utilize serial/ramdebug instead.
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    44
 */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    45
#ifndef NO_DEBUG_MESSAGES
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    46
void KPutStr(CONST_STRPTR format)
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    47
{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    48
	RawDoFmt(format, NULL, (void (*)())RAWFMTFUNC_SERIAL, NULL);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    49
}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    50
#else
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    51
#define KPutStr(x)
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    52
#endif
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
/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    56
 * MorphOS version for ThreadObject.
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    57
 */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    58
class ThreadObject_MorphOS : public ThreadObject {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    59
private:
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    60
	APTR m_thr;                  ///< System thread identifier.
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    61
	struct MsgPort *m_replyport;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    62
	struct OTTDThreadStartupMessage m_msg;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    63
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    64
public:
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    65
	/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    66
	 * 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
    67
	 */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    68
	ThreadObject_MorphOS(OTTDThreadFunc proc, void *param) : m_thr(0)
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    69
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    70
		struct Task *parent;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    71
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    72
		KPutStr("[OpenTTD] Create thread...\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    73
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    74
		parent = FindTask(NULL);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    75
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    76
		/* Make sure main thread runs with sane priority */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    77
		SetTaskPri(parent, 0);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    78
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    79
		/* 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
    80
		m_msg.func = proc;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    81
		m_msg.arg  = param;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    82
		m_msg.ret  = NULL;
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
		m_replyport = CreateMsgPort();
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    85
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    86
		if (m_replyport != NULL) {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    87
			struct Process *child;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    88
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    89
			m_msg.msg.mn_Node.ln_Type = NT_MESSAGE;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    90
			m_msg.msg.mn_ReplyPort    = m_replyport;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    91
			m_msg.msg.mn_Length       = sizeof(struct OTTDThreadStartupMessage);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    92
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    93
			child = CreateNewProcTags(
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    94
				NP_CodeType,     CODETYPE_PPC,
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    95
				NP_Entry,        ThreadObject_MorphOS::Proxy,
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    96
				NP_StartupMsg,   (IPTR)&m_msg,
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    97
				NP_Priority,     5UL,
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    98
				NP_Name,         (IPTR)"OpenTTD Thread",
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
    99
				NP_PPCStackSize, 131072UL,
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   100
				TAG_DONE);
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
			m_thr = (APTR) child;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   103
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   104
			if (child != NULL) {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   105
				KPutStr("[OpenTTD] Child process launched.\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   106
			} else {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   107
				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
   108
				DeleteMsgPort(m_replyport);
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
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   113
	/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   114
	 * Create a thread and attach current thread to it.
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
	ThreadObject_MorphOS() : m_thr(0)
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
		m_thr = FindTask(NULL);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   119
	}
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
	/* virtual */ ~ThreadObject_MorphOS()
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
	}
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
	/* virtual */ bool IsRunning()
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
		return m_thr != 0;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   128
	}
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
	/* virtual */ bool WaitForStop()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   131
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   132
		/* You can't wait on yourself */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   133
		assert(!IsCurrent());
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   134
		/* If the thread is not running, waiting is over */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   135
		if (!IsRunning()) return true;
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
		WaitPort(m_replyport);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   138
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   139
		GetMsg(m_replyport);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   140
		DeleteMsgPort(m_replyport);
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
		return true;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   143
	}
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
	/* virtual */ bool Exit()
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
		struct OTTDThreadStartupMessage *msg;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   148
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   149
		/* You can only exit yourself */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   150
		assert(IsCurrent());
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   151
		/* 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
   152
		if (!IsRunning()) return false;
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
		KPutStr("[Child] Aborting...\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   155
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   156
		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
   157
			/* 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
   158
			throw 0;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   159
		}
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
		return true;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   162
	}
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
	/* virtual */ void *Join()
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
		struct OTTDThreadStartupMessage *reply;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   167
		void *ret;
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
		/* You cannot join yourself */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   170
		assert(!IsCurrent());
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   171
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   172
		KPutStr("[OpenTTD] Join threads...\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   173
		KPutStr("[OpenTTD] Wait for child to quit...\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   174
		WaitPort(m_replyport);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   175
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   176
		reply = (struct OTTDThreadStartupMessage *)GetMsg(m_replyport);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   177
		ret   = reply->ret;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   178
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   179
		DeleteMsgPort(m_replyport);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   180
		m_thr = 0;
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
		return ret;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   183
	}
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
	/* virtual */ bool IsCurrent()
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
		return FindTask(NULL) == m_thr;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   188
	}
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
	/* virtual */ uint GetId()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   191
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   192
		return (uint)m_thr;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   193
	}
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
private:
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   196
	/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   197
	 * 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
   198
	 *  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
   199
	 */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   200
	static void Proxy(void)
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   201
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   202
		struct Task *child = FindTask(NULL);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   203
		struct OTTDThreadStartupMessage *msg;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   204
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   205
		/* Make sure, we don't block the parent. */
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   206
		SetTaskPri(child, -5);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   207
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   208
		KPutStr("[Child] Progressing...\n");
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
		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
   211
			try {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   212
				msg->ret = msg->func(msg->arg);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   213
			} catch(...) {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   214
				KPutStr("[Child] Returned to main()\n");
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   215
			}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   216
		}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   217
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   218
		/*  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
   219
		KPutStr("[Child] Done.\n");
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
};
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
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
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
	return new ThreadObject_MorphOS(proc, param);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   226
}
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
/* static */ ThreadObject *ThreadObject::AttachCurrent()
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
	return new ThreadObject_MorphOS();
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
/* static */ uint ThreadObject::CurrentId()
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
	return (uint) FindTask(NULL);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   236
}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   237
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
/**
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   240
 * MorphOS version of ThreadSemaphore.
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
class ThreadSemaphore_MorphOS : public ThreadSemaphore {
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   243
private:
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   244
	struct SignalSemaphore m_sem;
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   245
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   246
public:
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   247
	ThreadSemaphore_MorphOS()
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
		InitSemaphore(&m_sem);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   250
	}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   251
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   252
	/* virtual */ ~ThreadSemaphore_MorphOS()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   253
	{
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
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   257
	/* virtual */ void Set()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   258
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   259
		// Check if semaphore count is really important there.
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   260
		ReleaseSemaphore(&m_sem);
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
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   263
	/* virtual */ void Wait()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   264
	{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   265
		ObtainSemaphore(&m_sem);
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   266
	}
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   267
};
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   268
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   269
/* static */ ThreadSemaphore *ThreadSemaphore::New()
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   270
{
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   271
	return new ThreadSemaphore_MorphOS();
b000673d2e44 (svn r12980) -Fix: MorphOS threading support. Patch by Fabien Coeurjoly.
rubidium
parents:
diff changeset
   272
}