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.

237 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. JackLog("JackWinThread::Kill\n");
  130. TerminateThread(fThread, 0); /// TO CHECK : dangerous
  131. CloseHandle(fThread);
  132. fThread = NULL;
  133. fRunning = false;
  134. return 0;
  135. } else {
  136. return -1;
  137. }
  138. }
  139. int JackWinThread::Stop()
  140. {
  141. if (fThread) { // If thread has been started
  142. JackLog("JackWinThread::Stop\n");
  143. fRunning = false; // Request for the thread to stop
  144. WaitForSingleObject(fEvent, INFINITE);
  145. CloseHandle(fThread);
  146. fThread = NULL;
  147. return 0;
  148. } else {
  149. return -1;
  150. }
  151. }
  152. int JackWinThread::AcquireRealTime()
  153. {
  154. JackLog("JackWinThread::AcquireRealTime\n");
  155. if (fThread) {
  156. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  157. jack_error("Cannot set thread priority = %d", GetLastError());
  158. return -1;
  159. }
  160. JackLog("JackWinThread::AcquireRealTime OK\n");
  161. return 0;
  162. } else {
  163. return -1;
  164. }
  165. }
  166. int JackWinThread::AcquireRealTime(int priority)
  167. {
  168. JackLog("JackWinThread::AcquireRealTime priority = %ld\n", priority);
  169. return AcquireRealTime();
  170. }
  171. int JackWinThread::DropRealTime()
  172. {
  173. if (fThread) {
  174. if (!SetThreadPriority(fThread, THREAD_PRIORITY_NORMAL)) {
  175. jack_error("Cannot set thread priority = %d", GetLastError());
  176. return -1;
  177. }
  178. return 0;
  179. } else {
  180. return -1;
  181. }
  182. }
  183. pthread_t JackWinThread::GetThreadID()
  184. {
  185. return fThread;
  186. }
  187. } // end of namespace