Active waiting with yield()
From: Mikulas Patocka
Date: Fri Nov 14 2008 - 14:00:18 EST
Hi
Every time I write yield() somewhere into my code, there comes a comment
"real-time people don't like yield()". Without any explanation why yield()
is not suitable at that specific point, just that "people don't like it".
We all know that waiting with yield() is bad for general code that
processes requests, because repeated scheduling wastes extra CPU time and
the process waiting for some event with yield() is woken up late. In these
normal cases, wait queues should be used.
However, there is code where in my opinion using yield() for waiting is
appropriate, for example:
* driver unload --- check the count of outstanding requests and call
yield() repeatedly until it goes to zero, then unload.
* some rare race contition --- a workaround for a race that triggers once
per hour for one user in several years doesn't really have to be fast.
A typical example where yield() can be used is a driver that counts a
number of outstanding requests --- when the user tries to unload the
driver, the driver will make sure that new requests won't come, repeatedly
yield()s until the count of requests reaches zero and then destroys its
data structures.
Here, yield() has some advantages over wait queues, because:
* reduced size of data structures (and reduced cache footprint for the hot
path that actually processes requests)
* slightly reduced code size even on the hot path (no need to wake up an
empty queue)
* less possibility for bugs coming from the fact that someone forgets to
wake the queue up
The downside of yield is slower unloading of the driver by few tens of
miliseconds, but the user doesn't really care about fractions of a second
when unloading drivers.
--- but people complain even if I use yield() in these cases. None of
those complains contains any valid technical argument --- just that
"someone said that yield() is bad". Is there a real reason why yield() in
these cases is bad? (for example that with some real-time patches,
yield()ing high-priority thread would be spinning forever, killing the
system?) Or should I use msleep(1) instead? Or should wait queues really
be used even in those performance-insensitive cases?
Mikulas
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/