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.

253 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. jack_log("JackWinThread::AcquireRealTime OK");
  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. return 0;
  160. } else {
  161. jack_error("Cannot set thread priority = %d", GetLastError());
  162. return -1;
  163. }
  164. }
  165. pthread_t JackWinThread::GetThreadID()
  166. {
  167. return fThread;
  168. }
  169. void JackWinThread::Terminate()
  170. {
  171. ExitThread(0);
  172. }
  173. } // end of namespace
  174. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  175. {
  176. DWORD key;
  177. key = TlsAlloc();
  178. if (key == TLS_OUT_OF_INDEXES)
  179. {
  180. jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
  181. return false;
  182. }
  183. *key_ptr = key;
  184. return true;
  185. }
  186. bool jack_tls_free_key(jack_tls_key key)
  187. {
  188. if (!TlsFree(key))
  189. {
  190. jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
  191. return false;
  192. }
  193. return true;
  194. }
  195. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  196. {
  197. if (!TlsSetValue(key, data_ptr))
  198. {
  199. jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
  200. return false;
  201. }
  202. return true;
  203. }
  204. void *jack_tls_get(jack_tls_key key)
  205. {
  206. return TlsGetValue(key);
  207. }