public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Active waiting with yield()
@ 2008-11-14 18:59 Mikulas Patocka
  2008-11-14 19:06 ` Alan Cox
  0 siblings, 1 reply; 17+ messages in thread
From: Mikulas Patocka @ 2008-11-14 18:59 UTC (permalink / raw)
  To: linux-kernel; +Cc: mingo, rml, Alasdair G Kergon, Milan Broz

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

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2008-11-20  9:18 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-14 18:59 Active waiting with yield() Mikulas Patocka
2008-11-14 19:06 ` Alan Cox
2008-11-14 19:34   ` Mikulas Patocka
2008-11-14 20:57     ` Peter Zijlstra
2008-11-14 21:41       ` Mikulas Patocka
2008-11-15 22:55         ` Peter Zijlstra
2008-11-17 17:39           ` Mikulas Patocka
2008-11-17 18:01             ` Alan Cox
2008-11-18 14:34               ` Mikulas Patocka
2008-11-18 14:40                 ` Alan Cox
2008-11-18 17:11                   ` Mikulas Patocka
2008-11-18 21:26                     ` Alan Cox
2008-11-20  9:18                       ` Mikulas Patocka
2008-11-19 16:14                     ` Peter Zijlstra
2008-11-18 14:49                 ` Arjan van de Ven
2008-11-18 17:14                   ` Mikulas Patocka
2008-11-14 21:21     ` Alan Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox