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.

280 lines
6.2KB

  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. : JackThreadInterface(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::KillImp(pthread_t thread)
  132. {
  133. if (thread) { // If thread has been started
  134. TerminateThread(thread, 0);
  135. WaitForSingleObject(thread, INFINITE);
  136. CloseHandle(thread);
  137. return 0;
  138. } else {
  139. return -1;
  140. }
  141. }
  142. int JackWinThread::StopImp(pthread_t thread)
  143. {
  144. if (thread) { // If thread has been started
  145. WaitForSingleObject(thread, INFINITE);
  146. CloseHandle(thread);
  147. return 0;
  148. } else {
  149. return -1;
  150. }
  151. }
  152. int JackWinThread::AcquireRealTime()
  153. {
  154. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  155. }
  156. int JackWinThread::AcquireRealTime(int priority)
  157. {
  158. fPriority = priority;
  159. return AcquireRealTime();
  160. }
  161. int JackWinThread::AcquireRealTimeImp(pthread_t thread, int priority)
  162. {
  163. jack_log("JackWinThread::AcquireRealTime");
  164. if (SetThreadPriority(thread, THREAD_PRIORITY_TIME_CRITICAL)) {
  165. return 0;
  166. } else {
  167. jack_error("Cannot set thread priority = %d", GetLastError());
  168. return -1;
  169. }
  170. }
  171. int JackWinThread::DropRealTime()
  172. {
  173. return DropRealTimeImp(fThread);
  174. }
  175. int JackWinThread::DropRealTimeImp(pthread_t thread)
  176. {
  177. if (SetThreadPriority(thread, THREAD_PRIORITY_NORMAL)) {
  178. return 0;
  179. } else {
  180. jack_error("Cannot set thread priority = %d", GetLastError());
  181. return -1;
  182. }
  183. }
  184. pthread_t JackWinThread::GetThreadID()
  185. {
  186. return fThread;
  187. }
  188. void JackWinThread::Terminate()
  189. {
  190. jack_log("JackWinThread::Terminate");
  191. ExitThread(0);
  192. }
  193. SERVER_EXPORT void ThreadExit()
  194. {
  195. jack_log("ThreadExit");
  196. ExitThread(0);
  197. }
  198. } // end of namespace
  199. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  200. {
  201. DWORD key;
  202. key = TlsAlloc();
  203. if (key == TLS_OUT_OF_INDEXES)
  204. {
  205. jack_error("TlsAlloc() failed. Error is %d", (unsigned int)GetLastError());
  206. return false;
  207. }
  208. *key_ptr = key;
  209. return true;
  210. }
  211. bool jack_tls_free_key(jack_tls_key key)
  212. {
  213. if (!TlsFree(key))
  214. {
  215. jack_error("TlsFree() failed. Error is %d", (unsigned int)GetLastError());
  216. return false;
  217. }
  218. return true;
  219. }
  220. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  221. {
  222. if (!TlsSetValue(key, data_ptr))
  223. {
  224. jack_error("TlsSetValue() failed. Error is %d", (unsigned int)GetLastError());
  225. return false;
  226. }
  227. return true;
  228. }
  229. void *jack_tls_get(jack_tls_key key)
  230. {
  231. return TlsGetValue(key);
  232. }