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.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. #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. // Signal creation thread when started with StartSync
  30. jack_log("ThreadHandler: start");
  31. obj->fStatus = kIniting;
  32. // Call Init method
  33. if (!runnable->Init()) {
  34. jack_error("Thread init fails: thread quits");
  35. return 0;
  36. }
  37. obj->fStatus = kRunning;
  38. // If Init succeed, start the thread loop
  39. bool res = true;
  40. while (obj->fStatus == kRunning && res) {
  41. res = runnable->Execute();
  42. }
  43. SetEvent(obj->fEvent);
  44. jack_log("ThreadHandler: exit");
  45. return 0;
  46. }
  47. JackWinThread::JackWinThread(JackRunnableInterface* runnable)
  48. : JackThread(runnable, 0, false, 0)
  49. {
  50. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  51. fThread = NULL;
  52. assert(fEvent);
  53. }
  54. JackWinThread::~JackWinThread()
  55. {
  56. CloseHandle(fEvent);
  57. CloseHandle(fThread);
  58. }
  59. int JackWinThread::Start()
  60. {
  61. fStatus = kStarting;
  62. // Check if the thread was correctly started
  63. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  64. fStatus = kIdle;
  65. return -1;
  66. } else {
  67. return 0;
  68. }
  69. }
  70. int JackWinThread::StartSync()
  71. {
  72. fStatus = kStarting;
  73. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  74. fStatus = kIdle;
  75. return -1;
  76. } else {
  77. int count = 0;
  78. while (fStatus == kStarting && ++count < 1000) {
  79. JackSleep(1000);
  80. }
  81. return (count == 1000) ? -1 : 0;
  82. }
  83. }
  84. int JackWinThread::StartImp(pthread_t* thread, int priority, int realtime, ThreadCallback start_routine, void* arg)
  85. {
  86. DWORD id;
  87. *thread = CreateThread(NULL, 0, start_routine, arg, 0, &id);
  88. if (*thread == NULL) {
  89. jack_error("Cannot create thread error = %d", GetLastError());
  90. return -1;
  91. }
  92. if (realtime) {
  93. jack_log("Create RT thread");
  94. if (!SetThreadPriority(*thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  95. jack_error("Cannot set priority class = %d", GetLastError());
  96. return -1;
  97. }
  98. } else {
  99. jack_log("Create non RT thread");
  100. }
  101. return 0;
  102. }
  103. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  104. int JackWinThread::Kill()
  105. {
  106. if (fThread) { // If thread has been started
  107. TerminateThread(fThread, 0);
  108. WaitForSingleObject(fThread, INFINITE);
  109. CloseHandle(fThread);
  110. jack_log("JackWinThread::Kill");
  111. fThread = NULL;
  112. fStatus = kIdle;
  113. return 0;
  114. } else {
  115. return -1;
  116. }
  117. }
  118. int JackWinThread::Stop()
  119. {
  120. if (fThread) { // If thread has been started
  121. jack_log("JackWinThread::Stop");
  122. fStatus = kIdle; // Request for the thread to stop
  123. WaitForSingleObject(fEvent, INFINITE);
  124. CloseHandle(fThread);
  125. fThread = NULL;
  126. return 0;
  127. } else {
  128. return -1;
  129. }
  130. }
  131. int JackWinThread::AcquireRealTime()
  132. {
  133. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  134. }
  135. int JackWinThread::AcquireRealTime(int priority)
  136. {
  137. fPriority = priority;
  138. return AcquireRealTime();
  139. }
  140. int JackWinThread::AcquireRealTimeImp(pthread_t thread, int priority)
  141. {
  142. jack_log("JackWinThread::AcquireRealTime");
  143. if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  144. return 0;
  145. } else {
  146. jack_error("Cannot set thread priority = %d", GetLastError());
  147. return -1;
  148. }
  149. }
  150. int JackWinThread::DropRealTime()
  151. {
  152. return DropRealTimeImp(fThread);
  153. }
  154. int JackWinThread::DropRealTimeImp(pthread_t thread)
  155. {
  156. if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
  157. return 0;
  158. } else {
  159. jack_error("Cannot set thread priority = %d", GetLastError());
  160. return -1;
  161. }
  162. }
  163. pthread_t JackWinThread::GetThreadID()
  164. {
  165. return fThread;
  166. }
  167. void JackWinThread::Terminate()
  168. {
  169. ExitThread(0);
  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. }