Revert "metadata: Make JACK_METADATA_* constant"
[jack2.git] / windows / JackWinThread.cpp
blobd586f9640e0b5c804bc5365ec878ad3b15441e0b
1 /*
2 Copyright (C) 2004-2008 Grame
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.
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.
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.
20 #include "JackWinThread.h"
21 #include "JackError.h"
22 #include "JackTime.h"
23 #include <assert.h>
24 #include <stdio.h>
26 namespace Jack
29 DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
31 JackWinThread* obj = (JackWinThread*)arg;
32 JackRunnableInterface* runnable = obj->fRunnable;
34 // Signal creation thread when started with StartSync
35 jack_log("JackWinThread::ThreadHandler : start");
36 obj->fStatus = kIniting;
38 // Call Init method
39 if (!runnable->Init()) {
40 jack_error("Thread init fails: thread quits");
41 return 0;
44 obj->fStatus = kRunning;
46 // If Init succeed, start the thread loop
47 bool res = true;
48 while (obj->fStatus == kRunning && res) {
49 res = runnable->Execute();
52 SetEvent(obj->fEvent);
53 jack_log("JackWinThread::ThreadHandler : exit");
54 return 0;
57 JackWinThread::JackWinThread(JackRunnableInterface* runnable)
58 : JackMMCSS(), JackThreadInterface(runnable, 0, false, 0)
60 fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
61 fThread = (HANDLE)NULL;
62 assert(fEvent);
65 JackWinThread::~JackWinThread()
67 CloseHandle(fEvent);
68 CloseHandle(fThread);
71 int JackWinThread::Start()
73 fStatus = kStarting;
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;
84 int JackWinThread::StartSync()
86 fStatus = kStarting;
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);
96 return (count == 1000) ? -1 : 0;
100 int JackWinThread::StartImp(jack_native_thread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
102 DWORD id;
103 *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);
105 if (*thread == NULL) {
106 jack_error("Cannot create thread error = %d", GetLastError());
107 return -1;
110 if (realtime) {
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;
118 } else {
119 jack_log("JackWinThread::StartImp : create non RT thread");
122 return 0;
125 // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
127 int JackWinThread::Kill()
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;
142 int JackWinThread::Stop()
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;
156 int JackWinThread::KillImp(jack_native_thread_t thread)
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;
168 int JackWinThread::StopImp(jack_native_thread_t thread)
170 if (thread) { // If thread has been started
171 WaitForSingleObject(thread, INFINITE);
172 CloseHandle(thread);
173 return 0;
174 } else {
175 return -1;
179 int JackWinThread::AcquireRealTime()
181 return (fThread != (HANDLE)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
184 int JackWinThread::AcquireSelfRealTime()
186 return AcquireRealTimeImp(GetCurrentThread(), fPriority);
189 int JackWinThread::AcquireRealTime(int priority)
191 fPriority = priority;
192 return AcquireRealTime();
195 int JackWinThread::AcquireSelfRealTime(int priority)
197 fPriority = priority;
198 return AcquireSelfRealTime();
201 int JackWinThread::AcquireRealTimeImp(jack_native_thread_t thread, int priority)
203 jack_log("JackWinThread::AcquireRealTimeImp priority = %d", priority);
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;
219 int JackWinThread::DropRealTime()
221 return (fThread != (HANDLE)NULL) ? DropRealTimeImp(fThread) : -1;
224 int JackWinThread::DropSelfRealTime()
226 return DropRealTimeImp(GetCurrentThread());
229 int JackWinThread::DropRealTimeImp(jack_native_thread_t thread)
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;
242 jack_native_thread_t JackWinThread::GetThreadID()
244 return fThread;
247 bool JackWinThread::IsThread()
249 return GetCurrentThread() == fThread;
252 void JackWinThread::Terminate()
254 jack_log("JackWinThread::Terminate");
255 ExitThread(0);
258 SERVER_EXPORT void ThreadExit()
260 jack_log("ThreadExit");
261 ExitThread(0);
264 } // end of namespace
266 bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
268 return false;
271 bool jack_tls_allocate_key(jack_tls_key *key_ptr)
273 DWORD key;
275 key = TlsAlloc();
276 if (key == TLS_OUT_OF_INDEXES)
278 jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
279 return false;
282 *key_ptr = key;
283 return true;
286 bool jack_tls_free_key(jack_tls_key key)
288 if (!TlsFree(key))
290 jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
291 return false;
294 return true;
297 bool jack_tls_set(jack_tls_key key, void *data_ptr)
299 if (!TlsSetValue(key, data_ptr))
301 jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
302 return false;
305 return true;
308 void *jack_tls_get(jack_tls_key key)
310 return TlsGetValue(key);