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.

213 lines
5.7KB

  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 "JackPosixThread.h"
  17. #include "JackError.h"
  18. #include <string.h> // for memset
  19. namespace Jack
  20. {
  21. void* JackPosixThread::ThreadHandler(void* arg)
  22. {
  23. JackPosixThread* obj = (JackPosixThread*)arg;
  24. JackRunnableInterface* runnable = obj->fRunnable;
  25. int err;
  26. if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
  27. jack_error("pthread_setcanceltype err = %s", strerror(err));
  28. }
  29. // Call Init method
  30. if (!runnable->Init()) {
  31. jack_error("Thread init fails: thread quits");
  32. return 0;
  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. //pthread_testcancel();
  40. }
  41. JackLog("ThreadHandler: exit\n");
  42. return 0;
  43. }
  44. int JackPosixThread::Start()
  45. {
  46. int res;
  47. fRunning = true;
  48. if (fRealTime) {
  49. JackLog("Create RT thread\n");
  50. /* Get the client thread to run as an RT-FIFO
  51. scheduled thread of appropriate priority.
  52. */
  53. pthread_attr_t attributes;
  54. struct sched_param rt_param;
  55. pthread_attr_init(&attributes);
  56. if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
  57. jack_error("Cannot request explicit scheduling for RT thread %d %s", res, strerror(errno));
  58. return -1;
  59. }
  60. if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
  61. jack_error("Cannot request joinable thread creation for RT thread %d %s", res, strerror(errno));
  62. return -1;
  63. }
  64. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  65. jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
  66. return -1;
  67. }
  68. //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
  69. if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
  70. jack_error("Cannot set FIFO scheduling class for RT thread %d %s", res, strerror(errno));
  71. return -1;
  72. }
  73. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  74. jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
  75. return -1;
  76. }
  77. memset(&rt_param, 0, sizeof(rt_param));
  78. rt_param.sched_priority = fPriority;
  79. if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
  80. jack_error("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
  81. return -1;
  82. }
  83. if ((res = pthread_create(&fThread, &attributes, ThreadHandler, this))) {
  84. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  85. return -1;
  86. }
  87. return 0;
  88. } else {
  89. JackLog("Create non RT thread\n");
  90. if ((res = pthread_create(&fThread, 0, ThreadHandler, this))) {
  91. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. }
  97. int JackPosixThread::StartSync()
  98. {
  99. jack_error("Not implemented yet");
  100. return -1;
  101. }
  102. int JackPosixThread::Kill()
  103. {
  104. if (fThread) { // If thread has been started
  105. JackLog("JackPosixThread::Kill\n");
  106. void* status;
  107. pthread_cancel(fThread);
  108. pthread_join(fThread, &status);
  109. fRunning = false;
  110. fThread = (pthread_t)NULL;
  111. return 0;
  112. } else {
  113. return -1;
  114. }
  115. }
  116. int JackPosixThread::Stop()
  117. {
  118. if (fThread) { // If thread has been started
  119. JackLog("JackPosixThread::Stop\n");
  120. void* status;
  121. fRunning = false; // Request for the thread to stop
  122. pthread_join(fThread, &status);
  123. fThread = (pthread_t)NULL;
  124. return 0;
  125. } else {
  126. return -1;
  127. }
  128. }
  129. int JackPosixThread::AcquireRealTime()
  130. {
  131. struct sched_param rtparam;
  132. int res;
  133. if (!fThread)
  134. return -1;
  135. memset(&rtparam, 0, sizeof(rtparam));
  136. rtparam.sched_priority = fPriority;
  137. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  138. if ((res = pthread_setschedparam(fThread, SCHED_RR, &rtparam)) != 0) {
  139. jack_error("Cannot use real-time scheduling (FIFO/%d) "
  140. "(%d: %s)", rtparam.sched_priority, res,
  141. strerror(res));
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. int JackPosixThread::AcquireRealTime(int priority)
  147. {
  148. fPriority = priority;
  149. return AcquireRealTime();
  150. }
  151. int JackPosixThread::DropRealTime()
  152. {
  153. struct sched_param rtparam;
  154. int res;
  155. if (!fThread)
  156. return -1;
  157. memset(&rtparam, 0, sizeof(rtparam));
  158. rtparam.sched_priority = 0;
  159. if ((res = pthread_setschedparam(fThread, SCHED_OTHER, &rtparam)) != 0) {
  160. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. pthread_t JackPosixThread::GetThreadID()
  166. {
  167. return fThread;
  168. }
  169. } // end of namespace