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.

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