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.

288 lines
6.5KB

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