Re: [KJ] [patch] fix common mistake in polling loops

From: Andrew James Wade
Date: Wed Aug 09 2006 - 20:23:05 EST


[resending due to delivery failure.]

On Monday 07 August 2006 22:53, Om N. wrote:
> On 8/7/06, Darren Jenkins <darrenrjenkins@xxxxxxxxx> wrote:
> > G'day
> >
> > On 8/8/06, Pavel Machek <pavel@xxxxxxx> wrote:
> > >
> > > But you have to make sure YOU CHECK READY AFTER THE TIMEOUT. Linus'
> > > code does not do that.
> > > Pavel
> >
> >
> > Sorry I did not realise that was your problem with the code.
> > Would it help if we just explicitly added a
> >
> unsigned long timeout = jiffies + HZ/2;
> for (;;) {
> if (ready())
> return 0;
> [IMAGINE HALF A SECOND DELAY HERE]
> if (time_after(timeout, jiffies)) {
> if (ready())
> return 0;
> break;
> }
> msleep(10);
> }
> Wouldn't this be better than adding a check after the break of loop?

You're getting duplicated code there. That'll be an issue in more
complex loops. How about:

unsigned long timeout = jiffies + HZ/2;
int timeup = 0;

for (;;;) {
if (ready())
return 0;
if (timeup)
break;
msleep(10);
timeup = time_after(timeout, jiffies);
};
... timeout ...

However care is needed with the use of timeup, since timeup==1 only
indicates failure at the point it's tested in the loop; in particular
it won't indicate failure after the loop if "return 0" is changed to
"break".

Andrew Wade
-
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/