jack1 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.

345 lines
7.7KB

  1. /*
  2. Copyright (C) 2004 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. Thread creation function including workarounds for real-time scheduling
  15. behaviour on different glibc versions.
  16. */
  17. #include <config.h>
  18. #include <jack/jack.h>
  19. #include <jack/thread.h>
  20. #include "internal.h"
  21. #include <pthread.h>
  22. #include <sched.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #if defined(__FreeBSD__)
  27. #include <sys/types.h>
  28. #include <sys/rtprio.h>
  29. #include <unistd.h>
  30. #endif
  31. #include "local.h"
  32. #ifdef JACK_USE_MACH_THREADS
  33. #include <sysdeps/pThreadUtilities.h>
  34. #endif
  35. jack_thread_creator_t jack_thread_creator = pthread_create;
  36. void
  37. jack_set_thread_creator (jack_thread_creator_t jtc)
  38. {
  39. jack_thread_creator = jtc;
  40. }
  41. static inline void
  42. log_result (char *msg, int res)
  43. {
  44. char outbuf[500];
  45. snprintf (outbuf, sizeof(outbuf),
  46. "jack_client_create_thread: error %d %s: %s",
  47. res, msg, strerror (res));
  48. jack_error (outbuf);
  49. }
  50. static void
  51. maybe_get_capabilities (jack_client_t* client)
  52. {
  53. #ifdef USE_CAPABILITIES
  54. if (client != 0) {
  55. jack_request_t req;
  56. if (client->engine->has_capabilities != 0) {
  57. /* we need to ask the engine for realtime capabilities
  58. before trying to run the thread work function
  59. */
  60. VALGRIND_MEMSET (&req, 0, sizeof(req));
  61. req.type = SetClientCapabilities;
  62. req.x.cap_pid = getpid ();
  63. jack_client_deliver_request (client, &req);
  64. if (req.status) {
  65. /* what to do? engine is running realtime, it
  66. is using capabilities and has them
  67. (otherwise we would not get an error
  68. return) but for some reason it could not
  69. give the client the required capabilities.
  70. for now, allow the client to run, albeit
  71. non-realtime.
  72. */
  73. jack_error ("could not receive realtime capabilities, "
  74. "client will run non-realtime");
  75. }
  76. }
  77. }
  78. #endif /* USE_CAPABILITIES */
  79. }
  80. static void
  81. jack_thread_touch_stack ()
  82. {
  83. char buf[JACK_THREAD_STACK_TOUCH];
  84. int i;
  85. volatile char *buf_ptr = buf;
  86. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++)
  87. buf_ptr[i] = (char)(i & 0xff);
  88. }
  89. typedef void (*stack_touch_t)();
  90. static volatile stack_touch_t ptr_jack_thread_touch_stack = jack_thread_touch_stack;
  91. static void*
  92. jack_thread_proxy (void* varg)
  93. {
  94. jack_thread_arg_t* arg = (jack_thread_arg_t*)varg;
  95. void* (*work)(void*);
  96. void* warg;
  97. jack_client_t* client = arg->client;
  98. if (arg->realtime) {
  99. ptr_jack_thread_touch_stack ();
  100. maybe_get_capabilities (client);
  101. jack_acquire_real_time_scheduling (pthread_self (), arg->priority);
  102. }
  103. warg = arg->arg;
  104. work = arg->work_function;
  105. free (arg);
  106. return work (warg);
  107. }
  108. int
  109. jack_client_create_thread (jack_client_t* client,
  110. pthread_t* thread,
  111. int priority,
  112. int realtime,
  113. void*(*start_routine)(void*),
  114. void* arg)
  115. {
  116. #ifndef JACK_USE_MACH_THREADS
  117. pthread_attr_t attr;
  118. jack_thread_arg_t* thread_args;
  119. #endif /* !JACK_USE_MACH_THREADS */
  120. int result = 0;
  121. if (!realtime) {
  122. result = jack_thread_creator (thread, 0, start_routine, arg);
  123. if (result) {
  124. log_result ("creating thread with default parameters",
  125. result);
  126. }
  127. return result;
  128. }
  129. /* realtime thread. this disgusting mess is a reflection of
  130. * the 2nd-class nature of RT programming under POSIX in
  131. * general and Linux in particular.
  132. */
  133. #ifndef JACK_USE_MACH_THREADS
  134. pthread_attr_init (&attr);
  135. result = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
  136. if (result) {
  137. log_result ("requesting explicit scheduling", result);
  138. return result;
  139. }
  140. result = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
  141. if (result) {
  142. log_result ("requesting joinable thread creation", result);
  143. return result;
  144. }
  145. #ifdef __OpenBSD__
  146. result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
  147. #else
  148. result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
  149. #endif
  150. if (result) {
  151. log_result ("requesting system scheduling scope", result);
  152. return result;
  153. }
  154. result = pthread_attr_setstacksize (&attr, THREAD_STACK);
  155. if (result) {
  156. log_result ("setting thread stack size", result);
  157. return result;
  158. }
  159. if ((thread_args = (jack_thread_arg_t*)malloc (sizeof(jack_thread_arg_t))) == NULL) {
  160. return -1;
  161. }
  162. thread_args->client = client;
  163. thread_args->work_function = start_routine;
  164. thread_args->arg = arg;
  165. thread_args->realtime = 1;
  166. thread_args->priority = priority;
  167. result = jack_thread_creator (thread, &attr, jack_thread_proxy, thread_args);
  168. if (result) {
  169. log_result ("creating realtime thread", result);
  170. return result;
  171. }
  172. #else /* JACK_USE_MACH_THREADS */
  173. result = jack_thread_creator (thread, 0, start_routine, arg);
  174. if (result) {
  175. log_result ("creating realtime thread", result);
  176. return result;
  177. }
  178. /* time constraint thread */
  179. setThreadToPriority (*thread, 96, TRUE, 10000000);
  180. #endif /* JACK_USE_MACH_THREADS */
  181. return 0;
  182. }
  183. int
  184. jack_client_real_time_priority (jack_client_t* client)
  185. {
  186. if (!client->engine->real_time) {
  187. return -1;
  188. }
  189. return client->engine->client_priority;
  190. }
  191. int
  192. jack_client_max_real_time_priority (jack_client_t* client)
  193. {
  194. if (!client->engine->real_time) {
  195. return -1;
  196. }
  197. return client->engine->max_client_priority;
  198. }
  199. #if JACK_USE_MACH_THREADS
  200. int
  201. jack_drop_real_time_scheduling (pthread_t thread)
  202. {
  203. setThreadToPriority (thread, 31, FALSE, 10000000);
  204. return 0;
  205. }
  206. int
  207. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  208. //priority is unused
  209. {
  210. setThreadToPriority (thread, 96, TRUE, 10000000);
  211. return 0;
  212. }
  213. #else /* !JACK_USE_MACH_THREADS */
  214. static int
  215. jack_process_already_has_real_time_scheduling (int priority)
  216. {
  217. #if defined(__FreeBSD__)
  218. int res;
  219. struct rtprio rtp;
  220. res = rtprio(RTP_LOOKUP, getpid(), &rtp);
  221. if (res == 0 && rtp.type == RTP_PRIO_REALTIME && rtp.prio <= priority) {
  222. jack_info("process already runs at sufficient realtime priority %u (<=%d)",
  223. (unsigned)rtp.prio,
  224. priority);
  225. return 1; // process priority is sufficient
  226. }
  227. #endif
  228. return 0; // no or don't know
  229. }
  230. int
  231. jack_drop_real_time_scheduling (pthread_t thread)
  232. {
  233. struct sched_param rtparam;
  234. int x, policy;
  235. if ((x = pthread_getschedparam (thread, &policy, &rtparam)) != 0) {
  236. jack_error ("cannot read thread scheduling priority(%s)\n",
  237. strerror (errno));
  238. return -1;
  239. }
  240. if (policy == SCHED_OTHER) {
  241. return 0; // already
  242. }
  243. memset (&rtparam, 0, sizeof(rtparam));
  244. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  245. jack_error ("cannot switch to normal scheduling priority(%s)\n",
  246. strerror (errno));
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. int
  252. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  253. {
  254. struct sched_param rtparam;
  255. int x;
  256. if (jack_process_already_has_real_time_scheduling (priority) != 0) {
  257. return 0;
  258. }
  259. memset (&rtparam, 0, sizeof(rtparam));
  260. rtparam.sched_priority = priority;
  261. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  262. jack_error ("cannot use real-time scheduling (FIFO at priority %d) "
  263. "[for thread %d, from thread %d] (%d: %s)",
  264. rtparam.sched_priority,
  265. thread, pthread_self (),
  266. x, strerror (x));
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. #endif /* JACK_USE_MACH_THREADS */