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.

245 lines
5.6KB

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