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"
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
;
39 if (!runnable
->Init()) {
40 jack_error("Thread init fails: thread quits");
44 obj
->fStatus
= kRunning
;
46 // If Init succeed, start the thread loop
48 while (obj
->fStatus
== kRunning
&& res
) {
49 res
= runnable
->Execute();
52 SetEvent(obj
->fEvent
);
53 jack_log("JackWinThread::ThreadHandler : exit");
57 JackWinThread::JackWinThread(JackRunnableInterface
* runnable
)
58 : JackMMCSS(), JackThreadInterface(runnable
, 0, false, 0)
60 fEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
61 fThread
= (HANDLE
)NULL
;
65 JackWinThread::~JackWinThread()
71 int JackWinThread::Start()
75 // Check if the thread was correctly started
76 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
84 int JackWinThread::StartSync()
88 if (StartImp(&fThread
, fPriority
, fRealTime
, ThreadHandler
, this) < 0) {
93 while (fStatus
== kStarting
&& ++count
< 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
)
103 *thread
= CreateThread(NULL
, 0, start_routine
, arg
, 0, &id
);
105 if (*thread
== NULL
) {
106 jack_error("Cannot create thread error = %d", GetLastError());
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());
119 jack_log("JackWinThread::StartImp : create non RT thread");
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
;
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
;
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
);
168 int JackWinThread::StopImp(jack_native_thread_t thread
)
170 if (thread
) { // If thread has been started
171 WaitForSingleObject(thread
, INFINITE
);
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");
209 jack_log("MMCSS API not used...");
210 if (SetThreadPriority(thread
, THREAD_PRIORITY_TIME_CRITICAL
)) {
213 jack_error("Cannot set thread priority = %d", GetLastError());
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");
234 } else if (SetThreadPriority(thread
, THREAD_PRIORITY_NORMAL
)) {
237 jack_error("Cannot set thread priority = %d", GetLastError());
242 jack_native_thread_t
JackWinThread::GetThreadID()
247 bool JackWinThread::IsThread()
249 return GetCurrentThread() == fThread
;
252 void JackWinThread::Terminate()
254 jack_log("JackWinThread::Terminate");
258 SERVER_EXPORT
void ThreadExit()
260 jack_log("ThreadExit");
264 } // end of namespace
266 bool jack_get_thread_realtime_priority_range(int * min_ptr
, int * max_ptr
)
271 bool jack_tls_allocate_key(jack_tls_key
*key_ptr
)
276 if (key
== TLS_OUT_OF_INDEXES
)
278 jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
286 bool jack_tls_free_key(jack_tls_key key
)
290 jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
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());
308 void *jack_tls_get(jack_tls_key key
)
310 return TlsGetValue(key
);