[mythtv] usleep(), busy wait and FreeBSD

Lorenzo Vicisano lorenzo at vicisano.net
Tue Sep 2 13:10:22 EDT 2003


In the process of trying to make mythtv work on FreeBSD
I came across an usleep() related issue.. hopefully somebody
on this list can shed some light..

Mythtv internals, e.g. libmythtv, make large use of usleep()
to implement inter-thread synchronization based on busy waits.

Many times usleep() is called with 5 to 100 *microseconds* delay
and this is the source of my problem in FreeBSD: the pthread
implementation in FreeBSD tries to honor the small waiting time
(below the system tick granularity) with a busy wait, that eats
all the CPU.

Not sure why this doesn't happen in Linux, a possibility is that the
process of the calling thread is actually put to sleep ... which would
bump the effective wait to the system tick value (1 millisecond ?).
If you know I'd be curious to learn..

Anyhow, these are my questions:

a) Is there a reason why synchronization in mythtv is implemented with
   busy waits and not with condition variables ?
b) If so, why such tiny waiting intervals are used ? 5 microseconds,
   for example.


BTW, I increased by a factor 1000 all the intervals under 1ms
and now mythtv runs almost fine.. there are a few bugs that were
either introduced or exposed by the timer changes, but the whole
thing seems still responsive.

More information about the mythtv-dev mailing list