Debian Codegraph @

3rdparty/iaxclient/lib/unixfuncs.c

raw symbols SCIP
1 /*
2 * iaxclient: a cross-platform IAX softphone library
3 *
4 * Copyrights:
5 * Copyright (C) 2003-2006, Horizon Wimba, Inc.
6 * Copyright (C) 2007, Wimba, Inc.
7 *
8 * Contributors:
9 * Steve Kann <[email protected]>
10 *
11 * This program is free software, distributed under the terms of
12 * the GNU Lesser (Library) General Public License.
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 /* Unix-specific functions */
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 /* yes, it can return early. We don't care */
57 nanosleep(&req,NULL);
58 }
59
60
61 /* TODO: Implement for X/MacOSX? */
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 /* XXX Test return value? */
70 PostMessage(post_event_handle,post_event_id,(WPARAM) NULL, (LPARAM) e);
71 #endif
72 return 0;
73 }
74
75 #ifdef MACOSX
76 /* Presently, OSX allows user-level processes to request RT
77 * priority. The API is nice, but the scheduler presently ignores
78 * the parameters (but the API validates that you're not asking for
79 * too much). See
80 * http://lists.apple.com/archives/darwin-development/2004/Feb/msg00079.html
81 */
82 /* include mach stuff for declaration of thread_policy stuff */
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 /* get hz */
94 sz = sizeof (hzms);
95 sysctl (params, 2, &hzms, &sz, NULL, 0);
96
97 /* make hzms actually hz per ms */
98 hzms /= 1000;
99
100 /* give us at least how much? 6-8ms every 10ms (we generally need less) */
101 ttcpolicy.period = 10 * hzms; /* 10 ms */
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 /* TODO */
118 return 0;
119 }
120
121 #else
122
123
124 /* Priority boosting/monitoring: Adapted from portaudio/pa_unix.c ,
125 * which carries the following copyright notice:
126 * PortAudio Portable Real-Time Audio Library
127 * Latest Version at: http://www.portaudio.com
128 * Linux OSS Implementation by douglas repetto and Phil Burk
129 *
130 * Copyright (c) 1999-2000 Phil Burk
131 *
132 * Permission is hereby granted, free of charge, to any person obtaining
133 * a copy of this software and associated documentation files
134 * (the "Software"), to deal in the Software without restriction,
135 * including without limitation the rights to use, copy, modify, merge,
136 * publish, distribute, sublicense, and/or sell copies of the Software,
137 * and to permit persons to whom the Software is furnished to do so,
138 * subject to the following conditions:
139 *
140 * The above copyright notice and this permission notice shall be
141 * included in all copies or substantial portions of the Software.
142 *
143 * Any person wishing to distribute modifications to the Software is
144 * requested to send the modifications to the original developer so that
145 * they can be incorporated into the canonical version.
146 *
147 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
148 */
149
150 /* It has been clarified by the authors that the request to send modifications
151 is a request, and not a condition */
152
153 /* Theory:
154 * The main thread is boosted to a medium real-time priority.
155 * Two additional threads are created:
156 * Canary: Runs as normal priority, updates a timevalue every second.
157 * WatchDog: Runs as a higher real-time priority. Checks to see that
158 * Canary is running. If Canary isn't running, lowers
159 * priority of calling thread, which has presumably run away
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 //#define DBUG(...) fprintf(stderr, __VA_ARGS__)
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 /* set us up with normal priority, please */
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 /* Run at a priority level above main thread so we can still run if it hangs. */
223 /* Rise more than 1 because of rumored off-by-one scheduler bugs. */
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 /* Compare watchdog time with audio and canary thread times. */
237 /* Sleep for a while or until thread cancelled. */
238 while( b->WatchDogRun )
239 {
240
241 int delta;
242 struct timeval currentTime;
243
244 usleep( WATCHDOG_INTERVAL_USEC );
245 gettimeofday( &currentTime, NULL );
246
247 #if 0
248 /* If audio thread is not advancing, then it must be hung so kill it. */
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 /* If canary died, then lower audio priority and halt canary. */
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 /* Fall through into killing audio thread. */
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 //pthread_cancel( b->ThreadID);
288 //pthread_join( b->ThreadID);
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(); /* SB20010904 */
302 #endif
303 return 0;
304 }
305
306 static void StopWatchDog( prioboost *b )
307 {
308 /* Cancel WatchDog thread if there is one. */
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 /* Cancel Canary thread if there is one. */
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 /* The watch dog watches for these timer updates */
336 gettimeofday( &b->CanaryTime, NULL );
337
338 /* Launch a canary thread to detect priority abuse. */
339 b->CanaryRun = 1;
340 hres = pthread_create(&(b->CanaryThread),
341 NULL /*pthread_attr_t * attr*/,
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 /* Launch a watchdog thread to prevent runaway audio thread. */
352 b->WatchDogRun = 1;
353 hres = pthread_create(&(b->WatchDogThread),
354 NULL /*pthread_attr_t * attr*/,
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); /* We are running at high priority so we should have a watchdog in case audio goes wild. */
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