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.

222 lines
6.0KB

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