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.

222 lines
5.4KB

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