|
danderson@0
|
1 |
// vim: set sts=8 ts=2 sw=2 tw=99 et: |
|
danderson@0
|
2 |
// |
|
danderson@0
|
3 |
// Copyright (C) 2013, David Anderson and AlliedModders LLC |
|
danderson@0
|
4 |
// All rights reserved. |
|
danderson@0
|
5 |
// |
|
danderson@0
|
6 |
// Redistribution and use in source and binary forms, with or without |
|
danderson@0
|
7 |
// modification, are permitted provided that the following conditions are met: |
|
danderson@0
|
8 |
// |
|
danderson@0
|
9 |
// * Redistributions of source code must retain the above copyright notice, this |
|
danderson@0
|
10 |
// list of conditions and the following disclaimer. |
|
danderson@0
|
11 |
// * Redistributions in binary form must reproduce the above copyright notice, |
|
danderson@0
|
12 |
// this list of conditions and the following disclaimer in the documentation |
|
danderson@0
|
13 |
// and/or other materials provided with the distribution. |
|
danderson@0
|
14 |
// * Neither the name of AlliedModders LLC nor the names of its contributors |
|
danderson@0
|
15 |
// may be used to endorse or promote products derived from this software |
|
danderson@0
|
16 |
// without specific prior written permission. |
|
danderson@0
|
17 |
// |
|
danderson@0
|
18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
danderson@0
|
19 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
danderson@0
|
20 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
danderson@0
|
21 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
danderson@0
|
22 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
danderson@0
|
23 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
danderson@0
|
24 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
danderson@0
|
25 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
danderson@0
|
26 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
danderson@0
|
27 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
danderson@0
|
28 |
// POSSIBILITY OF SUCH DAMAGE. |
|
danderson@0
|
29 |
|
|
danderson@0
|
30 |
#ifndef _include_amtl_thread_windows_h_ |
|
danderson@0
|
31 |
#define _include_amtl_thread_windows_h_ |
|
danderson@0
|
32 |
|
|
danderson@0
|
33 |
#include <windows.h> |
|
danderson@0
|
34 |
|
|
danderson@0
|
35 |
namespace ke { |
|
danderson@0
|
36 |
|
|
danderson@0
|
37 |
class CriticalSection : public Lockable |
|
danderson@0
|
38 |
{ |
|
danderson@0
|
39 |
public: |
|
danderson@0
|
40 |
CriticalSection() { |
|
danderson@0
|
41 |
InitializeCriticalSection(&cs_); |
|
danderson@0
|
42 |
} |
|
danderson@0
|
43 |
~CriticalSection() { |
|
danderson@0
|
44 |
DeleteCriticalSection(&cs_); |
|
danderson@0
|
45 |
} |
|
danderson@0
|
46 |
|
|
danderson@0
|
47 |
bool DoTryLock() KE_OVERRIDE { |
|
danderson@0
|
48 |
return !!TryEnterCriticalSection(&cs_); |
|
danderson@0
|
49 |
} |
|
danderson@0
|
50 |
void DoLock() KE_OVERRIDE { |
|
danderson@0
|
51 |
EnterCriticalSection(&cs_); |
|
danderson@0
|
52 |
} |
|
danderson@0
|
53 |
|
|
danderson@0
|
54 |
void DoUnlock() KE_OVERRIDE { |
|
danderson@0
|
55 |
LeaveCriticalSection(&cs_); |
|
danderson@0
|
56 |
} |
|
danderson@0
|
57 |
|
|
danderson@0
|
58 |
private: |
|
danderson@0
|
59 |
CRITICAL_SECTION cs_; |
|
danderson@0
|
60 |
}; |
|
danderson@0
|
61 |
|
|
danderson@0
|
62 |
typedef CriticalSection Mutex; |
|
danderson@0
|
63 |
|
|
danderson@0
|
64 |
// Currently, this class only supports single-listener CVs. |
|
danderson@0
|
65 |
class ConditionVariable : public Lockable |
|
danderson@0
|
66 |
{ |
|
danderson@0
|
67 |
public: |
|
danderson@0
|
68 |
ConditionVariable() { |
|
danderson@0
|
69 |
event_ = CreateEvent(NULL, FALSE, FALSE, NULL); |
|
danderson@0
|
70 |
} |
|
danderson@0
|
71 |
~ConditionVariable() { |
|
danderson@0
|
72 |
CloseHandle(event_); |
|
danderson@0
|
73 |
} |
|
danderson@0
|
74 |
|
|
danderson@0
|
75 |
bool DoTryLock() KE_OVERRIDE { |
|
danderson@0
|
76 |
return cs_.DoTryLock(); |
|
danderson@0
|
77 |
} |
|
danderson@0
|
78 |
void DoLock() KE_OVERRIDE { |
|
danderson@0
|
79 |
cs_.DoLock(); |
|
danderson@0
|
80 |
} |
|
danderson@0
|
81 |
void DoUnlock() KE_OVERRIDE { |
|
danderson@0
|
82 |
cs_.DoUnlock(); |
|
danderson@0
|
83 |
} |
|
danderson@0
|
84 |
|
|
danderson@0
|
85 |
void Notify() { |
|
danderson@0
|
86 |
AssertCurrentThreadOwns(); |
|
danderson@0
|
87 |
SetEvent(event_); |
|
danderson@0
|
88 |
} |
|
danderson@0
|
89 |
|
|
danderson@0
|
90 |
WaitResult Wait(size_t timeout_ms) { |
|
danderson@0
|
91 |
// This will assert if the lock has not been acquired. We don't need to be |
|
danderson@0
|
92 |
// atomic here, like pthread_cond_wait, because the event bit will stick |
|
danderson@0
|
93 |
// until reset by a wait function. |
|
danderson@0
|
94 |
Unlock(); |
|
danderson@0
|
95 |
DWORD rv = WaitForSingleObject(event_, timeout_ms); |
|
danderson@0
|
96 |
Lock(); |
|
danderson@0
|
97 |
|
|
danderson@0
|
98 |
if (rv == WAIT_TIMEOUT) |
|
danderson@0
|
99 |
return Wait_Timeout; |
|
danderson@0
|
100 |
if (rv == WAIT_FAILED) |
|
danderson@0
|
101 |
return Wait_Error; |
|
danderson@0
|
102 |
return Wait_Signaled; |
|
danderson@0
|
103 |
} |
|
danderson@0
|
104 |
|
|
danderson@0
|
105 |
WaitResult Wait() { |
|
danderson@0
|
106 |
return Wait(INFINITE); |
|
danderson@0
|
107 |
} |
|
danderson@0
|
108 |
|
|
danderson@0
|
109 |
private: |
|
danderson@0
|
110 |
CriticalSection cs_; |
|
danderson@0
|
111 |
HANDLE event_; |
|
danderson@0
|
112 |
}; |
|
danderson@0
|
113 |
|
|
danderson@0
|
114 |
class Thread |
|
danderson@0
|
115 |
{ |
|
danderson@0
|
116 |
public: |
|
danderson@0
|
117 |
Thread(IRunnable *run, const char *name = NULL) { |
|
danderson@0
|
118 |
thread_ = CreateThread(NULL, 0, Main, run, 0, NULL); |
|
danderson@0
|
119 |
} |
|
danderson@0
|
120 |
~Thread() { |
|
danderson@0
|
121 |
if (!thread_) |
|
danderson@0
|
122 |
return; |
|
danderson@0
|
123 |
CloseHandle(thread_); |
|
danderson@0
|
124 |
} |
|
danderson@0
|
125 |
|
|
danderson@0
|
126 |
bool Succeeded() const { |
|
danderson@0
|
127 |
return !!thread_; |
|
danderson@0
|
128 |
} |
|
danderson@0
|
129 |
|
|
danderson@0
|
130 |
void Join() { |
|
danderson@0
|
131 |
if (!Succeeded()) |
|
danderson@0
|
132 |
return; |
|
danderson@0
|
133 |
WaitForSingleObject(thread_, INFINITE); |
|
danderson@0
|
134 |
} |
|
danderson@0
|
135 |
|
|
danderson@0
|
136 |
HANDLE handle() const { |
|
danderson@0
|
137 |
return thread_; |
|
danderson@0
|
138 |
} |
|
danderson@0
|
139 |
|
|
danderson@0
|
140 |
private: |
|
danderson@0
|
141 |
static DWORD WINAPI Main(LPVOID arg) { |
|
danderson@0
|
142 |
((IRunnable *)arg)->Run(); |
|
danderson@0
|
143 |
return 0; |
|
danderson@0
|
144 |
} |
|
danderson@0
|
145 |
|
|
danderson@0
|
146 |
#pragma pack(push, 8) |
|
danderson@0
|
147 |
struct ThreadNameInfo { |
|
danderson@0
|
148 |
DWORD dwType; |
|
danderson@0
|
149 |
LPCSTR szName; |
|
danderson@0
|
150 |
DWORD dwThreadID; |
|
danderson@0
|
151 |
DWORD dwFlags; |
|
danderson@0
|
152 |
}; |
|
danderson@0
|
153 |
#pragma pack(pop) |
|
danderson@0
|
154 |
|
|
danderson@0
|
155 |
private: |
|
danderson@0
|
156 |
HANDLE thread_; |
|
danderson@0
|
157 |
}; |
|
danderson@0
|
158 |
|
|
danderson@0
|
159 |
} // namespace ke |
|
danderson@0
|
160 |
|
|
danderson@0
|
161 |
#endif // _include_amtl_thread_windows_h_ |