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.

310 lines
6.9KB

  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. #include "local.h"
  27. #ifdef JACK_USE_MACH_THREADS
  28. #include <sysdeps/pThreadUtilities.h>
  29. #endif
  30. jack_thread_creator_t jack_thread_creator = pthread_create;
  31. void
  32. jack_set_thread_creator (jack_thread_creator_t jtc)
  33. {
  34. jack_thread_creator = jtc;
  35. }
  36. static inline void
  37. log_result (char *msg, int res)
  38. {
  39. char outbuf[500];
  40. snprintf (outbuf, sizeof(outbuf),
  41. "jack_client_create_thread: error %d %s: %s",
  42. res, msg, strerror (res));
  43. jack_error (outbuf);
  44. }
  45. static void
  46. maybe_get_capabilities (jack_client_t* client)
  47. {
  48. #ifdef USE_CAPABILITIES
  49. if (client != 0) {
  50. jack_request_t req;
  51. if (client->engine->has_capabilities != 0) {
  52. /* we need to ask the engine for realtime capabilities
  53. before trying to run the thread work function
  54. */
  55. VALGRIND_MEMSET (&req, 0, sizeof(req));
  56. req.type = SetClientCapabilities;
  57. req.x.cap_pid = getpid ();
  58. jack_client_deliver_request (client, &req);
  59. if (req.status) {
  60. /* what to do? engine is running realtime, it
  61. is using capabilities and has them
  62. (otherwise we would not get an error
  63. return) but for some reason it could not
  64. give the client the required capabilities.
  65. for now, allow the client to run, albeit
  66. non-realtime.
  67. */
  68. jack_error ("could not receive realtime capabilities, "
  69. "client will run non-realtime");
  70. }
  71. }
  72. }
  73. #endif /* USE_CAPABILITIES */
  74. }
  75. static void
  76. jack_thread_touch_stack ()
  77. {
  78. char buf[JACK_THREAD_STACK_TOUCH];
  79. int i;
  80. volatile char *buf_ptr = buf;
  81. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++)
  82. buf_ptr[i] = (char)(i & 0xff);
  83. }
  84. typedef void (*stack_touch_t)();
  85. static volatile stack_touch_t ptr_jack_thread_touch_stack = jack_thread_touch_stack;
  86. static void*
  87. jack_thread_proxy (void* varg)
  88. {
  89. jack_thread_arg_t* arg = (jack_thread_arg_t*)varg;
  90. void* (*work)(void*);
  91. void* warg;
  92. jack_client_t* client = arg->client;
  93. if (arg->realtime) {
  94. ptr_jack_thread_touch_stack ();
  95. maybe_get_capabilities (client);
  96. jack_acquire_real_time_scheduling (pthread_self (), arg->priority);
  97. }
  98. warg = arg->arg;
  99. work = arg->work_function;
  100. free (arg);
  101. return work (warg);
  102. }
  103. int
  104. jack_client_create_thread (jack_client_t* client,
  105. pthread_t* thread,
  106. int priority,
  107. int realtime,
  108. void*(*start_routine)(void*),
  109. void* arg)
  110. {
  111. #ifndef JACK_USE_MACH_THREADS
  112. pthread_attr_t attr;
  113. jack_thread_arg_t* thread_args;
  114. #endif /* !JACK_USE_MACH_THREADS */
  115. int result = 0;
  116. if (!realtime) {
  117. result = jack_thread_creator (thread, 0, start_routine, arg);
  118. if (result) {
  119. log_result ("creating thread with default parameters",
  120. result);
  121. }
  122. return result;
  123. }
  124. /* realtime thread. this disgusting mess is a reflection of
  125. * the 2nd-class nature of RT programming under POSIX in
  126. * general and Linux in particular.
  127. */
  128. #ifndef JACK_USE_MACH_THREADS
  129. pthread_attr_init (&attr);
  130. result = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
  131. if (result) {
  132. log_result ("requesting explicit scheduling", result);
  133. return result;
  134. }
  135. result = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
  136. if (result) {
  137. log_result ("requesting joinable thread creation", result);
  138. return result;
  139. }
  140. #ifdef __OpenBSD__
  141. result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
  142. #else
  143. result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
  144. #endif
  145. if (result) {
  146. log_result ("requesting system scheduling scope", result);
  147. return result;
  148. }
  149. result = pthread_attr_setstacksize (&attr, THREAD_STACK);
  150. if (result) {
  151. log_result ("setting thread stack size", result);
  152. return result;
  153. }
  154. if ((thread_args = (jack_thread_arg_t*)malloc (sizeof(jack_thread_arg_t))) == NULL) {
  155. return -1;
  156. }
  157. thread_args->client = client;
  158. thread_args->work_function = start_routine;
  159. thread_args->arg = arg;
  160. thread_args->realtime = 1;
  161. thread_args->priority = priority;
  162. result = jack_thread_creator (thread, &attr, jack_thread_proxy, thread_args);
  163. if (result) {
  164. log_result ("creating realtime thread", result);
  165. return result;
  166. }
  167. #else /* JACK_USE_MACH_THREADS */
  168. result = jack_thread_creator (thread, 0, start_routine, arg);
  169. if (result) {
  170. log_result ("creating realtime thread", result);
  171. return result;
  172. }
  173. /* time constraint thread */
  174. setThreadToPriority (*thread, 96, TRUE, 10000000);
  175. #endif /* JACK_USE_MACH_THREADS */
  176. return 0;
  177. }
  178. int
  179. jack_client_real_time_priority (jack_client_t* client)
  180. {
  181. if (!client->engine->real_time) {
  182. return -1;
  183. }
  184. return client->engine->client_priority;
  185. }
  186. int
  187. jack_client_max_real_time_priority (jack_client_t* client)
  188. {
  189. if (!client->engine->real_time) {
  190. return -1;
  191. }
  192. return client->engine->max_client_priority;
  193. }
  194. #if JACK_USE_MACH_THREADS
  195. int
  196. jack_drop_real_time_scheduling (pthread_t thread)
  197. {
  198. setThreadToPriority (thread, 31, FALSE, 10000000);
  199. return 0;
  200. }
  201. int
  202. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  203. //priority is unused
  204. {
  205. setThreadToPriority (thread, 96, TRUE, 10000000);
  206. return 0;
  207. }
  208. #else /* !JACK_USE_MACH_THREADS */
  209. int
  210. jack_drop_real_time_scheduling (pthread_t thread)
  211. {
  212. struct sched_param rtparam;
  213. int x;
  214. memset (&rtparam, 0, sizeof(rtparam));
  215. rtparam.sched_priority = 0;
  216. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  217. jack_error ("cannot switch to normal scheduling priority(%s)\n",
  218. strerror (errno));
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. int
  224. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  225. {
  226. struct sched_param rtparam;
  227. int x;
  228. memset (&rtparam, 0, sizeof(rtparam));
  229. rtparam.sched_priority = priority;
  230. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  231. jack_error ("cannot use real-time scheduling (FIFO at priority %d) "
  232. "[for thread %d, from thread %d] (%d: %s)",
  233. rtparam.sched_priority,
  234. thread, pthread_self (),
  235. x, strerror (x));
  236. return -1;
  237. }
  238. return 0;
  239. }
  240. #endif /* JACK_USE_MACH_THREADS */