src/thread.h
author truebrain
Mon, 16 Jun 2008 14:43:19 +0000
branchnoai
changeset 10978 13fd0364b2c6
parent 10867 5de2923d6e59
child 11095 a0da321a39b3
permissions -rw-r--r--
(svn r13532) [NoAI] -Fix: in MultiPlayer SignID wasn't set correctly, causing weird effects. Code is now more unified, which should avoid simular effects in the future (tnx to glx for initial patch)
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
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10172
diff changeset
     3
/** @file thread.h Base of all threads. */
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9514
diff changeset
     4
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     5
#ifndef THREAD_H
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     6
#define THREAD_H
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     7
10867
5de2923d6e59 (svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents: 10455
diff changeset
     8
typedef void (*OTTDThreadFunc)(void *);
2285
3193cbd1ba88 (svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff changeset
     9
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    10
/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    11
 * A Thread Object which works on all our supported OSes.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    12
 */
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    13
class ThreadObject {
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    14
public:
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    15
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    16
	 * Virtual destructor to allow 'delete' operator to work properly.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    17
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    18
	virtual ~ThreadObject() {};
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    19
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    20
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    21
	 * Check if the thread is currently running.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    22
	 * @return True if the thread is running.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    23
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    24
	virtual bool IsRunning() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    25
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    26
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    27
	 * Waits for the thread to exit.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    28
	 * @return True if the thread has exited.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    29
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    30
	virtual bool WaitForStop() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    31
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    32
	/**
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    33
	 * Exit this thread.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    34
	 */
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    35
	virtual bool Exit() = 0;
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    36
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    37
	/**
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    38
	 * Join this thread.
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    39
	 */
10867
5de2923d6e59 (svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents: 10455
diff changeset
    40
	virtual void Join() = 0;
9859
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    41
81621c6ba0e9 (svn r12538) [NoAI] -Codechange: introducing fiber.hpp, a class to have fibers in your application via either Windows Fibers, or via thread.h
truebrain
parents: 9856
diff changeset
    42
	/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    43
	 * Check if this thread is the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    44
	 * @return True if it is the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    45
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    46
	virtual bool IsCurrent() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    47
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    48
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    49
	 * Get the unique ID of this thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    50
	 * @return A value unique to each thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    51
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    52
	virtual uint GetId() = 0;
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    53
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    54
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    55
	 * Create a thread; proc will be called as first function inside the thread,
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    56
	 *  with optinal params.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    57
	 * @param proc The procedure to call inside the thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    58
	 * @param param The params to give with 'proc'.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    59
	 * @return True if the thread was started correctly.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    60
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    61
	static ThreadObject *New(OTTDThreadFunc proc, void *param);
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    62
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    63
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    64
	 * Convert the current thread to a new ThreadObject.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    65
	 * @return A new ThreadObject with the current thread attached to it.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    66
	 */
10867
5de2923d6e59 (svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents: 10455
diff changeset
    67
	static ThreadObject *AttachCurrent();
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    68
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    69
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    70
	 * Find the Id of the current running thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    71
	 * @return The thread ID of the current active thread.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    72
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    73
	static uint CurrentId();
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    74
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    75
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    76
/**
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    77
 * Cross-platform Thread Semaphore. Wait() waits for a Set() of someone else.
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    78
 */
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    79
class ThreadSemaphore {
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    80
public:
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    81
	static ThreadSemaphore *New();
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    82
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    83
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    84
	 * Virtual Destructor to avoid compiler warnings.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    85
	 */
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    86
	virtual ~ThreadSemaphore() {};
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    87
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    88
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    89
	 * Signal all threads that are in Wait() to continue.
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    90
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    91
	virtual void Set() = 0;
9856
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    92
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    93
	/**
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    94
	 * Wait until we are signaled by a call to Set().
6a0dcee9f8e4 (svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents: 9694
diff changeset
    95
	 */
9514
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    96
	virtual void Wait() = 0;
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    97
};
e31710af1ca0 (svn r9419) [NoAI] -Codechange: support AI threads also on Win32 (using threads on Win95 and fibers on other Win32 platforms)
KUDr
parents: 6574
diff changeset
    98
2380
3b26659b4a9a (svn r2906) Fix some threaded saving problems. Now the thread only interfaces with the main program through a sort of mutex. Communication uses the function OTTD_SendThreadMessage() with the approiate message which is handled in ProcessSentMessage() during the main loop.
Darkvater
parents: 2286
diff changeset
    99
#endif /* THREAD_H */