public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Machek <pavel@ucw.cz>
To: Zed 0xff <zed.0xff@gmail.com>
Cc: kernel-janitors@osdl.org, linux-kernel@vger.kernel.org
Subject: Re: [patch] fix common mistake in polling loops
Date: Sat, 5 Aug 2006 11:45:47 +0000	[thread overview]
Message-ID: <20060805114547.GA5386@ucw.cz> (raw)
In-Reply-To: <20060805114052.GE4506@ucw.cz>

Hi!

> > task taken from http://kerneljanitors.org/TODO:
> > 
> > A _lot_ of drivers end up caring about absolute time, 
> > because a _lot_ of
> > drivers have a very simple issue like:
> > 
> > - poll this port every 10ms until it returns "ready", or 
> > until we time
> >   out after 500ms.
> > 
> > And the thing is, you can do it the stupid way:
> > 
> > 	for (i = 0; i < 50; i++) {
> > 		if (ready())
> > 			return 0;
> > 		msleep(10);
> > 	}
> > 	.. timeout ..
> > 
> > or you can do it the _right_ way. The stupid way is 
> > simpler, but anybody
> > who doesn't see what the problem is has some serious 
> > problems in kernel
> > programming. Hint: it might not be polling for half a 
> 
> Well, whoever wrote thi has some serious problems (in attitude
> department). *Any* loop you design may take half a minute under
> streange circumstances.
> 
> > second, it might be
> > polling for half a _minute_ for all you know.
> > 
> > In other words, the _right_ way to do this is literally
> > 
> > 	unsigned long timeout = jiffies + 
> > 	msecs_to_jiffies(500);
> > 	for (;;) {
> > 		if (ready())
> > 			return 0;
> > 		if (time_after(timeout, jiffies))
> > 			break;
> > 		msleep(10);
> > 	}
> > 
> > which is unquestionably more complex, yes, but it's more 
> > complex because
> > it is CORRECT!

Actually it may be broken, depending on use. In some cases this loop
may want to poll the hardware 50 times, 10msec appart... and your loop
can poll it only once in extreme conditions.

Actually your loop is totally broken, and may poll only once (without
any delay) and then directly timeout :-P -- that will break _any_
user.
							Pavel
-- 
Thanks for all the (sleeping) penguins.

  reply	other threads:[~2006-08-06 22:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-28  8:28 [patch] fix common mistake in polling loops Zed 0xff
2006-07-28  8:51 ` Jeff Garzik
2006-07-28 12:43 ` Dmitry Torokhov
2006-08-05 11:40 ` Pavel Machek
2006-08-05 11:45   ` Pavel Machek [this message]
2006-08-06 23:39     ` [KJ] " Darren Jenkins
2006-08-07 23:34       ` Pavel Machek
2006-08-08  0:53         ` Darren Jenkins
2006-08-08  2:53           ` Om N.
2006-08-10  0:25             ` Andrew James Wade
2006-08-10  1:11               ` Darren Jenkins
2006-08-08  9:19           ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060805114547.GA5386@ucw.cz \
    --to=pavel@ucw.cz \
    --cc=kernel-janitors@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zed.0xff@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox