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.

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