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.

194 lines
4.6KB

  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. fRunning = true;
  59. StartSync():
  60. }
  61. int JackWinThread::StartSync()
  62. {
  63. DWORD id;
  64. if (fRealTime) {
  65. JackLog("Create RT thread\n");
  66. fThread = CreateThread(NULL, 0, ThreadHandler, (void*)this, 0, &id);
  67. if (fThread == NULL) {
  68. jack_error("Cannot create thread error = %d", GetLastError());
  69. return -1;
  70. }
  71. if (WaitForSingleObject(fEvent, 3000) != WAIT_OBJECT_0) { // wait 3 sec
  72. jack_error("Thread has not started");
  73. return -1;
  74. }
  75. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  76. jack_error("Cannot set priority class = %d", GetLastError());
  77. return -1;
  78. }
  79. return 0;
  80. } else {
  81. JackLog("Create non 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. return 0;
  92. }
  93. }
  94. // voir http://www.microsoft.com/belux/msdn/nl/community/columns/ldoc/multithread1.mspx
  95. int JackWinThread::Kill()
  96. {
  97. if (fThread) { // If thread has been started
  98. JackLog("JackWinThread::Kill\n");
  99. TerminateThread(fThread, 0); /// TO CHECK : dangerous
  100. CloseHandle(fThread);
  101. fThread = NULL;
  102. fRunning = false;
  103. return 0;
  104. } else {
  105. return -1;
  106. }
  107. }
  108. int JackWinThread::Stop()
  109. {
  110. if (fThread) { // If thread has been started
  111. JackLog("JackWinThread::Stop\n");
  112. fRunning = false; // Request for the thread to stop
  113. WaitForSingleObject(fEvent, INFINITE);
  114. CloseHandle(fThread);
  115. fThread = NULL;
  116. return 0;
  117. } else {
  118. return -1;
  119. }
  120. }
  121. int JackWinThread::AcquireRealTime()
  122. {
  123. JackLog("JackWinThread::AcquireRealTime\n");
  124. if (fThread) {
  125. if (!SetThreadPriority(fThread, THREAD_PRIORITY_TIME_CRITICAL)) {
  126. jack_error("Cannot set thread priority = %d", GetLastError());
  127. return -1;
  128. }
  129. JackLog("JackWinThread::AcquireRealTime OK\n");
  130. return 0;
  131. } else {
  132. return -1;
  133. }
  134. }
  135. int JackWinThread::AcquireRealTime(int priority)
  136. {
  137. JackLog("JackWinThread::AcquireRealTime priority = %ld\n", priority);
  138. return AcquireRealTime();
  139. }
  140. int JackWinThread::DropRealTime()
  141. {
  142. if (fThread) {
  143. if (!SetThreadPriority(fThread, THREAD_PRIORITY_NORMAL)) {
  144. jack_error("Cannot set thread priority = %d", GetLastError());
  145. return -1;
  146. }
  147. return 0;
  148. } else {
  149. return -1;
  150. }
  151. }
  152. pthread_t JackWinThread::GetThreadID()
  153. {
  154. return fThread;
  155. }
  156. } // end of namespace