src/thread.cpp
author rubidium
Wed, 09 Jan 2008 18:11:12 +0000
branchnoai
changeset 9723 eee46cb39750
parent 9574 698395509d12
child 9856 6a0dcee9f8e4
permissions -rw-r--r--
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     1
/* $Id$ */
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     2
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9544
diff changeset
     3
/** @file thread.cpp */
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9544
diff changeset
     4
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     5
#include "stdafx.h"
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     6
#include "thread.h"
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
     7
#include "debug.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9574
diff changeset
     8
#include "core/alloc_func.hpp"
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     9
#include <stdlib.h>
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    10
6230
06c91ff0af27 (svn r8678) [PSP] -Add: added LIBS and CFLAGS needed to compile PSP
truelight
parents: 5860
diff changeset
    11
#if defined(__AMIGA__) || defined(__MORPHOS__) || defined(PSP) || defined(NO_THREADS)
4324
9c999cc382fa (svn r5978) -Add: allow a switch in Makefile.config to disable threads in OpenTTD (no matter what system you are on). Only useful for testing.
truelight
parents: 4300
diff changeset
    12
OTTDThread *OTTDCreateThread(OTTDThreadFunc function, void *arg) { return NULL; }
9c999cc382fa (svn r5978) -Add: allow a switch in Makefile.config to disable threads in OpenTTD (no matter what system you are on). Only useful for testing.
truelight
parents: 4300
diff changeset
    13
void *OTTDJoinThread(OTTDThread *t) { return NULL; }
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6230
diff changeset
    14
