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.

288 lines
7.4KB

  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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include "JackPosixThread.h"
  20. #include "JackError.h"
  21. #include "JackTime.h"
  22. #include <string.h> // for memset
  23. namespace Jack
  24. {
  25. void* JackPosixThread::ThreadHandler(void* arg)
  26. {
  27. JackPosixThread* obj = (JackPosixThread*)arg;
  28. JackRunnableInterface* runnable = obj->fRunnable;
  29. int err;
  30. if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
  31. jack_error("pthread_setcanceltype err = %s", strerror(err));
  32. }
  33. set_threaded_log_function();
  34. // Signal creation thread when started with StartSync
  35. jack_log("ThreadHandler: start");
  36. obj->fStatus = kIniting;
  37. // Call Init method
  38. if (!runnable->Init()) {
  39. jack_error("Thread init fails: thread quits");
  40. return 0;
  41. }
  42. obj->fStatus = kRunning;
  43. // If Init succeed, start the thread loop
  44. bool res = true;
  45. while (obj->fStatus == kRunning && res) {
  46. res = runnable->Execute();
  47. }
  48. jack_log("ThreadHandler: exit");
  49. return 0;
  50. }
  51. int JackPosixThread::Start()
  52. {
  53. fStatus = kStarting;
  54. // Check if the thread was correctly started
  55. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  56. fStatus = kIdle;
  57. return -1;
  58. } else {
  59. return 0;
  60. }
  61. }
  62. int JackPosixThread::StartSync()
  63. {
  64. fStatus = kStarting;
  65. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  66. fStatus = kIdle;
  67. return -1;
  68. } else {
  69. int count = 0;
  70. while (fStatus == kStarting && ++count < 1000) {
  71. JackSleep(1000);
  72. }
  73. return (count == 1000) ? -1 : 0;
  74. }
  75. }
  76. int JackPosixThread::StartImp(pthread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
  77. {
  78. pthread_attr_t attributes;
  79. struct sched_param rt_param;
  80. pthread_attr_init(&attributes);
  81. int res;
  82. if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
  83. jack_error("Cannot request joinable thread creation for RT thread res = %d err = %s", res, strerror(errno));
  84. return -1;
  85. }
  86. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  87. jack_error("Cannot set scheduling scope for RT thread res = %d err = %s", res, strerror(errno));
  88. return -1;
  89. }
  90. if (realtime) {
  91. jack_log("Create RT thread");
  92. if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
  93. jack_error("Cannot request explicit scheduling for RT thread res = %d err = %s", res, strerror(errno));
  94. return -1;
  95. }
  96. //if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_FIFO))) {
  97. if ((res = pthread_attr_setschedpolicy(&attributes, SCHED_RR))) {
  98. jack_error("Cannot set RR scheduling class for RT thread res = %d err = %s", res, strerror(errno));
  99. return -1;
  100. }
  101. } else {
  102. jack_log("Create non RT thread");
  103. }
  104. memset(&rt_param, 0, sizeof(rt_param));
  105. rt_param.sched_priority = priority;
  106. if ((res = pthread_attr_setschedparam(&attributes, &rt_param))) {
  107. jack_error("Cannot set scheduling priority for RT thread res = %d err = %s", res, strerror(errno));
  108. return -1;
  109. }
  110. if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
  111. jack_error("Cannot set thread stack size res = %d err = %s", res, strerror(errno));
  112. return -1;
  113. }
  114. if ((res = pthread_create(thread, &attributes, start_routine, arg))) {
  115. jack_error("Cannot create thread res = %d err = %s", res, strerror(errno));
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. int JackPosixThread::Kill()
  121. {
  122. if (fThread != (pthread_t)NULL) { // If thread has been started
  123. jack_log("JackPosixThread::Kill");
  124. void* status;
  125. pthread_cancel(fThread);
  126. pthread_join(fThread, &status);
  127. fStatus = kIdle;
  128. fThread = (pthread_t)NULL;
  129. return 0;
  130. } else {
  131. return -1;
  132. }
  133. }
  134. int JackPosixThread::Stop()
  135. {
  136. if (fThread != (pthread_t)NULL) { // If thread has been started
  137. jack_log("JackPosixThread::Stop");
  138. void* status;
  139. fStatus = kIdle; // Request for the thread to stop
  140. pthread_join(fThread, &status);
  141. fThread = (pthread_t)NULL;
  142. return 0;
  143. } else {
  144. return -1;
  145. }
  146. }
  147. int JackPosixThread::AcquireRealTime()
  148. {
  149. return (fThread != (pthread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  150. }
  151. int JackPosixThread::AcquireRealTime(int priority)
  152. {
  153. fPriority = priority;
  154. return AcquireRealTime();
  155. }
  156. int JackPosixThread::AcquireRealTimeImp(pthread_t thread, int priority)
  157. {
  158. struct sched_param rtparam;
  159. int res;
  160. memset(&rtparam, 0, sizeof(rtparam));
  161. rtparam.sched_priority = priority;
  162. //if ((res = pthread_setschedparam(fThread, SCHED_FIFO, &rtparam)) != 0) {
  163. if ((res = pthread_setschedparam(thread, SCHED_RR, &rtparam)) != 0) {
  164. jack_error("Cannot use real-time scheduling (RR/%d) "
  165. "(%d: %s)", rtparam.sched_priority, res,
  166. strerror(res));
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. int JackPosixThread::DropRealTime()
  172. {
  173. return (fThread != (pthread_t)NULL) ? DropRealTimeImp(fThread) : -1;
  174. }
  175. int JackPosixThread::DropRealTimeImp(pthread_t thread)
  176. {
  177. struct sched_param rtparam;
  178. int res;
  179. memset(&rtparam, 0, sizeof(rtparam));
  180. rtparam.sched_priority = 0;
  181. if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
  182. jack_error("Cannot switch to normal scheduling priority(%s)\n", strerror(errno));
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. pthread_t JackPosixThread::GetThreadID()
  188. {
  189. return fThread;
  190. }
  191. void JackPosixThread::Terminate()
  192. {
  193. jack_log("JackPosixThread::Terminate");
  194. pthread_exit(0);
  195. }
  196. } // end of namespace
  197. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  198. {
  199. int ret;
  200. ret = pthread_key_create(key_ptr, NULL);
  201. if (ret != 0)
  202. {
  203. jack_error("pthread_key_create() failed with error %d errno %s", ret, strerror(errno));
  204. return false;
  205. }
  206. return true;
  207. }
  208. bool jack_tls_free_key(jack_tls_key key)
  209. {
  210. int ret;
  211. ret = pthread_key_delete(key);
  212. if (ret != 0)
  213. {
  214. jack_error("pthread_key_delete() failed with error %d errno %s", ret, strerror(errno));
  215. return false;
  216. }
  217. return true;
  218. }
  219. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  220. {
  221. int ret;
  222. ret = pthread_setspecific(key, (const void *)data_ptr);
  223. if (ret != 0)
  224. {
  225. jack_error("pthread_setspecific() failed with error %d errno %s", ret, strerror(errno));
  226. return false;
  227. }
  228. return true;
  229. }
  230. void *jack_tls_get(jack_tls_key key)
  231. {
  232. return pthread_getspecific(key);
  233. }