Jack2 1.9.12

JackWinThread.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 */
19
20 #include "JackWinThread.h"
21 #include "JackError.h"
22 #include "JackTime.h"
23 #include <assert.h>
24 #include <stdio.h>
25
26 namespace Jack
27 {
28
29 DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
30 {
31 JackWinThread* obj = (JackWinThread*)arg;
32 JackRunnableInterface* runnable = obj->fRunnable;
33
34 // Signal creation thread when started with StartSync
35 jack_log("JackWinThread::ThreadHandler : start");
36 obj->fStatus = kIniting;
37
38 // Call Init method
39 if (!runnable->Init()) {
40 jack_error("Thread init fails: thread quits");
41 return 0;
42 }
43
44 obj->fStatus = kRunning;
45
46 // If Init succeed, start the thread loop
47 bool res = true;
48 while (obj->fStatus == kRunning && res) {
49 res = runnable->Execute();
50 }
51
52 SetEvent(obj->fEvent);
53 jack_log("JackWinThread::ThreadHandler : exit");
54 return 0;
55 }
56
57 JackWinThread::JackWinThread(JackRunnableInterface* runnable)
58 : JackMMCSS(), JackThreadInterface(runnable, 0, false, 0)
59 {
60 fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
61 fThread = (HANDLE)NULL;
62 assert(fEvent);
63 }
64
65 JackWinThread::~JackWinThread()
66 {
67 CloseHandle(fEvent);
68 CloseHandle(fThread);
69 }
70
71 int JackWinThread::Start()
72 {
73 fStatus = kStarting;
74
75 // Check if the thread was correctly started
76 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
77 fStatus = kIdle;
78 return -1;
79 } else {
80 return 0;
81 }
82 }
83
84 int JackWinThread::StartSync()
85 {
86 fStatus = kStarting;
87
88 if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
89 fStatus = kIdle;
90 return -1;
91 } else {
92 int count = 0;
93 while (fStatus == kStarting && ++count < 1000) {
94 JackSleep(1000);
95 }
96 return (count == 1000) ? -1 : 0;
97 }
98 }
99
100 int JackWinThread::StartImp(jack_native_thread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
101 {
102 DWORD id;
103 *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);
104
105 if (*thread == NULL) {
106 jack_error("Cannot create thread error = %d", GetLastError());
107 return -1;
108 }
109
110 if (realtime) {
111
112 jack_log("JackWinThread::StartImp : create RT thread");
113 if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
114 jack_error("Cannot set priority class = %d", GetLastError());
115 return -1;
116 }
117
118 } else {
119 jack_log("JackWinThread::StartImp : create non RT thread");
120 }
121
122 return 0;
123 }
124
125 // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
126
127 int JackWinThread::Kill()
128 {
129 if (fThread != (HANDLE)NULL) { // If thread has been started
130 TerminateThread(fThread, 0);
131 WaitForSingleObject(fThread, INFINITE);
132 CloseHandle(fThread);
133 jack_log("JackWinThread::Kill");
134 fThread = (HANDLE)NULL;
135 fStatus = kIdle;
136 return 0;
137 } else {
138 return -1;
139 }
140 }
141
142 int JackWinThread::Stop()
143 {
144 if (fThread != (HANDLE)NULL) { // If thread has been started
145 jack_log("JackWinThread::Stop");
146 fStatus = kIdle; // Request for the thread to stop
147 WaitForSingleObject(fEvent, INFINITE);
148 CloseHandle(fThread);
149 fThread = (HANDLE)NULL;
150 return 0;
151 } else {
152 return -1;
153 }
154 }
155
156 int JackWinThread::KillImp(jack_native_thread_t thread)
157 {
158 if (thread != (HANDLE)NULL) { // If thread has been started
159 TerminateThread(thread, 0);
160 WaitForSingleObject(thread, INFINITE);
161 CloseHandle(thread);
162 return 0;
163 } else {
164 return -1;
165 }
166 }
167
168 int JackWinThread::StopImp(jack_native_thread_t thread)
169 {
170 if (thread) { // If thread has been started
171 WaitForSingleObject(thread, INFINITE);
172 CloseHandle(thread);
173 return 0;
174 } else {
175 return -1;
176 }
177 }
178
179 int JackWinThread::AcquireRealTime()
180 {
181 return (fThread != (HANDLE)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
182 }
183
184 int JackWinThread::AcquireSelfRealTime()
185 {
186 return AcquireRealTimeImp(GetCurrentThread(), fPriority);
187 }
188
189 int JackWinThread::AcquireRealTime(int priority)
190 {
191 fPriority = priority;
192 return AcquireRealTime();
193 }
194
195 int JackWinThread::AcquireSelfRealTime(int priority)
196 {
197 fPriority = priority;
198 return AcquireSelfRealTime();
199 }
200
201 int JackWinThread::AcquireRealTimeImp(jack_native_thread_t thread, int priority)
202 {
203 jack_log("JackWinThread::AcquireRealTimeImp priority = %d", priority);
204
205 if (priority >= (BASE_REALTIME_PRIORITY - 1) && MMCSSAcquireRealTime(thread, priority) == 0) {
206 jack_log("MMCSS API used to acquire RT for thread");
207 return 0;
208 } else {
209 jack_log("MMCSS API not used...");
210 if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
211 return 0;
212 } else {
213 jack_error("Cannot set thread priority = %d", GetLastError());
214 return -1;
215 }
216 }
217 }
218
219 int JackWinThread::DropRealTime()
220 {
221 return (fThread != (HANDLE)NULL) ? DropRealTimeImp(fThread) : -1;
222 }
223
224 int JackWinThread::DropSelfRealTime()
225 {
226 return DropRealTimeImp(GetCurrentThread());
227 }
228
229 int JackWinThread::DropRealTimeImp(jack_native_thread_t thread)
230 {
231 if (MMCSSDropRealTime(thread) == 0) {
232 jack_log("MMCSS API used to drop RT for thread");
233 return 0;
234 } else if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
235 return 0;
236 } else {
237 jack_error("Cannot set thread priority = %d", GetLastError());
238 return -1;
239 }
240 }
241
242 jack_native_thread_t JackWinThread::GetThreadID()
243 {
244 return fThread;
245 }
246
247 bool JackWinThread::IsThread()
248 {
249 return GetCurrentThread() == fThread;
250 }
251
252 void JackWinThread::Terminate()
253 {
254 jack_log("JackWinThread::Terminate");
255 ExitThread(0);
256 }
257
258 SERVER_EXPORT void ThreadExit()
259 {
260 jack_log("ThreadExit");
261 ExitThread(0);
262 }
263
264 } // end of namespace
265
266 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
267 {
268 return false;
269 }
270
271 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
272 {
273 DWORD key;
274
275 key = TlsAlloc();
276 if (key == TLS_OUT_OF_INDEXES)
277 {
278 jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
279 return false;
280 }
281
282 *key_ptr = key;
283 return true;
284 }
285
286 bool jack_tls_free_key(jack_tls_key key)
287 {
288 if (!TlsFree(key))
289 {
290 jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
291 return false;
292 }
293
294 return true;
295 }
296
297 bool jack_tls_set(jack_tls_key key, void *data_ptr)
298 {
299 if (!TlsSetValue(key, data_ptr))
300 {
301 jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
302 return false;
303 }
304
305 return true;
306 }
307
308 void *jack_tls_get(jack_tls_key key)
309 {
310 return TlsGetValue(key);
311 }
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108