void OTTDExitThread() { NOT_REACHED(); };
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    15
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    16
#elif defined(__OS2__)
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    17
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    18
#define INCL_DOS
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    19
#include <os2.h>
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    20
#include <process.h>
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    21
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    22
struct OTTDThread {
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    23
	TID thread;
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    24
	OTTDThreadFunc func;
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    25
	void* arg;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    26
	void* ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    27
};
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    28
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    29
static void Proxy(void* arg)
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    30
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    31
	OTTDThread* t = (OTTDThread*)arg;
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    32
	t->ret = t->func(t->arg);
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    33
}
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    34
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    35
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    36
{
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    37
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    38
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    39
	if (t == NULL) return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    40
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    41
	t->func = function;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    42
	t->arg  = arg;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    43
	t->thread = _beginthread(Proxy, NULL, 32768, t);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    44
	if (t->thread != (TID)-1) {
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    45
		return t;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    46
	} else {
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    47
		free(t);
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    48
		return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    49
	}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    50
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    51
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    52
void* OTTDJoinThread(OTTDThread* t)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    53
{
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    54
	void* ret;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    55
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    56
	if (t == NULL) return NULL;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    57
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    58
	DosWaitThread(&t->thread, DCWW_WAIT);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    59
	ret = t->ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    60
	free(t);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    61
	return ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    62
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    63
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6230
diff changeset
    64
void OTTDExitThread()
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    65
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    66
	_endthread();
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
    67
}
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    68
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    69
#elif defined(UNIX)
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    70
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    71
#include <pthread.h>
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
    72
#include <semaphore.h>
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
    73
#include <unistd.h>
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    74
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    75
struct OTTDThread {
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    76
	pthread_t thread;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    77
};
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    78
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    79
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    80
{
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    81
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    82
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    83
	if (t == NULL) return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    84
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    85
	if (pthread_create(&t->thread, NULL, function, arg) == 0) {
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    86
		return t;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    87
	} else {
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    88
		free(t);
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    89
		return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    90
	}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    91
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    92
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
    93
void* OTTDJoinThread(OTTDThread* t)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    94
{
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    95
	void* ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
    96
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    97
	if (t == NULL) return NULL;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    98
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
    99
	pthread_join(t->thread, &ret);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   100
	free(t);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   101
	return ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   102
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   103
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6230
diff changeset
   104
void OTTDExitThread()
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   105
{
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   106
	pthread_exit(NULL);
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   107
}
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   108
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   109
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   110
/**
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   111
 * C++ thread wrapper - posix pthread version
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   112
 */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   113
struct ThreadObject_pthread : public ThreadObject {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   114
	pthread_t m_thr;             ///< system thread identifier
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   115
	void (CDECL *m_proc)(void*); ///< external thread procedure
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   116
	void      *m_param;          ///< parameter for the external thread procedure
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   117
	bool      m_attached;        ///< true if the ThreadObject was attached to an existing thread
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   118
	sem_t     m_sem_start;       ///< here the new thread waits before it starts
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   119
	sem_t     m_sem_stop;        ///< here the other thread can wait for this thread to end
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   120
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   121
	ThreadObject_pthread()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   122
		: m_thr(0)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   123
		, m_proc(NULL)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   124
		, m_param(NULL)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   125
		, m_attached(false)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   126
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   127
		sem_init(&m_sem_start, 0, 0);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   128
		sem_init(&m_sem_stop, 0, 0);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   129
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   130
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   131
	/** Virtual destructor to allow 'delete' operator to work properly. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   132
	/*virtual*/ ~ThreadObject_pthread()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   133
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   134
		sem_destroy(&m_sem_stop);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   135
		sem_destroy(&m_sem_start);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   136
	};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   137
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   138
	/** Returns true if thread is running. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   139
	/*virtual*/ bool IsRunning()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   140
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   141
		return m_thr != 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   142
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   143
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   144
	/** Waits for the thread exit. Returns true if thread exited. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   145
	/*virtual*/ bool WaitForStop()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   146
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   147
		assert(!IsCurrent());
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   148
		int ret = sem_wait(&m_sem_stop);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   149
		if (ret == 0) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   150
			/* we have passed semaphore so increment it again */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   151
			sem_post(&m_sem_stop);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   152
			return true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   153
		}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   154
		return false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   155
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   156
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   157
	/** Returns true if this Thread Object belongs to the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   158
	/*virtual*/ bool IsCurrent()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   159
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   160
		return pthread_self() == m_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   161
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   162
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   163
	/** Returns thread id of the thread associated to this Thread Object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   164
	/*virtual*/ uint Id()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   165
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   166
		return (uint)m_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   167
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   168
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   169
	/** Begin thread execution by calling given procedure and passing given parameter to it. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   170
	/*virtual*/ bool Begin(void (CDECL *proc)(void*), void *param)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   171
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   172
		m_proc = proc;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   173
		m_param = param;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   174
		pthread_create(&m_thr, NULL, &stThreadProc, this);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   175
		sem_post(&m_sem_start);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   176
		return true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   177
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   178
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   179
	/** Thread procedure that is called by new thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   180
	static void* stThreadProc(void *thr)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   181
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   182
		return ((ThreadObject_pthread*)thr)->ThreadProc();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   183
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   184
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   185
	/** Thread method that is called by new thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   186
	void* ThreadProc()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   187
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   188
		/* the new thread stops here so the calling thread can complete pthread_create() call */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   189
		sem_wait(&m_sem_start);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   190
		try {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   191
			m_proc(m_param);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   192
		} catch (...) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   193
			DEBUG(misc, 0, "Exception in thread %u!", Id());
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   194
		}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   195
		m_thr = 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   196
		/* notify threads waiting for our completion */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   197
		sem_post(&m_sem_stop);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   198
		return NULL;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   199
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   200
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   201
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   202
/** Used to construct new instance of thread object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   203
/*static*/ ThreadObject* ThreadObject::New()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   204
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   205
	ThreadObject_pthread *thr = new ThreadObject_pthread();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   206
	return thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   207
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   208
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   209
/** Returns thread object instance that belongs to the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   210
/*static*/ ThreadObject* ThreadObject::Current()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   211
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   212
	ThreadObject_pthread *thr = new ThreadObject_pthread();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   213
	thr->m_thr = pthread_self();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   214
	thr->m_attached = true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   215
	return thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   216
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   217
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   218
/** Returns thread id of the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   219
/*static*/ uint ThreadObject::CurrentId()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   220
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   221
	return (uint)pthread_self();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   222
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   223
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   224
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   225
/**
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   226
 * Event object that is signaled manually by Set() and reset automatically when thread passes Wait().
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   227
 */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   228
struct AutoResetEvent_pthread : public AutoResetEvent {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   229
	sem_t m_sem;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   230
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   231
	AutoResetEvent_pthread()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   232
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   233
		sem_init(&m_sem, 0, 0);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   234
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   235
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   236
	/** Virtual destructor to allow 'delete' operator to work properly. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   237
	/*virtual*/ ~AutoResetEvent_pthread()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   238
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   239
		sem_destroy(&m_sem);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   240
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   241
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   242
	/** Set the event state to signaled so that one thread can pass the Wait() method. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   243
	/*virtual*/ void Set()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   244
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   245
		int val = 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   246
		if (sem_getvalue(&m_sem, &val) == 0 && val == 0) sem_post(&m_sem);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   247
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   248
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   249
	/** Wait until event is signaled by calling Set() */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   250
	/*virtual*/ void Wait()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   251
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   252
		sem_wait(&m_sem);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   253
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   254
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   255
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   256
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   257
/** Used to construct new instance of event object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   258
/*static*/ AutoResetEvent* AutoResetEvent::New()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   259
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   260
	AutoResetEvent_pthread *evt = new AutoResetEvent_pthread();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   261
	return evt;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   262
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   263
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   264
#elif defined(WIN32)
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   265
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   266
#include <windows.h>
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   267
#include <process.h>
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   268
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   269
struct OTTDThread {
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   270
	HANDLE thread;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   271
	uint   thread_id;
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   272
	OTTDThreadFunc func;
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   273
	void* arg;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   274
	void* ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   275
};
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   276
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   277
static unsigned WINAPI Proxy(LPVOID arg)
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   278
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   279
	OTTDThread* t = (OTTDThread*)arg;
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   280
	t->ret = t->func(t->arg);
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   281
	return 0;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   282
}
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   283
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   284
OTTDThread* OTTDCreateThread(OTTDThreadFunc function, void* arg)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   285
{
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
   286
	OTTDThread* t = MallocT<OTTDThread>(1);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   287
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   288
	if (t == NULL) return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   289
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   290
	t->func = function;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   291
	t->arg  = arg;
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   292
	t->thread = (HANDLE)_beginthreadex(NULL, 0, Proxy, t, 0, &t->thread_id);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   293
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   294
	if (t->thread != NULL) {
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   295
		return t;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   296
	} else {
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   297
		free(t);
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   298
		return NULL;
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   299
	}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   300
}
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   301
4298
3417f80deca1 (svn r5943) -Merge TGP (part r5725): -Codechange: renamed Thread to OTTDThread, as Windows
truelight
parents: 2290
diff changeset
   302
void* OTTDJoinThread(OTTDThread* t)
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   303
{
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   304
	void* ret;
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   305
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   306
	if (t == NULL) return NULL;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   307
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   308
	WaitForSingleObject(t->thread, INFINITE);
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   309
	CloseHandle(t->thread);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   310
	ret = t->ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   311
	free(t);
2286
acb76b379e72 (svn r2810) Threads may now return information when they terminate using a void*.
tron
parents: 2285
diff changeset
   312
	return ret;
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   313
}
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   314
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6230
diff changeset
   315
void OTTDExitThread()
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   316
{
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   317
	_endthreadex(0);
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4298
diff changeset
   318
}
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   319
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   320
/**
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   321
 * C++ thread wrapper interface
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   322
 */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   323
struct ThreadObject_Win32 : public ThreadObject {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   324
	uint     m_id_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   325
	HANDLE   m_h_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   326
	void (CDECL *m_proc)(void*);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   327
	void     *m_param;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   328
	bool     m_attached;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   329
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   330
	ThreadObject_Win32()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   331
		: m_id_thr(0)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   332
		, m_h_thr(NULL)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   333
		, m_proc(NULL)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   334
		, m_param(NULL)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   335
		, m_attached(false)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   336
	{}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   337
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   338
	/** Virtual destructor to allow 'delete' operator to work properly. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   339
	/*virtual*/ ~ThreadObject_Win32()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   340
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   341
		if (m_h_thr != NULL) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   342
			CloseHandle(m_h_thr);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   343
			m_h_thr = NULL;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   344
		}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   345
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   346
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   347
	/** Returns true if thread is running. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   348
	/*virtual*/ bool IsRunning()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   349
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   350
		if (m_h_thr == NULL) return false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   351
		DWORD exit_code = 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   352
		if (!GetExitCodeThread(m_h_thr, &exit_code)) return false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   353
		return (exit_code == STILL_ACTIVE);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   354
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   355
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   356
	/** Waits for the thread exit. Returns true if thread exited. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   357
	virtual bool WaitForStop()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   358
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   359
		assert(!IsCurrent());
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   360
		if (!IsRunning()) return true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   361
		DWORD res = WaitForSingleObject(m_h_thr, INFINITE);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   362
		return res == WAIT_OBJECT_0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   363
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   364
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   365
	/** Returns true if this Thread Object belongs to the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   366
	/*virtual*/ bool IsCurrent()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   367
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   368
		DWORD id_cur = GetCurrentThreadId();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   369
		return id_cur == m_id_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   370
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   371
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   372
	/** Returns thread id of the thread associated to this Thread Object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   373
	/*virtual*/ uint Id()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   374
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   375
		return m_id_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   376
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   377
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   378
	/** Begin thread execution by calling given procedure and passing given parameter to it. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   379
	/*virtual*/ bool Begin(void (CDECL *proc)(void*), void *param)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   380
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   381
		assert(m_attached || !IsRunning());
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   382
		m_attached = false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   383
		m_proc = proc;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   384
		m_param = param;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   385
		m_h_thr = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &m_id_thr);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   386
		if (m_h_thr == NULL) return false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   387
		ResumeThread(m_h_thr);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   388
		return true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   389
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   390
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   391
	/** Thread procedure that is called by new thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   392
	static uint CALLBACK stThreadProc(void *thr)
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   393
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   394
		return ((ThreadObject_Win32*)thr)->ThreadProc();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   395
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   396
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   397
	/** Thread method that is called by new thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   398
	uint ThreadProc()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   399
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   400
		uint id_thr = m_id_thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   401
		try {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   402
			m_proc(m_param);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   403
		} catch (...) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   404
			DEBUG(misc, 0, "Exception in thread %d!", id_thr);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   405
		}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   406
		return 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   407
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   408
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   409
	/** Associates the current (caller) thread with this Thread Object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   410
	bool AttachCurrent()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   411
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   412
		assert(m_h_thr == NULL);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   413
		BOOL ret = DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &m_h_thr, 0, FALSE, DUPLICATE_SAME_ACCESS);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   414
		if (!ret) return false;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   415
		m_id_thr = GetCurrentThreadId();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   416
		m_attached = true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   417
		return true;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   418
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   419
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   420
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   421
/** Used to construct new instance of thread object. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   422
/*static*/ ThreadObject* ThreadObject::New()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   423
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   424
	ThreadObject_Win32 *thr = new ThreadObject_Win32;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   425
	return thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   426
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   427
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   428
/** Returns thread object instance that belongs to the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   429
/*static*/ ThreadObject* ThreadObject::Current()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   430
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   431
	ThreadObject_Win32 *thr = new ThreadObject_Win32;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   432
	if (!thr->AttachCurrent()) {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   433
		/* attach failed */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   434
		delete thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   435
		return NULL;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   436
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   437
	return thr;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   438
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   439
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   440
/** Returns thread id of the current (calling) thread. */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   441
/*static*/ uint ThreadObject::CurrentId()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   442
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   443
	return GetCurrentThreadId();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   444
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   445
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   446
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   447
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   448
/**
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   449
 * Event object that is signaled manually by Set() and reset automatically when thread passes Wait().
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   450
 */
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   451
struct AutoResetEvent_Win32 : public AutoResetEvent {
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   452
	HANDLE m_handle;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   453
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   454
	AutoResetEvent_Win32()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   455
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   456
		m_handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   457
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   458
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   459
	virtual ~AutoResetEvent_Win32()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   460
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   461
		::CloseHandle(m_handle);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   462
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   463
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   464
	virtual void Set()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   465
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   466
		::SetEvent(m_handle);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   467
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   468
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   469
	virtual void Wait()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   470
	{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   471
		::WaitForSingleObject(m_handle, INFINITE);
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   472
	}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   473
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   474
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   475
/*static*/ AutoResetEvent* AutoResetEvent::New()
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   476
{
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   477
	return new AutoResetEvent_Win32();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   478
}
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6573
diff changeset
   479
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
   480
#endif