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.

224 lines
5.5KB

  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. namespace Jack
  19. {
  20. DWORD WINAPI JackWinThread::ThreadHandler(void* arg)
  21. {
  22. JackWinThread* obj = (JackWinThread*)arg;
  23. JackRunnableInterface* runnable = obj->fRunnable;
  24. // Call Init method
  25. if (!runnable->Init()) {
  26. jack_error("Thread init fails: thread quits");
  27. return 0;
  28. }
  29. // Signal creation thread when started with StartSync
  30. if (!obj->fRunning) {
  31. obj->fRunning = true;
  32. SetEvent(obj->fEvent);
  33. }
  34. JackLog("ThreadHandler: start\n");
  35. // If Init succeed start the thread loop
  36. bool res = true;
  37. while (obj->fRunning && res) {
  38. res = runnable->Execute();
  39. }
  40. SetEvent(obj->fEvent);
  41. JackLog("ThreadHandler: exit\n");
  42. return 0;
  43. }
  44. int JackWinThread::Start()
  45. {
  46. DWORD id;
  47. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  48. if (fEvent == NULL) {
  49. jack_error("Cannot create event error = %d", GetLastError());
  50. return -1;
  51. }
  52. fRunning = true;
  53. if (fRealTime) {
  54. JackLog("Create RT thread\n");
  55. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  56. if (fThread == NULL) {
  57. jack_error("Cannot create thread error = %d", GetLastError());
  58. return -1;
  59. }
  60. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  61. jack_error("Cannot set priority class = %d", GetLastError());
  62. return -1;
  63. }
  64. return 0;
  65. } else {
  66. JackLog("Create non 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. return 0;
  73. }
  74. }
  75. int JackWinThread::StartSync()
  76. {
  77. DWORD id;
  78. fEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  79. if (fEvent == NULL) {
  80. jack_error("Cannot create event error = %d", GetLastError());
  81. return -1;
  82. }
  83. if (fRealTime) {
  84. JackLog("Create RT thread\n");
  85. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  86. if (fThread == NULL) {
  87. jack_error("Cannot create thread error = %d", GetLastError());
  88. return -1;
  89. }
  90. if (WaitForSingleObject(fEvent, 3000) != WAIT_OBJECT_0) { // wait 3 sec
  91. jack_error("Thread has not started");
  92. return -1;
  93. }
  94. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  95. jack_error("Cannot set priority class = %d", GetLastError());
  96. return -1;
  97. }
  98. return 0;
  99. } else {
  100. JackLog("Create non RT thread\n");
  101. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  102. if (fThread == NULL) {
  103. jack_error("Cannot create thread error = %d", GetLastError());
  104. return -1;
  105. }
  106. if (WaitForSingleObject(fEvent, 3000) != WAIT_OBJECT_0) { // wait 3 sec
  107. jack_error("Thread has not started");
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. }
  113. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  114. int JackWinThread::Kill()
  115. {
  116. if (fThread) { // If thread has been started
  117. JackLog("JackWinThread::Kill\n");
  118. TerminateThread(fThread, 0); /// TO CHECK : dangerous
  119. CloseHandle(fThread);
  120. CloseHandle(fEvent);
  121. return 0;
  122. } else {
  123. return -1;
  124. }
  125. }
  126. int JackWinThread::Stop()
  127. {
  128. if (fThread) { // If thread has been started
  129. JackLog("JackWinThread::Stop\n");
  130. fRunning = false; // Request for the thread to stop
  131. WaitForSingleObject(fEvent, INFINITE);
  132. CloseHandle(fThread);
  133. CloseHandle(fEvent);
  134. return 0;
  135. } else {
  136. CloseHandle(fEvent);
  137. return -1;
  138. }
  139. }
  140. int JackWinThread::AcquireRealTime()
  141. {
  142. JackLog("JackWinThread::AcquireRealTime\n");
  143. if (fThread) {
  144. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  145. jack_error("Cannot set thread priority = %d", GetLastError());
  146. return -1;
  147. }
  148. JackLog("JackWinThread::AcquireRealTime OK\n");
  149. return 0;
  150. } else {
  151. CloseHandle(fEvent);
  152. return -1;
  153. }
  154. }
  155. int JackWinThread::AcquireRealTime(int priority)
  156. {
  157. JackLog("JackWinThread::AcquireRealTime priority = %ld\n", priority);
  158. return AcquireRealTime();
  159. }
  160. int JackWinThread::DropRealTime()
  161. {
  162. if (fThread) {
  163. if (!SetThreadPriority(fThread, THREAD_PRIORITY_NORMAL)) {
  164. jack_error("Cannot set thread priority = %d", GetLastError());
  165. return -1;
  166. }
  167. return 0;
  168. } else {
  169. return -1;
  170. }
  171. }
  172. pthread_t JackWinThread::GetThreadID()
  173. {
  174. return fThread;
  175. }
  176. } // end of namespace