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.

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