jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
5.7KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackWinThread.h"
  17. #include "JackError.h"
  18. #include "JackTime.h"
  19. #include <assert.h>
  20. namespace Jack
  21. {
  22. DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
  23. {
  24. JackWinThread* obj = (JackWinThread*)arg;
  25. JackRunnableInterface* runnable = obj->fRunnable;
  26. set_threaded_log_function();
  27. // Signal creation thread when started with StartSync
  28. jack_log("ThreadHandler: start");
  29. obj->fStatus = kIniting;
  30. // Call Init method
  31. if (!runnable->Init()) {
  32. jack_error("Thread init fails: thread quits");
  33. return 0;
  34. }
  35. obj->fStatus = kRunning;
  36. // If Init succeed, start the thread loop
  37. bool res = true;
  38. while (obj->fStatus == kRunning && res) {
  39. res = runnable->Execute();
  40. }
  41. SetEvent(obj->fEvent);
  42. jack_log("ThreadHandler: exit");
  43. return 0;
  44. }
  45. JackWinThread::JackWinThread(JackRunnableInterface* runnable)
  46. : JackThread(runnable, 0, false, 0)
  47. {
  48. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  49. fThread = NULL;
  50. assert(fEvent);
  51. }
  52. JackWinThread::~JackWinThread()
  53. {
  54. CloseHandle(fEvent);
  55. }
  56. int JackWinThread::Start()
  57. {
  58. fStatus = kStarting;
  59. // Check if the thread was correctly started
  60. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  61. fStatus = kIdle;
  62. return -1;
  63. } else {
  64. return 0;
  65. }
  66. }
  67. int JackWinThread::StartSync()
  68. {
  69. fStatus = kStarting;
  70. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  71. fStatus = kIdle;
  72. return -1;
  73. } else {
  74. int count = 0;
  75. while (fStatus == kStarting && ++count < 1000) {
  76. JackSleep(1000);
  77. }
  78. return (count == 1000) ? -1 : 0;
  79. }
  80. }
  81. int JackWinThread::StartImp(pthread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
  82. {
  83. DWORD id;
  84. *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);
  85. if (*thread == NULL) {
  86. jack_error("Cannot create thread error = %d", GetLastError());
  87. return -1;
  88. }
  89. if (realtime) {
  90. jack_log("Create RT thread");
  91. if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  92. jack_error("Cannot set priority class = %d", GetLastError());
  93. return -1;
  94. }
  95. } else {
  96. jack_log("Create non RT thread");
  97. }
  98. return 0;
  99. }
  100. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  101. int JackWinThread::Kill()
  102. {
  103. if (fThread) { // If thread has been started
  104. TerminateThread(fThread, 0);
  105. WaitForSingleObject(fThread, INFINITE);
  106. CloseHandle(fThread);
  107. jack_log("JackWinThread::Kill");
  108. fThread = NULL;
  109. fStatus = kIdle;
  110. return 0;
  111. } else {
  112. return -1;
  113. }
  114. }
  115. int JackWinThread::Stop()
  116. {
  117. if (fThread) { // If thread has been started
  118. jack_log("JackWinThread::Stop");
  119. fStatus = kIdle; // Request for the thread to stop
  120. WaitForSingleObject(fEvent, INFINITE);
  121. CloseHandle(fThread);
  122. fThread = NULL;
  123. return 0;
  124. } else {
  125. return -1;
  126. }
  127. }
  128. int JackWinThread::AcquireRealTime()
  129. {
  130. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  131. }
  132. int JackWinThread::AcquireRealTime(int priority)
  133. {
  134. fPriority = priority;
  135. return AcquireRealTime();
  136. }
  137. int JackWinThread::AcquireRealTimeImp(pthread_t thread, int priority)
  138. {
  139. jack_log("JackWinThread::AcquireRealTime");
  140. if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  141. jack_log("JackWinThread::AcquireRealTime OK");
  142. return 0;
  143. } else {
  144. jack_error("Cannot set thread priority = %d", GetLastError());
  145. return -1;
  146. }
  147. }
  148. int JackWinThread::DropRealTime()
  149. {
  150. return DropRealTimeImp(fThread);
  151. }
  152. int JackWinThread::DropRealTimeImp(pthread_t thread)
  153. {
  154. if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
  155. return 0;
  156. } else {
  157. jack_error("Cannot set thread priority = %d", GetLastError());
  158. return -1;
  159. }
  160. }
  161. pthread_t JackWinThread::GetThreadID()
  162. {
  163. return fThread;
  164. }
  165. void JackWinThread::Terminate()
  166. {
  167. TerminateThread(fThread, 0);
  168. WaitForSingleObject(fThread, INFINITE);
  169. CloseHandle(fThread);
  170. }
  171. } // end of namespace
  172. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  173. {
  174. DWORD key;
  175. key = TlsAlloc();
  176. if (key == TLS_OUT_OF_INDEXES)
  177. {
  178. jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
  179. return false;
  180. }
  181. *key_ptr = key;
  182. return true;
  183. }
  184. bool jack_tls_free_key(jack_tls_key key)
  185. {
  186. if (!TlsFree(key))
  187. {
  188. jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
  189. return false;
  190. }
  191. return true;
  192. }
  193. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  194. {
  195. if (!TlsSetValue(key, data_ptr))
  196. {
  197. jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
  198. return false;
  199. }
  200. return true;
  201. }
  202. void *jack_tls_get(jack_tls_key key)
  203. {
  204. return TlsGetValue(key);
  205. }