| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define _BSD_SOURCE |
| 16 |
#define _DEFAULT_SOURCE |
| 17 |
#include <unistd.h> |
| 18 |
#ifndef __USE_POSIX199309 |
| 19 |
#define __USE_POSIX199309 |
| 20 |
#endif |
| 21 |
#include <time.h> |
| 22 |
#include "iaxclient_lib.h" |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
# include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#if TIME_WITH_SYS_TIME |
| 29 |
# include <sys/time.h> |
| 30 |
# include <time.h> |
| 31 |
#else |
| 32 |
# ifdef HAVE_SYS_TIME_H |
| 33 |
# include <sys/time.h> |
| 34 |
# else |
| 35 |
# include <time.h> |
| 36 |
# endif |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifndef NULL |
| 40 |
#define NULL (0) |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
void os_init(void) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
void iaxc_millisleep(long ms) |
| 50 |
{ |
| 51 |
struct timespec req; |
| 52 |
|
| 53 |
req.tv_nsec = (ms%1000)*1000*1000; |
| 54 |
req.tv_sec = ms/1000; |
| 55 |
|
| 56 |
|
| 57 |
nanosleep(&req,NULL); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int iaxci_post_event_callback(iaxc_event ev) |
| 63 |
{ |
| 64 |
#if 0 |
| 65 |
iaxc_event *e; |
| 66 |
e = malloc(sizeof(ev)); |
| 67 |
*e = ev; |
| 68 |
|
| 69 |
|
| 70 |
PostMessage(post_event_handle,post_event_id,(WPARAM) NULL, (LPARAM) e); |
| 71 |
#endif |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
#ifdef MACOSX |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#include <mach/mach.h> |
| 84 |
|
| 85 |
int iaxci_prioboostbegin() |
| 86 |
{ |
| 87 |
struct thread_time_constraint_policy ttcpolicy; |
| 88 |
int params [2] = {CTL_HW,HW_BUS_FREQ}; |
| 89 |
int hzms; |
| 90 |
size_t sz; |
| 91 |
int ret; |
| 92 |
|
| 93 |
|
| 94 |
sz = sizeof (hzms); |
| 95 |
sysctl (params, 2, &hzms, &sz, NULL, 0); |
| 96 |
|
| 97 |
|
| 98 |
hzms /= 1000; |
| 99 |
|
| 100 |
|
| 101 |
ttcpolicy.period = 10 * hzms; |
| 102 |
ttcpolicy.computation = 2 * hzms; |
| 103 |
ttcpolicy.constraint = 3 * hzms; |
| 104 |
ttcpolicy.preemptible = 1; |
| 105 |
|
| 106 |
if ( (ret = thread_policy_set(mach_thread_self(), |
| 107 |
THREAD_TIME_CONSTRAINT_POLICY, (int *)&ttcpolicy, |
| 108 |
THREAD_TIME_CONSTRAINT_POLICY_COUNT)) != KERN_SUCCESS ) |
| 109 |
{ |
| 110 |
fprintf(stderr, "thread_policy_set failed: %d.\n", ret); |
| 111 |
} |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
int iaxci_prioboostend() |
| 116 |
{ |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
#else |
| 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 |
#include <stdio.h> |
| 163 |
#include <sys/ioctl.h> |
| 164 |
#include <sys/time.h> |
| 165 |
#include <fcntl.h> |
| 166 |
#include <unistd.h> |
| 167 |
#include <signal.h> |
| 168 |
#include <sched.h> |
| 169 |
#include <pthread.h> |
| 170 |
#include <errno.h> |
| 171 |
|
| 172 |
|
| 173 |
#define DBUG(...) |
| 174 |
#define ERR_RPT(...) fprintf(stderr, __VA_ARGS__) |
| 175 |
|
| 176 |
#define SCHEDULER_POLICY SCHED_RR |
| 177 |
#define WATCHDOG_INTERVAL_USEC 1000000 |
| 178 |
#define WATCHDOG_MAX_SECONDS 3 |
| 179 |
|
| 180 |
typedef void *(*pthread_function_t)(void *); |
| 181 |
|
| 182 |
typedef struct { |
| 183 |
int priority; |
| 184 |
pthread_t ThreadID; |
| 185 |
|
| 186 |
struct timeval CanaryTime; |
| 187 |
int CanaryRun; |
| 188 |
pthread_t CanaryThread; |
| 189 |
int IsCanaryThreadValid; |
| 190 |
|
| 191 |
int WatchDogRun; |
| 192 |
pthread_t WatchDogThread; |
| 193 |
int IsWatchDogThreadValid; |
| 194 |
|
| 195 |
} prioboost; |
| 196 |
|
| 197 |
static prioboost *pb; |
| 198 |
|
| 199 |
static int CanaryProc( prioboost *b) |
| 200 |
{ |
| 201 |
int result = 0; |
| 202 |
struct sched_param schat = { 0 }; |
| 203 |
|
| 204 |
|
| 205 |
if( pthread_setschedparam(pthread_self(), SCHED_OTHER, &schat) != 0) |
| 206 |
return 1; |
| 207 |
|
| 208 |
while( b->CanaryRun) |
| 209 |
{ |
| 210 |
usleep( WATCHDOG_INTERVAL_USEC ); |
| 211 |
gettimeofday( &b->CanaryTime, NULL ); |
| 212 |
} |
| 213 |
|
| 214 |
return result; |
| 215 |
} |
| 216 |
|
| 217 |
static int WatchDogProc( prioboost *b ) |
| 218 |
{ |
| 219 |
struct sched_param schp = { 0 }; |
| 220 |
int maxPri; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
schp.sched_priority = b->priority + 4; |
| 225 |
maxPri = sched_get_priority_max(SCHEDULER_POLICY); |
| 226 |
if( schp.sched_priority > maxPri ) schp.sched_priority = maxPri; |
| 227 |
|
| 228 |
if (pthread_setschedparam(pthread_self(), SCHEDULER_POLICY, &schp) != 0) |
| 229 |
{ |
| 230 |
ERR_RPT("WatchDogProc: cannot set watch dog priority!\n"); |
| 231 |
goto killAudio; |
| 232 |
} |
| 233 |
|
| 234 |
DBUG("prioboost: WatchDog priority set to level %d!\n", schp.sched_priority); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
while( b->WatchDogRun ) |
| 239 |
{ |
| 240 |
|
| 241 |
int delta; |
| 242 |
struct timeval currentTime; |
| 243 |
|
| 244 |
usleep( WATCHDOG_INTERVAL_USEC ); |
| 245 |
gettimeofday( ¤tTime, NULL ); |
| 246 |
|
| 247 |
#if 0 |
| 248 |
|
| 249 |
delta = currentTime.tv_sec - b->EntryTime.tv_sec; |
| 250 |
DBUG(("WatchDogProc: audio delta = %d\n", delta )); |
| 251 |
if( delta > WATCHDOG_MAX_SECONDS ) |
| 252 |
{ |
| 253 |
goto killAudio; |
| 254 |
} |
| 255 |
#endif |
| 256 |
|
| 257 |
|
| 258 |
delta = currentTime.tv_sec - b->CanaryTime.tv_sec; |
| 259 |
DBUG("WatchDogProc: dogging, delta = %ld, mysec=%d\n", delta, currentTime.tv_sec); |
| 260 |
if( delta > WATCHDOG_MAX_SECONDS ) |
| 261 |
{ |
| 262 |
ERR_RPT("WatchDogProc: canary died!\n"); |
| 263 |
goto lowerAudio; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
DBUG("WatchDogProc: exiting.\n"); |
| 268 |
return 0; |
| 269 |
|
| 270 |
lowerAudio: |
| 271 |
{ |
| 272 |
struct sched_param schat = { 0 }; |
| 273 |
if( pthread_setschedparam(b->ThreadID, SCHED_OTHER, &schat) != 0) |
| 274 |
{ |
| 275 |
ERR_RPT("WatchDogProc: failed to lower audio priority. errno = %d\n", errno ); |
| 276 |
|
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
ERR_RPT("WatchDogProc: lowered audio priority to prevent hogging of CPU.\n"); |
| 281 |
goto cleanup; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
killAudio: |
| 286 |
ERR_RPT("WatchDogProc: killing hung audio thread!\n"); |
| 287 |
|
| 288 |
|
| 289 |
exit(1); |
| 290 |
|
| 291 |
cleanup: |
| 292 |
b->CanaryRun = 0; |
| 293 |
DBUG("WatchDogProc: cancel Canary\n"); |
| 294 |
pthread_cancel( b->CanaryThread ); |
| 295 |
DBUG("WatchDogProc: join Canary\n"); |
| 296 |
pthread_join( b->CanaryThread, NULL ); |
| 297 |
DBUG("WatchDogProc: forget Canary\n"); |
| 298 |
b->IsCanaryThreadValid = 0; |
| 299 |
|
| 300 |
#ifdef GNUSTEP |
| 301 |
GSUnregisterCurrentThread(); |
| 302 |
#endif |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
static void StopWatchDog( prioboost *b ) |
| 307 |
{ |
| 308 |
|
| 309 |
if( b->IsWatchDogThreadValid ) |
| 310 |
{ |
| 311 |
b->WatchDogRun = 0; |
| 312 |
DBUG("StopWatchDog: cancel WatchDog\n"); |
| 313 |
pthread_cancel( b->WatchDogThread ); |
| 314 |
pthread_join( b->WatchDogThread, NULL ); |
| 315 |
b->IsWatchDogThreadValid = 0; |
| 316 |
} |
| 317 |
|
| 318 |
if( b->IsCanaryThreadValid ) |
| 319 |
{ |
| 320 |
b->CanaryRun = 0; |
| 321 |
DBUG("StopWatchDog: cancel Canary\n"); |
| 322 |
pthread_cancel( b->CanaryThread ); |
| 323 |
DBUG("StopWatchDog: join Canary\n"); |
| 324 |
pthread_join( b->CanaryThread, NULL ); |
| 325 |
b->IsCanaryThreadValid = 0; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
static int StartWatchDog( prioboost *b) |
| 331 |
{ |
| 332 |
int hres; |
| 333 |
int result = 0; |
| 334 |
|
| 335 |
|
| 336 |
gettimeofday( &b->CanaryTime, NULL ); |
| 337 |
|
| 338 |
|
| 339 |
b->CanaryRun = 1; |
| 340 |
hres = pthread_create(&(b->CanaryThread), |
| 341 |
NULL , |
| 342 |
(pthread_function_t)CanaryProc, b); |
| 343 |
if( hres != 0 ) |
| 344 |
{ |
| 345 |
b->IsCanaryThreadValid = 0; |
| 346 |
result = 1; |
| 347 |
goto error; |
| 348 |
} |
| 349 |
b->IsCanaryThreadValid = 1; |
| 350 |
|
| 351 |
|
| 352 |
b->WatchDogRun = 1; |
| 353 |
hres = pthread_create(&(b->WatchDogThread), |
| 354 |
NULL , |
| 355 |
(pthread_function_t)WatchDogProc, b); |
| 356 |
if( hres != 0 ) { |
| 357 |
b->IsWatchDogThreadValid = 0; |
| 358 |
result = 1; |
| 359 |
goto error; |
| 360 |
} |
| 361 |
b->IsWatchDogThreadValid = 1; |
| 362 |
return result; |
| 363 |
|
| 364 |
error: |
| 365 |
StopWatchDog( b ); |
| 366 |
return result; |
| 367 |
} |
| 368 |
|
| 369 |
int iaxci_prioboostbegin() |
| 370 |
{ |
| 371 |
struct sched_param schp = { 0 }; |
| 372 |
prioboost *b = calloc(sizeof(*b),1); |
| 373 |
if (b == NULL) |
| 374 |
return 1; |
| 375 |
|
| 376 |
int result = 0; |
| 377 |
|
| 378 |
b->priority = (sched_get_priority_max(SCHEDULER_POLICY) - |
| 379 |
sched_get_priority_min(SCHEDULER_POLICY)) / 2; |
| 380 |
schp.sched_priority = b->priority; |
| 381 |
|
| 382 |
b->ThreadID = pthread_self(); |
| 383 |
|
| 384 |
if (pthread_setschedparam(b->ThreadID, SCHEDULER_POLICY, &schp) != 0) |
| 385 |
{ |
| 386 |
DBUG("prioboost: only superuser can use real-time priority.\n"); |
| 387 |
} |
| 388 |
else |
| 389 |
{ |
| 390 |
DBUG("prioboost: priority set to level %d!\n", schp.sched_priority); |
| 391 |
result = StartWatchDog( b ); |
| 392 |
} |
| 393 |
|
| 394 |
if(result == 0) { |
| 395 |
pb = b; |
| 396 |
} else { |
| 397 |
pb = NULL; |
| 398 |
schp.sched_priority = 0; |
| 399 |
pthread_setschedparam(b->ThreadID, SCHED_OTHER, &schp); |
| 400 |
} |
| 401 |
|
| 402 |
return result; |
| 403 |
} |
| 404 |
|
| 405 |
int iaxci_prioboostend() |
| 406 |
{ |
| 407 |
if(pb) StopWatchDog(pb); |
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
#endif |
| 412 |
|