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.

210 lines
5.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 "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. return 0;
  110. } else {
  111. return -1;
  112. }
  113. }
  114. int JackPosixThread::Stop()
  115. {
  116. if (fThread) { // If thread has been started
  117. JackLog("JackPosixThread::Stop\n");
  118. void* status;
  119. fRunning = false; // Request for the thread to stop
  120. pthread_join(fThread, &status);
  121. return 0;
  122. } else {
  123. return -1;
  124. }
  125. }
  126. int JackPosixThread::AcquireRealTime()
  127. {
  128. struct sched_param rtparam;
  129. int res;
  130. if (!fThread)
  131. return -1;
  132. memset(&rtparam, 0, sizeof(rtparam));
  133. rtparam.sched_priority = fPriority;
  134. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  135. if ((res = pthread_setschedparam(fThread, SCHED_RR, &rtparam)) != 0) {
  136. jack_error("Cannot use real-time scheduling (FIFO/%d) "
  137. "(%d: %s)", rtparam.sched_priority, res,
  138. strerror(res));
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. int JackPosixThread::AcquireRealTime(int priority)
  144. {
  145. fPriority = priority;
  146. return AcquireRealTime();
  147. }
  148. int JackPosixThread::DropRealTime()
  149. {
  150. struct sched_param rtparam;
  151. int res;
  152. if (!fThread)
  153. return -1;
  154. memset(&rtparam, 0, sizeof(rtparam));
  155. rtparam.sched_priority = 0;
  156. if ((res = pthread_setschedparam(fThread, SCHED_OTHER, &rtparam)) != 0) {
  157. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. pthread_t JackPosixThread::GetThreadID()
  163. {
  164. return fThread;
  165. }
  166. } // end of namespace