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.

269 lines
6.1KB

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