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;
35 jack_log(
"JackWinThread::ThreadHandler : start");
36 obj->fStatus = kIniting;
39 if (!runnable->Init()) {
44 obj->fStatus = kRunning;
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()
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");
127 int JackWinThread::Kill()
129 if (fThread != (HANDLE)NULL) {
130 TerminateThread(fThread, 0);
131 WaitForSingleObject(fThread, INFINITE);
132 CloseHandle(fThread);
134 fThread = (HANDLE)NULL;
142 int JackWinThread::Stop()
144 if (fThread != (HANDLE)NULL) {
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) {
159 TerminateThread(thread, 0);
160 WaitForSingleObject(thread, INFINITE);
168 int JackWinThread::StopImp(jack_native_thread_t thread)
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 >= 90 && MMCSSAcquireRealTime(thread) == 0) {
206 jack_info(
"MMCSS API used to acquire RT for thread");
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_info(
"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()
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);
SERVER_EXPORT void jack_error(const char *fmt,...)
SERVER_EXPORT void jack_info(const char *fmt,...)
SERVER_EXPORT void jack_log(const char *fmt,...)