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.

260 lines
6.1KB

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