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.

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