2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2006 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "JackWinThread.h"
22 #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("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("ThreadHandler: exit");
57 JackWinThread::JackWinThread(JackRunnableInterface
* runnable
)
58 : 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(pthread_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("Create RT thread");
113 if (!SetThreadPriority(*thread
, THREAD_PRIORITY_TIME_CRITICAL
)) {
114 jack_error("Cannot set priority class = %d", GetLastError());
119 jack_log("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(pthread_t thread
)
158 if (thread
!= (HANDLE
)NULL
) { // If thread has been started
159 TerminateThread(thread
, 0);
160 WaitForSingleObject(thread
, INFINITE
);
168 int JackWinThread::StopImp(pthread_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(pthread_t thread
, int priority
)
203 jack_log("JackWinThread::AcquireRealTime");
205 if (SetThreadPriority(thread
, THREAD_PRIORITY_TIME_CRITICAL
)) {
208 jack_error("Cannot set thread priority = %d", GetLastError());
213 int JackWinThread::DropRealTime()
215 return (fThread
!= (HANDLE
)NULL
) ? DropRealTimeImp(fThread
) : -1;
218 int JackWinThread::DropSelfRealTime()
220 return DropRealTimeImp(GetCurrentThread());
223 int JackWinThread::DropRealTimeImp(pthread_t thread
)
225 if (SetThreadPriority(thread
, THREAD_PRIORITY_NORMAL
)) {
228 jack_error("Cannot set thread priority = %d", GetLastError());
233 pthread_t
JackWinThread::GetThreadID()
238 void JackWinThread::Terminate()
240 jack_log("JackWinThread::Terminate");
244 SERVER_EXPORT
void ThreadExit()
246 jack_log("ThreadExit");
250 } // end of namespace
252 bool jack_get_thread_realtime_priority_range(int * min_ptr
, int * max_ptr
)
257 bool jack_tls_allocate_key(jack_tls_key
*key_ptr
)
262 if (key
== TLS_OUT_OF_INDEXES
)
264 jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
272 bool jack_tls_free_key(jack_tls_key key
)
276 jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
283 bool jack_tls_set(jack_tls_key key
, void *data_ptr
)
285 if (!TlsSetValue(key
, data_ptr
))
287 jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
294 void *jack_tls_get(jack_tls_key key
)
296 return TlsGetValue(key
);