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.

308 lines
7.0KB

  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 <jack/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. }
  85. typedef void (* stack_touch_t)();
  86. static volatile stack_touch_t ptr_jack_thread_touch_stack = jack_thread_touch_stack;
  87. static void*
  88. jack_thread_proxy (void* varg)
  89. {
  90. jack_thread_arg_t* arg = (jack_thread_arg_t*) varg;
  91. void* (*work)(void*);
  92. void* warg;
  93. jack_client_t* client = arg->client;
  94. if (arg->realtime) {
  95. ptr_jack_thread_touch_stack();
  96. maybe_get_capabilities (client);
  97. jack_acquire_real_time_scheduling (pthread_self(), arg->priority);
  98. }
  99. warg = arg->arg;
  100. work = arg->work_function;
  101. free (arg);
  102. return work (warg);
  103. }
  104. int
  105. jack_client_create_thread (jack_client_t* client,
  106. pthread_t* thread,
  107. int priority,
  108. int realtime,
  109. void*(*start_routine)(void*),
  110. void* arg)
  111. {
  112. #ifndef JACK_USE_MACH_THREADS
  113. pthread_attr_t attr;
  114. jack_thread_arg_t* thread_args;
  115. #endif /* !JACK_USE_MACH_THREADS */
  116. int result = 0;
  117. if (!realtime) {
  118. result = jack_thread_creator (thread, 0, start_routine, arg);
  119. if (result) {
  120. log_result("creating thread with default parameters",
  121. result);
  122. }
  123. return result;
  124. }
  125. /* realtime thread. this disgusting mess is a reflection of
  126. * the 2nd-class nature of RT programming under POSIX in
  127. * general and Linux in particular.
  128. */
  129. #ifndef JACK_USE_MACH_THREADS
  130. pthread_attr_init(&attr);
  131. result = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  132. if (result) {
  133. log_result("requesting explicit scheduling", result);
  134. return result;
  135. }
  136. result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  137. if (result) {
  138. log_result("requesting joinable thread creation", result);
  139. return result;
  140. }
  141. #ifdef __OpenBSD__
  142. result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
  143. #else
  144. result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  145. #endif
  146. if (result) {
  147. log_result("requesting system scheduling scope", result);
  148. return result;
  149. }
  150. result = pthread_attr_setstacksize(&attr, THREAD_STACK);
  151. if (result) {
  152. log_result("setting thread stack size", result);
  153. return result;
  154. }
  155. if ((thread_args = (jack_thread_arg_t *) malloc (sizeof (jack_thread_arg_t))) == NULL) {
  156. return -1;
  157. }
  158. thread_args->client = client;
  159. thread_args->work_function = start_routine;
  160. thread_args->arg = arg;
  161. thread_args->realtime = 1;
  162. thread_args->priority = priority;
  163. result = jack_thread_creator (thread, &attr, jack_thread_proxy, thread_args);
  164. if (result) {
  165. log_result ("creating realtime thread", result);
  166. return result;
  167. }
  168. #else /* JACK_USE_MACH_THREADS */
  169. result = jack_thread_creator (thread, 0, start_routine, arg);
  170. if (result) {
  171. log_result ("creating realtime thread", result);
  172. return result;
  173. }
  174. /* time constraint thread */
  175. setThreadToPriority (*thread, 96, TRUE, 10000000);
  176. #endif /* JACK_USE_MACH_THREADS */
  177. return 0;
  178. }
  179. int
  180. jack_client_real_time_priority (jack_client_t* client)
  181. {
  182. if (!client->engine->real_time) {
  183. return -1;
  184. }
  185. return client->engine->client_priority;
  186. }
  187. int
  188. jack_client_max_real_time_priority (jack_client_t* client)
  189. {
  190. if (!client->engine->real_time) {
  191. return -1;
  192. }
  193. return client->engine->max_client_priority;
  194. }
  195. #if JACK_USE_MACH_THREADS
  196. int
  197. jack_drop_real_time_scheduling (pthread_t thread)
  198. {
  199. setThreadToPriority(thread, 31, FALSE, 10000000);
  200. return 0;
  201. }
  202. int
  203. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  204. //priority is unused
  205. {
  206. setThreadToPriority(thread, 96, TRUE, 10000000);
  207. return 0;
  208. }
  209. #else /* !JACK_USE_MACH_THREADS */
  210. int
  211. jack_drop_real_time_scheduling (pthread_t thread)
  212. {
  213. struct sched_param rtparam;
  214. int x;
  215. memset (&rtparam, 0, sizeof (rtparam));
  216. rtparam.sched_priority = 0;
  217. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  218. jack_error ("cannot switch to normal scheduling priority(%s)\n",
  219. strerror (errno));
  220. return -1;
  221. }
  222. return 0;
  223. }
  224. int
  225. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  226. {
  227. struct sched_param rtparam;
  228. int x;
  229. memset (&rtparam, 0, sizeof (rtparam));
  230. rtparam.sched_priority = priority;
  231. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  232. jack_error ("cannot use real-time scheduling (FIFO at priority %d) "
  233. "[for thread %d, from thread %d] (%d: %s)",
  234. rtparam.sched_priority,
  235. thread, pthread_self(),
  236. x, strerror (x));
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. #endif /* JACK_USE_MACH_THREADS */