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.

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