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.

366 lines
8.9KB

  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 "JackGlobals.h"
  20. #include <string.h> // for memset
  21. #include <unistd.h> // for _POSIX_PRIORITY_SCHEDULING check
  22. //#define JACK_SCHED_POLICY SCHED_RR
  23. #define JACK_SCHED_POLICY SCHED_FIFO
  24. namespace Jack
  25. {
  26. void* JackPosixThread::ThreadHandler(void* arg)
  27. {
  28. JackPosixThread* obj = (JackPosixThread*)arg;
  29. JackRunnableInterface* runnable = obj->fRunnable;
  30. int err;
  31. if ((err = pthread_setcanceltype(obj->fCancellation, NULL)) != 0) {
  32. jack_error("pthread_setcanceltype err = %s", strerror(err));
  33. }
  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. pthread_exit(0);
  50. return 0; // never reached
  51. }
  52. int JackPosixThread::Start()
  53. {
  54. fStatus = kStarting;
  55. // Check if the thread was correctly started
  56. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  57. fStatus = kIdle;
  58. return -1;
  59. } else {
  60. return 0;
  61. }
  62. }
  63. int JackPosixThread::StartSync()
  64. {
  65. fStatus = kStarting;
  66. if (StartImp(&fThread, fPriority, fRealTime, ThreadHandler, this) < 0) {
  67. fStatus = kIdle;
  68. return -1;
  69. } else {
  70. int count = 0;
  71. while (fStatus == kStarting && ++count < 1000) {
  72. JackSleep(1000);
  73. }
  74. return (count == 1000) ? -1 : 0;
  75. }
  76. }
  77. int JackPosixThread::StartImp(jack_native_thread_t* thread, int priority, int realtime, void*(*start_routine)(void*), void* arg)
  78. {
  79. pthread_attr_t attributes;
  80. struct sched_param rt_param;
  81. pthread_attr_init(&attributes);
  82. int res;
  83. if ((res = pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_JOINABLE))) {
  84. jack_error("Cannot request joinable thread creation for thread res = %d", res);
  85. return -1;
  86. }
  87. if ((res = pthread_attr_setscope(&attributes, PTHREAD_SCOPE_SYSTEM))) {
  88. jack_error("Cannot set scheduling scope for thread res = %d", res);
  89. return -1;
  90. }
  91. if (realtime) {
  92. jack_log("Create RT thread");
  93. if ((res = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED))) {
  94. jack_error("Cannot request explicit scheduling for RT thread res = %d", res);
  95. return -1;
  96. }
  97. if ((res = pthread_attr_setschedpolicy(&attributes, JACK_SCHED_POLICY))) {
  98. jack_error("Cannot set RR scheduling class for RT thread res = %d", res);
  99. return -1;
  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", res);
  105. return -1;
  106. }
  107. } else {
  108. jack_log("Create non RT thread");
  109. }
  110. if ((res = pthread_attr_setstacksize(&attributes, THREAD_STACK))) {
  111. jack_error("Cannot set thread stack size res = %d", res);
  112. return -1;
  113. }
  114. if ((res = JackGlobals::fJackThreadCreator(thread, &attributes, start_routine, arg))) {
  115. jack_error("Cannot create thread res = %d", res);
  116. return -1;
  117. }
  118. pthread_attr_destroy(&attributes);
  119. return 0;
  120. }
  121. int JackPosixThread::Kill()
  122. {
  123. if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
  124. jack_log("JackPosixThread::Kill");
  125. void* status;
  126. pthread_cancel(fThread);
  127. pthread_join(fThread, &status);
  128. fStatus = kIdle;
  129. fThread = (jack_native_thread_t)NULL;
  130. return 0;
  131. } else {
  132. return -1;
  133. }
  134. }
  135. int JackPosixThread::Stop()
  136. {
  137. if (fThread != (jack_native_thread_t)NULL) { // If thread has been started
  138. jack_log("JackPosixThread::Stop");
  139. void* status;
  140. fStatus = kIdle; // Request for the thread to stop
  141. pthread_join(fThread, &status);
  142. fThread = (jack_native_thread_t)NULL;
  143. return 0;
  144. } else {
  145. return -1;
  146. }
  147. }
  148. int JackPosixThread::KillImp(jack_native_thread_t thread)
  149. {
  150. if (thread != (jack_native_thread_t)NULL) { // If thread has been started
  151. jack_log("JackPosixThread::Kill");
  152. void* status;
  153. pthread_cancel(thread);
  154. pthread_join(thread, &status);
  155. return 0;
  156. } else {
  157. return -1;
  158. }
  159. }
  160. int JackPosixThread::StopImp(jack_native_thread_t thread)
  161. {
  162. if (thread != (jack_native_thread_t)NULL) { // If thread has been started
  163. jack_log("JackPosixThread::Stop");
  164. void* status;
  165. pthread_join(thread, &status);
  166. return 0;
  167. } else {
  168. return -1;
  169. }
  170. }
  171. int JackPosixThread::AcquireRealTime()
  172. {
  173. return (fThread != (jack_native_thread_t)NULL) ? AcquireRealTimeImp(fThread, fPriority) : -1;
  174. }
  175. int JackPosixThread::AcquireSelfRealTime()
  176. {
  177. return AcquireRealTimeImp(pthread_self(), fPriority);
  178. }
  179. int JackPosixThread::AcquireRealTime(int priority)
  180. {
  181. fPriority = priority;
  182. return AcquireRealTime();
  183. }
  184. int JackPosixThread::AcquireSelfRealTime(int priority)
  185. {
  186. fPriority = priority;
  187. return AcquireSelfRealTime();
  188. }
  189. int JackPosixThread::AcquireRealTimeImp(jack_native_thread_t thread, int priority)
  190. {
  191. struct sched_param rtparam;
  192. int res;
  193. memset(&rtparam, 0, sizeof(rtparam));
  194. rtparam.sched_priority = priority;
  195. if ((res = pthread_setschedparam(thread, JACK_SCHED_POLICY, &rtparam)) != 0) {
  196. jack_error("Cannot use real-time scheduling (RR/%d)"
  197. "(%d: %s)", rtparam.sched_priority, res,
  198. strerror(res));
  199. return -1;
  200. }
  201. return 0;
  202. }
  203. int JackPosixThread::DropRealTime()
  204. {
  205. return (fThread != (jack_native_thread_t)NULL) ? DropRealTimeImp(fThread) : -1;
  206. }
  207. int JackPosixThread::DropSelfRealTime()
  208. {
  209. return DropRealTimeImp(pthread_self());
  210. }
  211. int JackPosixThread::DropRealTimeImp(jack_native_thread_t thread)
  212. {
  213. struct sched_param rtparam;
  214. int res;
  215. memset(&rtparam, 0, sizeof(rtparam));
  216. rtparam.sched_priority = 0;
  217. if ((res = pthread_setschedparam(thread, SCHED_OTHER, &rtparam)) != 0) {
  218. jack_error("Cannot switch to normal scheduling priority(%s)", strerror(errno));
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. jack_native_thread_t JackPosixThread::GetThreadID()
  224. {
  225. return fThread;
  226. }
  227. bool JackPosixThread::IsThread()
  228. {
  229. return pthread_self() == fThread;
  230. }
  231. void JackPosixThread::Terminate()
  232. {
  233. jack_log("JackPosixThread::Terminate");
  234. pthread_exit(0);
  235. }
  236. SERVER_EXPORT void ThreadExit()
  237. {
  238. jack_log("ThreadExit");
  239. pthread_exit(0);
  240. }
  241. } // end of namespace
  242. bool jack_get_thread_realtime_priority_range(int * min_ptr, int * max_ptr)
  243. {
  244. #if defined(_POSIX_PRIORITY_SCHEDULING) && !defined(__APPLE__)
  245. int min, max;
  246. min = sched_get_priority_min(JACK_SCHED_POLICY);
  247. if (min == -1)
  248. {
  249. jack_error("sched_get_priority_min() failed.");
  250. return false;
  251. }
  252. max = sched_get_priority_max(JACK_SCHED_POLICY);
  253. if (max == -1)
  254. {
  255. jack_error("sched_get_priority_max() failed.");
  256. return false;
  257. }
  258. *min_ptr = min;
  259. *max_ptr = max;
  260. return true;
  261. #else
  262. return false;
  263. #endif
  264. }
  265. bool jack_tls_allocate_key(jack_tls_key *key_ptr)
  266. {
  267. int ret;
  268. ret = pthread_key_create(key_ptr, NULL);
  269. if (ret != 0)
  270. {
  271. jack_error("pthread_key_create() failed with error %d", ret);
  272. return false;
  273. }
  274. return true;
  275. }
  276. bool jack_tls_free_key(jack_tls_key key)
  277. {
  278. int ret;
  279. ret = pthread_key_delete(key);
  280. if (ret != 0)
  281. {
  282. jack_error("pthread_key_delete() failed with error %d", ret);
  283. return false;
  284. }
  285. return true;
  286. }
  287. bool jack_tls_set(jack_tls_key key, void *data_ptr)
  288. {
  289. int ret;
  290. ret = pthread_setspecific(key, (const void *)data_ptr);
  291. if (ret != 0)
  292. {
  293. jack_error("pthread_setspecific() failed with error %d", ret);
  294. return false;
  295. }
  296. return true;
  297. }
  298. void *jack_tls_get(jack_tls_key key)
  299. {
  300. return pthread_getspecific(key);
  301. }