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