Mercurial > hg > openttd
annotate src/fiber_win32.cpp @ 10480:649ba332458f draft
(svn r14735) -Codechange: remove a bit of bit-waste in the map array (without changing the map array) and make the CompanyIDs contiguous.
-Note: 15 should be enough for now... making it any more means adding more bytes to the map array and thus wasting more bits instead of reducing the bit waste.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Wed, 24 Dec 2008 09:53:15 +0000 (2008-12-24) |
parents | a56c5ca0f396 |
children |
rev | line source |
---|---|
8934
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
1 /* $Id$ */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
2 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
3 /** @file fiber_win32.cpp Win32 implementation of Fiber. */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
4 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
5 #include "stdafx.h" |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
6 #include "fiber.hpp" |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
8 #include <windows.h> |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
9 #include <process.h> |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
10 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
11 class Fiber_Win32 : public Fiber { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
12 private: |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
13 LPVOID m_fiber; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
14 FiberFunc m_proc; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
15 void *m_param; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
16 bool m_attached; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
17 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
18 static Fiber_Win32 *s_main; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
19 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
20 public: |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
21 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
22 * Create a win32 fiber and start it, calling proc(param). |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
23 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
24 Fiber_Win32(FiberFunc proc, void *param) : |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
25 m_fiber(NULL), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
26 m_proc(proc), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
27 m_param(param), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
28 m_attached(false) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
29 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
30 CreateFiber(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
31 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
32 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
33 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
34 * Create a win32 fiber and attach current thread to it. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
35 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
36 Fiber_Win32(void *param) : |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
37 m_fiber(NULL), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
38 m_proc(NULL), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
39 m_param(param), |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
40 m_attached(true) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
41 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
42 ConvertThreadToFiber(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
43 if (s_main == NULL) s_main = this; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
44 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
45 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
46 /* virtual */ ~Fiber_Win32() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
47 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
48 if (this->m_fiber != NULL) { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
49 if (this->m_attached) { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
50 this->ConvertFiberToThread(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
51 } else { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
52 this->DeleteFiber(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
53 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
54 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
55 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
56 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
57 /* virtual */ void SwitchToFiber() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
58 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
59 typedef VOID (WINAPI *FnSwitchToFiber)(LPVOID fiber); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
60 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
61 static FnSwitchToFiber fnSwitchToFiber = (FnSwitchToFiber)stGetProcAddr("SwitchToFiber"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
62 assert(fnSwitchToFiber != NULL); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
63 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
64 fnSwitchToFiber(this->m_fiber); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
65 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
66 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
67 /* virtual */ void Exit() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
68 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
69 /* Simply switch back to the main fiber, we kill the fiber sooner or later */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
70 s_main->SwitchToFiber(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
71 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
72 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
73 /* virtual */ bool IsRunning() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
74 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
75 return this->m_fiber != NULL; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
76 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
77 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
78 /* virtual */ void *GetFiberData() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
79 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
80 return this->m_param; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
81 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
82 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
83 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
84 * Win95 doesn't have Fiber support. So check if we have Fiber support, |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
85 * and else fall back on Fiber_Thread. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
86 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
87 static bool IsSupported() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
88 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
89 static bool first_run = true; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
90 static bool is_supported = false; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
91 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
92 if (first_run) { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
93 first_run = false; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
94 static const char *names[] = { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
95 "ConvertThreadToFiber", |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
96 "CreateFiber", |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
97 "DeleteFiber", |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
98 "ConvertFiberToThread", |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
99 "SwitchToFiber"}; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
100 for (size_t i = 0; i < lengthof(names); i++) { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
101 if (stGetProcAddr(names[i]) == NULL) return false; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
102 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
103 is_supported = true; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
104 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
105 return is_supported; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
106 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
107 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
108 private: |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
109 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
110 * Get a function from kernel32.dll. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
111 * @param name Function to get. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
112 * @return Proc to the function, or NULL when not found. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
113 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
114 static FARPROC stGetProcAddr(const char *name) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
115 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
116 static HMODULE hKernel = LoadLibraryA("kernel32.dll"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
117 return GetProcAddress(hKernel, name); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
118 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
119 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
120 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
121 * First function which is called within the fiber. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
122 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
123 static VOID CALLBACK stFiberProc(LPVOID fiber) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
124 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
125 Fiber_Win32 *cur = (Fiber_Win32 *)fiber; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
126 cur->m_proc(cur->m_param); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
127 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
128 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
129 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
130 * Delete a fiber. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
131 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
132 void DeleteFiber() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
133 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
134 typedef VOID (WINAPI *FnDeleteFiber)(LPVOID lpFiber); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
135 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
136 static FnDeleteFiber fnDeleteFiber = (FnDeleteFiber)stGetProcAddr("DeleteFiber"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
137 assert(fnDeleteFiber != NULL); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
138 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
139 fnDeleteFiber(this->m_fiber); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
140 this->m_fiber = NULL; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
141 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
142 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
143 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
144 * Convert a current thread to a fiber. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
145 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
146 void ConvertThreadToFiber() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
147 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
148 typedef LPVOID (WINAPI *FnConvertThreadToFiber)(LPVOID lpParameter); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
149 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
150 static FnConvertThreadToFiber fnConvertThreadToFiber = (FnConvertThreadToFiber)stGetProcAddr("ConvertThreadToFiber"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
151 assert(fnConvertThreadToFiber != NULL); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
152 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
153 this->m_fiber = fnConvertThreadToFiber(this); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
154 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
155 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
156 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
157 * Create a new fiber. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
158 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
159 void CreateFiber() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
160 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
161 typedef LPVOID (WINAPI *FnCreateFiber)(SIZE_T dwStackSize, LPFIBER_START_ROUTINE lpStartAddress, LPVOID lpParameter); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
162 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
163 static FnCreateFiber fnCreateFiber = (FnCreateFiber)stGetProcAddr("CreateFiber"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
164 assert(fnCreateFiber != NULL); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
165 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
166 this->m_fiber = fnCreateFiber(0, &stFiberProc, this); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
167 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
168 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
169 /** |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
170 * Convert a fiber back to a thread. |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
171 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
172 void ConvertFiberToThread() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
173 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
174 typedef BOOL (WINAPI *FnConvertFiberToThread)(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
175 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
176 static FnConvertFiberToThread fnConvertFiberToThread = (FnConvertFiberToThread)stGetProcAddr("ConvertFiberToThread"); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
177 assert(fnConvertFiberToThread != NULL); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
178 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
179 fnConvertFiberToThread(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
180 this->m_fiber = NULL; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
181 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
182 }; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
183 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
184 /* Initialize the static member of Fiber_Win32 */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
185 /* static */ Fiber_Win32 *Fiber_Win32::s_main = NULL; |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
186 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
187 /* Include Fiber_Thread, as Win95 needs it */ |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
188 #include "fiber_thread.cpp" |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
189 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
190 /* static */ Fiber *Fiber::New(FiberFunc proc, void *param) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
191 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
192 if (Fiber_Win32::IsSupported()) return new Fiber_Win32(proc, param); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
193 return new Fiber_Thread(proc, param); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
194 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
195 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
196 /* static */ Fiber *Fiber::AttachCurrent(void *param) |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
197 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
198 if (Fiber_Win32::IsSupported()) return new Fiber_Win32(param); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
199 return new Fiber_Thread(param); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
200 } |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
201 |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
202 /* static */ void *Fiber::GetCurrentFiberData() |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
203 { |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
204 if (Fiber_Win32::IsSupported()) return ((Fiber *)::GetFiberData())->GetFiberData(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
205 return Fiber_Thread::GetCurrentFiber()->GetFiberData(); |
a56c5ca0f396
(svn r12706) -Merge: the thread rewrite from NoAI. The rewrite makes the threading we have better extendable.
rubidium <rubidium@openttd.org>
parents:
diff
changeset
|
206 } |