Wed, 08 Nov 2000 Mattias Engdeg?rd wrote:
Probably not… What’s needed at that level is sort of cooperative
… which is what we people who don’t believe in threads are using
I usually insert a small delay in the main game processing
to allow the kernel to schedule things like audio, if they are necessary.
Since it’s hard to do <10ms sleeps in Linux on x86, can you afford to
wait that long? (I’m not sure how to even get less than 20ms sleeps,
but I presume select() can do it.)
You can have a thread block for as short time as you like/need, as long as you
have some kernel driver, or other event to wake you up before the next jiffy
calls the scheduler. That is, without a high resolution timer, forget about
sleeping for less than 20 ms. (I don’t remember why, but IIRC, it’s not
possible to wake up in the next jiffy, not even with SCHED_FIFO. This might
be ralated to some special cases, though; not sure.)
Anyway, where to find a hires timer? Well, in some kinds of applications, the
audio card comes in very handy, and this works without kernel modifications.
However, it might not be a good idea to force audio buffering in the ms range
just to get a wake up source, as this will result in audio drop-outs, unless
you’re running on a lowlatency patched kernel… (Then again, this is a
non-issue if you’re using mmap() I/O for the audio, as that decouples the
buffering from the timing.)
Oh, well, short version: It’s possible, but a bit messy, especially on kernels
without high resolution timers.
There is a patch for the “normal” timers, but I actually don’t know if it’s in
2.4.0… checking No, I can’t find it. (It’s quite a messy patch, as I
understand it - it has to mess with the 100 Hz jiffy timer that the kernel
uses for scheduling, wall clock time etc, without breaking those parts of the
However, the RTC (Real Time Clock) is another usable time base. It’s a timer
that can be set as a 24 hour alarm clock, or to generate interrupts at
power-of-two rates from 2 Hz to 8192 Hz, and you can sleep on /dev/rtc and be
woken up every time an interrupt occurs. It’s available on all PCs, and Alphas.
(Similar chips are probably available on some other architectures as well.)
On Windoze, mmsystem timers with timeBeginPeriod()/timeEndPeriod() should be
usable for this. Music software have used them as a 1 kHz time base for MIDI
timing, calling their sequencer engines at that rate. (Nowadays, there is
finally a usable MIDI API with timestamped event queues, so that this idiotic
approach isn’t required. Idiotic, because the jitter was higher than the
resolution anyway, due to the mediocre RT performance of Windows, and waking
up the application at 1 kHz didn’t improve things…)
I am looking forward to common implementations of real-time processing.
If you have problem with the scheduling, try a FIFO or RR scheduling
policy. Unfortunately they require root privs (which is unwise to give
a program), but it can be handled by an external suided program or
I was just going to write one, but other things got in the way. The reason for
that is that 1) I need it at work, 2) I need it for my audio projects (both of
these requiring SCHED_FIFO on lowlatency kernels) and 3) others in the Linux
audio community want it for their audio hacking on LL kernels.
It’ll be a daemon which can promote non-root applications to SCHED_FIFO at any
priority lower than the max SCHED_FIFO prio (via a library interface). It will
use a thread runnig at the highest prio, and some system load measurement method
to implement watchdog functionality, in order to eliminate another problem with
SCHED_FIFO: System freezes due to bugs or overload in RT threads.
I think the standard 100Hz clock is silly nowadays; people who have
compiled their kernel with HZ=1000 report very little slowdowns due
to increased timer processing, but a noticeable better interactive
response. And Alpha has had it all along.
Well, Alpha used to be a bit more efficient than x86 on interrupt handling,
but these days it’s probably the memory technologies (cache misses) that impact
task switching the most.
OTOH, it’s not good design to “stress” the time sharing scheduling just because
some applications want to do periodic operations at extreme frequencies. If
these can be driven from other timeing sources (any hardware that delivers
interrupts will do), that’s the nice way to do it. As an extra bonus, you get
more accurate timing than you can ever get with a sane HZ value.
…- M u C o S -------------------------. .- David Olofson --------.
| A Free/Open Source | | Audio Hacker |
| Plugin and Integration Standard | | Linux Advocate |
| for | | Open Source Advocate |
| Professional and Consumer | | Singer |
| Multimedia | | Songwriter |
-----> http://www.linuxdj.com/mucos -'—> david at linuxdj.com -’