1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
Copyright (C) 2004 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Thread creation function including workarounds for real-time scheduling
behaviour on different glibc versions.
*/
#include <config.h>
#include <jack/jack.h>
#include <jack/thread.h>
#include "internal.h"
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/rtprio.h>
#include <unistd.h>
#endif
#include "local.h"
#ifdef JACK_USE_MACH_THREADS
#include <sysdeps/pThreadUtilities.h>
#endif
jack_thread_creator_t jack_thread_creator = pthread_create;
void
jack_set_thread_creator (jack_thread_creator_t jtc)
{
jack_thread_creator = jtc;
}
static inline void
log_result (char *msg, int res)
{
char outbuf[500];
snprintf (outbuf, sizeof(outbuf),
"jack_client_create_thread: error %d %s: %s",
res, msg, strerror (res));
jack_error (outbuf);
}
static void
maybe_get_capabilities (jack_client_t* client)
{
#ifdef USE_CAPABILITIES
if (client != 0) {
jack_request_t req;
if (client->engine->has_capabilities != 0) {
/* we need to ask the engine for realtime capabilities
before trying to run the thread work function
*/
VALGRIND_MEMSET (&req, 0, sizeof(req));
req.type = SetClientCapabilities;
req.x.cap_pid = getpid ();
jack_client_deliver_request (client, &req);
if (req.status) {
/* what to do? engine is running realtime, it
is using capabilities and has them
(otherwise we would not get an error
return) but for some reason it could not
give the client the required capabilities.
for now, allow the client to run, albeit
non-realtime.
*/
jack_error ("could not receive realtime capabilities, "
"client will run non-realtime");
}
}
}
#endif /* USE_CAPABILITIES */
}
static void
jack_thread_touch_stack ()
{
char buf[JACK_THREAD_STACK_TOUCH];
int i;
volatile char *buf_ptr = buf;
for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++)
buf_ptr[i] = (char)(i & 0xff);
}
typedef void (*stack_touch_t)();
static volatile stack_touch_t ptr_jack_thread_touch_stack = jack_thread_touch_stack;
static void*
jack_thread_proxy (void* varg)
{
jack_thread_arg_t* arg = (jack_thread_arg_t*)varg;
void* (*work)(void*);
void* warg;
jack_client_t* client = arg->client;
if (arg->realtime) {
ptr_jack_thread_touch_stack ();
maybe_get_capabilities (client);
jack_acquire_real_time_scheduling (pthread_self (), arg->priority);
}
warg = arg->arg;
work = arg->work_function;
free (arg);
return work (warg);
}
int
jack_client_create_thread (jack_client_t* client,
pthread_t* thread,
int priority,
int realtime,
void*(*start_routine)(void*),
void* arg)
{
#ifndef JACK_USE_MACH_THREADS
pthread_attr_t attr;
jack_thread_arg_t* thread_args;
#endif /* !JACK_USE_MACH_THREADS */
int result = 0;
if (!realtime) {
result = jack_thread_creator (thread, 0, start_routine, arg);
if (result) {
log_result ("creating thread with default parameters",
result);
}
return result;
}
/* realtime thread. this disgusting mess is a reflection of
* the 2nd-class nature of RT programming under POSIX in
* general and Linux in particular.
*/
#ifndef JACK_USE_MACH_THREADS
pthread_attr_init (&attr);
result = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
if (result) {
log_result ("requesting explicit scheduling", result);
return result;
}
result = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
if (result) {
log_result ("requesting joinable thread creation", result);
return result;
}
#ifdef __OpenBSD__
result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
#else
result = pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
#endif
if (result) {
log_result ("requesting system scheduling scope", result);
return result;
}
result = pthread_attr_setstacksize (&attr, THREAD_STACK);
if (result) {
log_result ("setting thread stack size", result);
return result;
}
if ((thread_args = (jack_thread_arg_t*)malloc (sizeof(jack_thread_arg_t))) == NULL) {
return -1;
}
thread_args->client = client;
thread_args->work_function = start_routine;
thread_args->arg = arg;
thread_args->realtime = 1;
thread_args->priority = priority;
result = jack_thread_creator (thread, &attr, jack_thread_proxy, thread_args);
if (result) {
log_result ("creating realtime thread", result);
return result;
}
#else /* JACK_USE_MACH_THREADS */
result = jack_thread_creator (thread, 0, start_routine, arg);
if (result) {
log_result ("creating realtime thread", result);
return result;
}
/* time constraint thread */
setThreadToPriority (*thread, 96, TRUE, 10000000);
#endif /* JACK_USE_MACH_THREADS */
return 0;
}
int
jack_client_real_time_priority (jack_client_t* client)
{
if (!client->engine->real_time) {
return -1;
}
return client->engine->client_priority;
}
int
jack_client_max_real_time_priority (jack_client_t* client)
{
if (!client->engine->real_time) {
return -1;
}
return client->engine->max_client_priority;
}
#if JACK_USE_MACH_THREADS
int
jack_drop_real_time_scheduling (pthread_t thread)
{
setThreadToPriority (thread, 31, FALSE, 10000000);
return 0;
}
int
jack_acquire_real_time_scheduling (pthread_t thread, int priority)
//priority is unused
{
setThreadToPriority (thread, 96, TRUE, 10000000);
return 0;
}
#else /* !JACK_USE_MACH_THREADS */
static int
jack_process_already_has_real_time_scheduling (int priority)
{
#if defined(__FreeBSD__)
int res;
struct rtprio rtp;
res = rtprio(RTP_LOOKUP, getpid(), &rtp);
if (res == 0 && rtp.type == RTP_PRIO_REALTIME && rtp.prio <= priority) {
jack_info("process already runs at sufficient realtime priority %u (<=%d)",
(unsigned)rtp.prio,
priority);
return 1; // process priority is sufficient
}
#endif
return 0; // no or don't know
}
int
jack_drop_real_time_scheduling (pthread_t thread)
{
struct sched_param rtparam;
int x, policy;
if ((x = pthread_getschedparam (thread, &policy, &rtparam)) != 0) {
jack_error ("cannot read thread scheduling priority(%s)\n",
strerror (errno));
return -1;
}
if (policy == SCHED_OTHER) {
return 0; // already
}
memset (&rtparam, 0, sizeof(rtparam));
if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
jack_error ("cannot switch to normal scheduling priority(%s)\n",
strerror (errno));
return -1;
}
return 0;
}
int
jack_acquire_real_time_scheduling (pthread_t thread, int priority)
{
struct sched_param rtparam;
int x;
if (jack_process_already_has_real_time_scheduling (priority) != 0) {
return 0;
}
memset (&rtparam, 0, sizeof(rtparam));
rtparam.sched_priority = priority;
if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
jack_error ("cannot use real-time scheduling (FIFO at priority %d) "
"[for thread %d, from thread %d] (%d: %s)",
rtparam.sched_priority,
thread, pthread_self (),
x, strerror (x));
return -1;
}
return 0;
}
#endif /* JACK_USE_MACH_THREADS */
|