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.

238 lines
5.8KB

  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. JackLog("ThreadHandler: start\n");
  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. JackLog("ThreadHandler: exit\n");
  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. DWORD id;
  59. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  60. if (fEvent == NULL) {
  61. jack_error("Cannot create event error = %d", GetLastError());
  62. return -1;
  63. }
  64. fRunning = true;
  65. if (fRealTime) {
  66. JackLog("Create RT thread\n");
  67. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  68. if (fThread == NULL) {
  69. jack_error("Cannot create thread error = %d", GetLastError());
  70. return -1;
  71. }
  72. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  73. jack_error("Cannot set priority class = %d", GetLastError());
  74. return -1;
  75. }
  76. return 0;
  77. } else {
  78. JackLog("Create non RT thread\n");
  79. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  80. if (fThread == NULL) {
  81. jack_error("Cannot create thread error = %d", GetLastError());
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. }
  87. int JackWinThread::StartSync()
  88. {
  89. DWORD id;
  90. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  91. if (fEvent == NULL) {
  92. jack_error("Cannot create event error = %d", GetLastError());
  93. return -1;
  94. }
  95. if (fRealTime) {
  96. JackLog("Create RT thread\n");
  97. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  98. if (fThread == NULL) {
  99. jack_error("Cannot create thread error = %d", GetLastError());
  100. return -1;
  101. }
  102. if (WaitForSingleObject(fEvent, 3000) != WAIT_OBJECT_0) { // wait 3 sec
  103. jack_error("Thread has not started");
  104. return -1;
  105. }
  106. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  107. jack_error("Cannot set priority class = %d", GetLastError());
  108. return -1;
  109. }
  110. return 0;
  111. } else {
  112. JackLog("Create non RT thread\n");
  113. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  114. if (fThread == NULL) {
  115. jack_error("Cannot create thread error = %d", GetLastError());
  116. return -1;
  117. }
  118. if (WaitForSingleObject(fEvent, 3000) != WAIT_OBJECT_0) { // wait 3 sec
  119. jack_error("Thread has not started");
  120. return -1;
  121. }
  122. return 0;
  123. }
  124. }
  125. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  126. int JackWinThread::Kill()
  127. {
  128. if (fThread) { // If thread has been started
  129. TerminateThread(fThread, 0);
  130. WaitForSingleObject(fThread, INFINITE);
  131. CloseHandle(fThread);
  132. JackLog("JackWinThread::Kill 2\n");
  133. fThread = NULL;
  134. fRunning = false;
  135. return 0;
  136. } else {
  137. return -1;
  138. }
  139. }
  140. int JackWinThread::Stop()
  141. {
  142. if (fThread) { // If thread has been started
  143. JackLog("JackWinThread::Stop\n");
  144. fRunning = false; // Request for the thread to stop
  145. WaitForSingleObject(fEvent, INFINITE);
  146. CloseHandle(fThread);
  147. fThread = NULL;
  148. return 0;
  149. } else {
  150. return -1;
  151. }
  152. }
  153. int JackWinThread::AcquireRealTime()
  154. {
  155. JackLog("JackWinThread::AcquireRealTime\n");
  156. if (fThread) {
  157. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  158. jack_error("Cannot set thread priority = %d", GetLastError());
  159. return -1;
  160. }
  161. JackLog("JackWinThread::AcquireRealTime OK\n");
  162. return 0;
  163. } else {
  164. return -1;
  165. }
  166. }
  167. int JackWinThread::AcquireRealTime(int priority)
  168. {
  169. JackLog("JackWinThread::AcquireRealTime priority = %ld\n", priority);
  170. return AcquireRealTime();
  171. }
  172. int JackWinThread::DropRealTime()
  173. {
  174. if (fThread) {
  175. if (!SetThreadPriority(fThread, THREAD_PRIORITY_NORMAL)) {
  176. jack_error("Cannot set thread priority = %d", GetLastError());
  177. return -1;
  178. }
  179. return 0;
  180. } else {
  181. return -1;
  182. }
  183. }
  184. pthread_t JackWinThread::GetThreadID()
  185. {
  186. return fThread;
  187. }
  188. } // end of namespace