author | truebrain |
Fri, 18 Jul 2008 10:15:16 +0000 | |
branch | noai |
changeset 11168 | 3842648184cd |
parent 11095 | a0da321a39b3 |
permissions | -rw-r--r-- |
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 | 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 |
|
11095
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
10 |
class OTTDThreadExitSignal { }; |
a0da321a39b3
(svn r13652) [NoAI] -Fix: throw thread-exit-signal with a custom class, not with the value '0', which might be captured by a lot of other layers
truebrain
parents:
10867
diff
changeset
|
11 |
|
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 |
* 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
|
14 |
*/ |
9856
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
15 |
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
|
16 |
public: |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
17 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
18 |
* 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
|
19 |
*/ |
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
|
20 |
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
|
21 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
22 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
23 |
* 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
|
24 |
* @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
|
25 |
*/ |
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
|
26 |
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
|
27 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
28 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
29 |
* 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
|
30 |
* @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
|
31 |
*/ |
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
|
32 |
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
|
33 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
34 |
/** |
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
|
35 |
* 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
|
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 |
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
|
38 |
|
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 |
/** |
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
|
40 |
* 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
|
41 |
*/ |
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
42 |
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
|
43 |
|
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
|
44 |
/** |
9856
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
45 |
* 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
|
46 |
* @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
|
47 |
*/ |
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
|
48 |
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
|
49 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
50 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
51 |
* 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
|
52 |
* @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
|
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 |
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
|
55 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
56 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
57 |
* 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
|
58 |
* 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
|
59 |
* @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
|
60 |
* @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
|
61 |
* @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
|
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 |
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
|
64 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
65 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
66 |
* 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
|
67 |
* @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
|
68 |
*/ |
10867
5de2923d6e59
(svn r13418) [NoAI] -Sync: with trunk r13380:13417.
rubidium
parents:
10455
diff
changeset
|
69 |
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
|
70 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
71 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
72 |
* 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
|
73 |
* @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
|
74 |
*/ |
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
|
75 |
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
|
76 |
}; |
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
|
77 |
|
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 |
* 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
|
80 |
*/ |
9856
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
81 |
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
|
82 |
public: |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
83 |
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
|
84 |
|
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 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
|
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 |
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
|
89 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
90 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
91 |
* 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
|
92 |
*/ |
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
|
93 |
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
|
94 |
|
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
95 |
/** |
6a0dcee9f8e4
(svn r12528) [NoAI] -Change: rewrote most of the internal ThreadObject to make it a bit more readable
truebrain
parents:
9694
diff
changeset
|
96 |
* 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
|
97 |
*/ |
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
|
98 |
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
|
99 |
}; |
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
|
100 |
|
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
|
101 |
#endif /* THREAD_H */ |