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.

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