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.

254 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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackWinThread.h"
  20. #include "JackError.h"
  21. #include "JackTime.h"
  22. #include <assert.h>
  23. namespace Jack
  24. {
  25. DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
  26. {
  27. JackWinThread* obj = (JackWinThread*)arg;
  28. JackRunnableInterface* runnable = obj->fRunnable;
  29. set_threaded_log_function();
  30. // Signal creation thread when started with StartSync
  31. jack_log("ThreadHandler: start");
  32. obj->fStatus = kIniting;
  33. // Call Init method
  34. if (!runnable->Init()) {
  35. jack_error("Thread init fails: thread quits");
  36. return 0;
  37. }
  38. obj->fStatus = kRunning;
  39. // If Init succeed, start the thread loop
  40. bool res = true;
  41. while (obj->fStatus == kRunning && res) {
  42. res = runnable->Execute();
  43. }
  44. SetEvent(obj->fEvent);
  45. jack_log("ThreadHandler: exit");
  46. return 0;
  47. }
  48. JackWinThread::JackWinThread(JackRunnableInterface* runnable)
  49. : JackThread(runnable, 0, false, 0)
  50. {
  51. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  52. fThread = NULL;
  53. assert(fEvent);
  54. }
  55. JackWinThread::~JackWinThread()
  56. {
  57. CloseHandle(fEvent);
  58. CloseHandle(fThread);
  59. }
  60. int JackWinThread::Start()
  61. {
  62. fStatus = kStarting;
  63. // Check if the thread was correctly started
  64. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  65. fStatus = kIdle;
  66. return -1;
  67. } else {
  68. return 0;
  69. }
  70. }
  71. int JackWinThread::StartSync()
  72. {
  73. fStatus = kStarting;
  74. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  75. fStatus = kIdle;
  76. return -1;
  77. } else {
  78. int count = 0;
  79. while (fStatus == kStarting && ++count < 1000) {
  80. JackSleep(1000);
  81. }
  82. return (count == 1000) ? -1 : 0;
  83. }
  84. }
  85. int JackWinThread::StartImp(pthread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
  86. {
  87. DWORD id;
  88. *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);
  89. if (*thread == NULL) {
  90. jack_error("Cannot create thread error = %d", GetLastError());
  91. return -1;
  92. }
  93. if (realtime) {
  94. jack_log("Create RT thread");
  95. if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  96. jack_error("Cannot set priority class = %d", GetLastError());
  97. return -1;
  98. }
  99. } else {
  100. jack_log("Create non RT thread");
  101. }
  102. return 0;
  103. }
  104. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  105. int JackWinThread::Kill()
  106. {
  107. if (fThread) { // If thread has been started
  108. TerminateThread(fThread, 0);
  109. WaitForSingleObject(fThread, INFINITE);
  110. CloseHandle(fThread);
  111. jack_log("JackWinThread::Kill");
  112. fThread = NULL;
  113. fStatus = kIdle;
  114. return 0;
  115. } else {
  116. return -1;
  117. }
  118. }
  119. int JackWinThread::Stop()
  120. {
  121. if (fThread) { // If thread has been started
  122. jack_log("JackWinThread::Stop");
  123. fStatus = kIdle; // Request for the thread to stop
  124. WaitForSingleObject(fEvent, INFINITE);
  125. CloseHandle(fThread);
  126. fThread = NULL;
  127. return 0;
  128. } else {
  129. return -1;
  130. }
  131. }
  132. int JackWinThread::AcquireRealTime()
  133. {
  134. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  135. }
  136. int JackWinThread::AcquireRealTime(int priority)
  137. {
  138. fPriority = priority;
  139. return AcquireRealTime();
  140. }
  141. int JackWinThread::AcquireRealTimeImp(pthread_t thread, int priority)
  142. {
  143. jack_log("JackWinThread::AcquireRealTime");
  144. if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  145. fRealTime = true;
  146. return 0;
  147. } else {
  148. jack_error("Cannot set thread priority = %d", GetLastError());
  149. return -1;
  150. }
  151. }
  152. int JackWinThread::DropRealTime()
  153. {
  154. return DropRealTimeImp(fThread);
  155. }
  156. int JackWinThread::DropRealTimeImp(pthread_t thread)
  157. {
  158. if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
  159. fRealTime = false;
  160. return 0;
  161. } else {
  162. jack_error("Cannot set thread priority = %d", GetLastError());
  163. return -1;
  164. }
  165. }
  166. pthread_t JackWinThread::GetThreadID()
  167. {
  168. return fThread;
  169. }
  170. void JackWinThread::Terminate()
  171. {
  172. ExitThread(0);
  173. }
  174. } // end of namespace
  175. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  176. {
  177. DWORD key;
  178. key = TlsAlloc();
  179. if (key == TLS_OUT_OF_INDEXES)
  180. {
  181. jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
  182. return false;
  183. }
  184. *key_ptr = key;
  185. return true;
  186. }
  187. bool jack_tls_free_key(jack_tls_key key)
  188. {
  189. if (!TlsFree(key))
  190. {
  191. jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
  192. return false;
  193. }
  194. return true;
  195. }
  196. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  197. {
  198. if (!TlsSetValue(key, data_ptr))
  199. {
  200. jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
  201. return false;
  202. }
  203. return true;
  204. }
  205. void *jack_tls_get(jack_tls_key key)
  206. {
  207. return TlsGetValue(key);
  208. }