src/thread_win32.cpp
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 21:51:14 +0300
changeset 11180 982e9f814f97
parent 10866 242436c016b8
permissions -rw-r--r--
scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     1
/* $Id$ */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     2
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     3
/** @file thread_win32.cpp Win32 thread implementation of Threads. */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     4
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     5
#include "stdafx.h"
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     6
#include "thread.h"
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     7
#include "debug.h"
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     8
#include "core/alloc_func.hpp"
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
     9
#include <stdlib.h>
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    10
#include <windows.h>
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    11
#include <process.h>
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    12
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    13
/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    14
 * Win32 thread version for ThreadObject.
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    15
 */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    16
class ThreadObject_Win32 : public ThreadObject {
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    17
private:
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    18
	uint     m_id_thr;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    19
	HANDLE   m_h_thr;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    20
	OTTDThreadFunc m_proc;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    21
	void     *m_param;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    22
	bool     m_attached;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    23
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    24
public:
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    25
	/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    26
	 * Create a win32 thread and start it, calling proc(param).
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    27
	 */
10866
242436c016b8 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 10861
diff changeset
    28
	ThreadObject_Win32(OTTDThreadFunc proc, void *param) :
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    29
		m_id_thr(0),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    30
		m_h_thr(NULL),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    31
		m_proc(proc),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    32
		m_param(param),
10866
242436c016b8 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 10861
diff changeset
    33
		m_attached(false)
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    34
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    35
		m_h_thr = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &m_id_thr);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    36
		if (m_h_thr == NULL) return;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    37
		ResumeThread(m_h_thr);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    38
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    39
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    40
	/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    41
	 * Create a win32 thread and attach current thread to it.
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    42
	 */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    43
	ThreadObject_Win32() :
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    44
		m_id_thr(0),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    45
		m_h_thr(NULL),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    46
		m_proc(NULL),
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    47
		m_param(NULL),
10866
242436c016b8 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 10861
diff changeset
    48
		m_attached(false)
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    49
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    50
		BOOL ret = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &m_h_thr, 0, FALSE, DUPLICATE_SAME_ACCESS);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    51
		if (!ret) return;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    52
		m_id_thr = GetCurrentThreadId();
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    53
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    54
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    55
	/* virtual */ ~ThreadObject_Win32()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    56
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    57
		if (m_h_thr != NULL) {
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    58
			CloseHandle(m_h_thr);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    59
			m_h_thr = NULL;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    60
		}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    61
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    62
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    63
	/* virtual */ bool IsRunning()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    64
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    65
		if (m_h_thr == NULL) return false;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    66
		DWORD exit_code = 0;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    67
		if (!GetExitCodeThread(m_h_thr, &exit_code)) return false;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    68
		return (exit_code == STILL_ACTIVE);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    69
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    70
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    71
	/* virtual */ bool WaitForStop()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    72
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    73
		/* You can't wait on yourself */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    74
		assert(!IsCurrent());
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    75
		/* If the thread is not running, waiting is over */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    76
		if (!IsRunning()) return true;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    77
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    78
		DWORD res = WaitForSingleObject(m_h_thr, INFINITE);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    79
		return res == WAIT_OBJECT_0;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    80
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    81
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    82
	/* virtual */ bool Exit()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    83
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    84
		/* You can only exit yourself */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    85
		assert(IsCurrent());
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    86
		/* If the thread is not running, we are already closed */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    87
		if (!IsRunning()) return false;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    88
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    89
		/* For now we terminate by throwing an error, gives much cleaner cleanup */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    90
		throw 0;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    91
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    92
10860
ae89867701fe (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 10175
diff changeset
    93
	/* virtual */ void Join()
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    94
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    95
		/* You cannot join yourself */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    96
		assert(!IsCurrent());
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    97
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    98
		WaitForSingleObject(m_h_thr, INFINITE);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
    99
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   100
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   101
	/* virtual */ bool IsCurrent()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   102
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   103
		DWORD id_cur = GetCurrentThreadId();
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   104
		return id_cur == m_id_thr;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   105
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   106
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   107
	/* virtual */ uint GetId()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   108
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   109
		return m_id_thr;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   110
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   111
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   112
private:
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   113
	/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   114
	 * On thread creation, this function is called, which calls the real startup
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   115
	 *  function. This to get back into the correct instance again.
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   116
	 */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   117
	static uint CALLBACK stThreadProc(void *thr)
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   118
	{
10860
ae89867701fe (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 10175
diff changeset
   119
		((ThreadObject_Win32 *)thr)->ThreadProc();
ae89867701fe (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 10175
diff changeset
   120
		return 0;
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   121
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   122
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   123
	/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   124
	 * A new thread is created, and this function is called. Call the custom
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   125
	 *  function of the creator of the thread.
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   126
	 */
10860
ae89867701fe (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 10175
diff changeset
   127
	void ThreadProc()
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   128
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   129
		try {
10860
ae89867701fe (svn r13411) -Codechange: remove the return value from the thread procs because it is never used.
rubidium
parents: 10175
diff changeset
   130
			m_proc(m_param);
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   131
		} catch (...) {
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   132
		}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   133
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   134
};
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   135
10866
242436c016b8 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 10861
diff changeset
   136
/* static */ ThreadObject *ThreadObject::New(OTTDThreadFunc proc, void *param)
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   137
{
10866
242436c016b8 (svn r13417) -Fix (r12945, r13413): freeing the ThreadObjects in a manner that hopefully doesn't cause crashes.
rubidium
parents: 10861
diff changeset
   138
	return new ThreadObject_Win32(proc, param);
10175
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   139
}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   140
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   141
/* static */ ThreadObject* ThreadObject::AttachCurrent()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   142
{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   143
	return new ThreadObject_Win32();
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   144
}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   145
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   146
/* static */ uint ThreadObject::CurrentId()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   147
{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   148
	return GetCurrentThreadId();
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   149
}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   150
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   151
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   152
/**
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   153
 * Win32 thread version of ThreadSemaphore.
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   154
 */
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   155
class ThreadSemaphore_Win32 : public ThreadSemaphore {
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   156
private:
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   157
	HANDLE m_handle;
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   158
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   159
public:
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   160
	ThreadSemaphore_Win32()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   161
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   162
		m_handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   163
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   164
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   165
	/* virtual */ ~ThreadSemaphore_Win32()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   166
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   167
		::CloseHandle(m_handle);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   168
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   169
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   170
	/* virtual */ void Set()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   171
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   172
		::SetEvent(m_handle);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   173
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   174
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   175
	/* virtual */ void Wait()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   176
	{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   177
		::WaitForSingleObject(m_handle, INFINITE);
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   178
	}
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   179
};
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   180
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   181
/* static */ ThreadSemaphore *ThreadSemaphore::New()
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   182
{
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   183
	return new ThreadSemaphore_Win32();
1119f6640ee6 (svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium
parents:
diff changeset
   184
}