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.2KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. jack_log("ThreadHandler: start");
  35. // If Init succeed, start the thread loop
  36. bool res = true;
  37. while (obj->fRunning && res) {
  38. res = runnable->Execute();
  39. }
  40. jack_log("ThreadHandler: exit");
  41. return 0;
  42. }
  43. int JackPosixThread::Start()
  44. {
  45. fRunning = true;
  46. // Check if the thread was correctly started
  47. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  48. fRunning = false;
  49. return -1;
  50. } else {
  51. return 0;
  52. }
  53. }
  54. int JackPosixThread::StartImp(pthread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
  55. {
  56. int res;
  57. if (realtime) {
  58. jack_log("Create RT thread");
  59. /* Get the client thread to run as an RT-FIFO
  60. scheduled thread of appropriate priority.
  61. */
  62. pthread_attr_t attributes;
  63. struct sched_param rt_param;
  64. pthread_attr_init(&attributes);
  65. if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
  66. jack_error("Cannot request explicit scheduling for RT thread %d %s", res, strerror(errno));
  67. return -1;
  68. }
  69. if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
  70. jack_error("Cannot request joinable thread creation 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. //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
  78. if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
  79. jack_error("Cannot set RR scheduling class for RT thread %d %s", res, strerror(errno));
  80. return -1;
  81. }
  82. memset(&rt_param, 0, sizeof(rt_param));
  83. rt_param.sched_priority = priority;
  84. if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
  85. jack_error("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
  86. return -1;
  87. }
  88. if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
  89. jack_error("setting thread stack size%d %s", res, strerror(errno));
  90. return -1;
  91. }
  92. if ((res = pthread_create(thread, &attributes, start_routine, arg))) {
  93. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  94. return -1;
  95. }
  96. return 0;
  97. } else {
  98. jack_log("Create non RT thread");
  99. if ((res = pthread_create(thread, 0, start_routine, arg))) {
  100. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. }
  106. int JackPosixThread::StartSync()
  107. {
  108. jack_error("Not implemented yet");
  109. return -1;
  110. }
  111. int JackPosixThread::Kill()
  112. {
  113. if (fThread) { // If thread has been started
  114. jack_log("JackPosixThread::Kill");
  115. void* status;
  116. pthread_cancel(fThread);
  117. pthread_join(fThread, &status);
  118. fRunning = false;
  119. fThread = (pthread_t)NULL;
  120. return 0;
  121. } else {
  122. return -1;
  123. }
  124. }
  125. int JackPosixThread::Stop()
  126. {
  127. if (fThread) { // If thread has been started
  128. jack_log("JackPosixThread::Stop");
  129. void* status;
  130. fRunning = false; // Request for the thread to stop
  131. pthread_join(fThread, &status);
  132. fThread = (pthread_t)NULL;
  133. return 0;
  134. } else {
  135. return -1;
  136. }
  137. }
  138. int JackPosixThread::AcquireRealTime()
  139. {
  140. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  141. }
  142. int JackPosixThread::AcquireRealTime(int priority)
  143. {
  144. fPriority = priority;
  145. return AcquireRealTime();
  146. }
  147. int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
  148. {
  149. struct sched_param rtparam;
  150. int res;
  151. memset(&rtparam, 0, sizeof(rtparam));
  152. rtparam.sched_priority = priority;
  153. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  154. if ((res = pthread_setschedparam(thread, SCHED_RR, &rtparam)) != 0) {
  155. jack_error("Cannot use real-time scheduling (RR/%d) "
  156. "(%d: %s)", rtparam.sched_priority, res,
  157. strerror(res));
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. int JackPosixThread::DropRealTime()
  163. {
  164. return (fThread) ? DropRealTimeImp(fThread) : -1;
  165. }
  166. int JackPosixThread::DropRealTimeImp(pthread_t thread)
  167. {
  168. struct sched_param rtparam;
  169. int res;
  170. memset(&rtparam, 0, sizeof(rtparam));
  171. rtparam.sched_priority = 0;
  172. if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
  173. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. pthread_t JackPosixThread::GetThreadID()
  179. {
  180. return fThread;
  181. }
  182. void JackPosixThread::Terminate()
  183. {
  184. jack_log("JackPosixThread::Terminate");
  185. pthread_exit(0);
  186. }
  187. } // end of namespace