[Asterisk-Dev] High resolution timers using POSIX clocks instead
of zaptel
Steve Kann
stevek at stevek.com
Fri May 27 09:53:48 MST 2005
Tilghman Lesher wrote:
>On Friday 27 May 2005 05:33, David Woodhouse wrote:
>
>
>>Using zaptel just for a time source is silly. We should use POSIX
>>timers instead. Here's a start, but I don't know how long it'll be
>>before I can get to finish it, so I thought I'd post it here in the
>>hope that someone else will pick it up.
>>
>>
>
>As noted here:
>
>http://lists.digium.com/pipermail/asterisk-users/2002-April/002072.html
>
>software timers are lacking.
>
>
I think that is just narrow thinking.
app_conference works all day and week long, without any hardware
support. It also does it without posix timers. It just uses nanosleep
and gettimeofday.
You can make things go X times per second, with great accuracy over a
long period of time with just those tools. What you can't always do is
make sure that you don't have a period where you oversleep for a few ten
of milliseconds. But if you keep track of time, you can just do more
work when you do wake up.
i.e. in pseudo-code
do_something_n_times_per_second(func_t something, int n) {
long now, last, begin = getcurrentmillis();
last = begin;
for(;;) {
usleep(n);
time now = getcurrentmillis();
while(last < now) {
something();
last+=n;
}
}
}
where getcurrentmillis() can be based on gettimeofday, of course.
This will call something() 1000/n times per second. It might not be
spaced out so that it happens _exactly_ every n ms, but it will, over a
particular time period, happen 1000/n times per second.
If this runs in a real-time process, and the something() takes less than
n ms to run, it will happen very close to every n ms.
Of course, most things don't need to run every ms, and this will work
better when n>1.
-SteveK
More information about the asterisk-dev
mailing list