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.

237 lines
6.3KB

  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 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. }
  40. JackLog("ThreadHandler: exit\n");
  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. JackLog("Create RT thread\n");
  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 FIFO scheduling class for RT thread %d %s", res, strerror(errno));
  80. return -1;
  81. }
  82. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  83. jack_error("Cannot set scheduling scope for RT thread %d %s", res, strerror(errno));
  84. return -1;
  85. }
  86. memset(&rt_param, 0, sizeof(rt_param));
  87. rt_param.sched_priority = priority;
  88. if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
  89. jack_error("Cannot set scheduling priority for RT thread %d %s", res, strerror(errno));
  90. return -1;
  91. }
  92. if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
  93. jack_error("setting thread stack size%d %s", res, strerror(errno));
  94. return -1;
  95. }
  96. if ((res = pthread_create(thread, &attributes, start_routine, arg))) {
  97. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  98. return -1;
  99. }
  100. return 0;
  101. } else {
  102. JackLog("Create non RT thread\n");
  103. if ((res = pthread_create(thread, 0, start_routine, arg))) {
  104. jack_error("Cannot set create thread %d %s", res, strerror(errno));
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. }
  110. int JackPosixThread::StartSync()
  111. {
  112. jack_error("Not implemented yet");
  113. return -1;
  114. }
  115. int JackPosixThread::Kill()
  116. {
  117. if (fThread) { // If thread has been started
  118. JackLog("JackPosixThread::Kill\n");
  119. void* status;
  120. pthread_cancel(fThread);
  121. pthread_join(fThread, &status);
  122. fRunning = false;
  123. fThread = (pthread_t)NULL;
  124. return 0;
  125. } else {
  126. return -1;
  127. }
  128. }
  129. int JackPosixThread::Stop()
  130. {
  131. if (fThread) { // If thread has been started
  132. JackLog("JackPosixThread::Stop\n");
  133. void* status;
  134. fRunning = false; // Request for the thread to stop
  135. pthread_join(fThread, &status);
  136. fThread = (pthread_t)NULL;
  137. return 0;
  138. } else {
  139. return -1;
  140. }
  141. }
  142. int JackPosixThread::AcquireRealTime()
  143. {
  144. return (fThread) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  145. }
  146. int JackPosixThread::AcquireRealTime(int priority)
  147. {
  148. fPriority = priority;
  149. return AcquireRealTime();
  150. }
  151. int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
  152. {
  153. struct sched_param rtparam;
  154. int res;
  155. memset(&rtparam, 0, sizeof(rtparam));
  156. rtparam.sched_priority = priority;
  157. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  158. if ((res = pthread_setschedparam(thread, SCHED_RR, &rtparam)) != 0) {
  159. jack_error("Cannot use real-time scheduling (FIFO/%d) "
  160. "(%d: %s)", rtparam.sched_priority, res,
  161. strerror(res));
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. int JackPosixThread::DropRealTime()
  167. {
  168. return (fThread) ? DropRealTimeImp(fThread) : -1;
  169. }
  170. int JackPosixThread::DropRealTimeImp(pthread_t thread)
  171. {
  172. struct sched_param rtparam;
  173. int res;
  174. memset(&rtparam, 0, sizeof(rtparam));
  175. rtparam.sched_priority = 0;
  176. if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
  177. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. pthread_t JackPosixThread::GetThreadID()
  183. {
  184. return fThread;
  185. }
  186. void JackPosixThread::Terminate()
  187. {
  188. JackLog("JackPosixThread::Terminate\n");
  189. pthread_exit(0);
  190. }
  191. } // end of namespace