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.

232 lines
6.3KB

  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. fRunning = true;
  47. // Check if the thread was correctly started
  48. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  49. fRunning = false;
  50. return -1;
  51. } else {
  52. return 0;
  53. }
  54. }
  55. int JackPosixThread::StartImp(pthread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
  56. {
  57. int res;
  58. if (realtime) {
  59. JackLog("Create RT thread\n");
  60. /* Get the client thread to run as an RT-FIFO
  61. scheduled thread of appropriate priority.
  62. */
  63. pthread_attr_t attributes;
  64. struct sched_param rt_param;
  65. pthread_attr_init(&attributes);
  66. if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
  67. jack_error("Cannot request explicit scheduling for RT thread %d %s", res, strerror(errno));
  68. return -1;
  69. }
  70. if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
  71. jack_error("Cannot request joinable thread creation for RT thread %d %s", res, strerror(errno));
  72. return -1;
  73. }
  74. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  75. jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
  76. return -1;
  77. }
  78. //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
  79. if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
  80. jack_error("Cannot set FIFO scheduling class for RT thread %d %s", res, strerror(errno));
  81. return -1;
  82. }
  83. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  84. jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
  85. return -1;
  86. }
  87. memset(&rt_param, 0, sizeof(rt_param));
  88. rt_param.sched_priority = priority;
  89. if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
  90. jack_error("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
  91. return -1;
  92. }
  93. if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
  94. jack_error("setting thread stack size%d %s", res, strerror(errno));
  95. return -1;
  96. }
  97. if ((res = pthread_create(thread, &attributes, start_routine, arg))) {
  98. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  99. return -1;
  100. }
  101. return 0;
  102. } else {
  103. JackLog("Create non RT thread\n");
  104. if ((res = pthread_create(thread, 0, start_routine, arg))) {
  105. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. }
  111. int JackPosixThread::StartSync()
  112. {
  113. jack_error("Not implemented yet");
  114. return -1;
  115. }
  116. int JackPosixThread::Kill()
  117. {
  118. if (fThread) { // If thread has been started
  119. JackLog("JackPosixThread::Kill\n");
  120. void* status;
  121. pthread_cancel(fThread);
  122. pthread_join(fThread, &status);
  123. fRunning = false;
  124. fThread = (pthread_t)NULL;
  125. return 0;
  126. } else {
  127. return -1;
  128. }
  129. }
  130. int JackPosixThread::Stop()
  131. {
  132. if (fThread) { // If thread has been started
  133. JackLog("JackPosixThread::Stop\n");
  134. void* status;
  135. fRunning = false; // Request for the thread to stop
  136. pthread_join(fThread, &status);
  137. fThread = (pthread_t)NULL;
  138. return 0;
  139. } else {
  140. return -1;
  141. }
  142. }
  143. int JackPosixThread::AcquireRealTime()
  144. {
  145. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  146. }
  147. int JackPosixThread::AcquireRealTime(int priority)
  148. {
  149. fPriority = priority;
  150. return AcquireRealTime();
  151. }
  152. int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
  153. {
  154. struct sched_param rtparam;
  155. int res;
  156. memset(&rtparam, 0, sizeof(rtparam));
  157. rtparam.sched_priority = priority;
  158. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  159. if ((res = pthread_setschedparam(thread, SCHED_RR, &rtparam)) != 0) {
  160. jack_error("Cannot use real-time scheduling (FIFO/%d) "
  161. "(%d: %s)", rtparam.sched_priority, res,
  162. strerror(res));
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. int JackPosixThread::DropRealTime()
  168. {
  169. return (fThread) ? DropRealTimeImp(fThread) : -1;
  170. }
  171. int JackPosixThread::DropRealTimeImp(pthread_t thread)
  172. {
  173. struct sched_param rtparam;
  174. int res;
  175. memset(&rtparam, 0, sizeof(rtparam));
  176. rtparam.sched_priority = 0;
  177. if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
  178. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. pthread_t JackPosixThread::GetThreadID()
  184. {
  185. return fThread;
  186. }
  187. } // end of namespace