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.

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