Re: [PATCH v3] Staging: comedi: convert while loop to timeout inni_mio_common.c

From: Greg KH
Date: Tue Jan 14 2014 - 18:09:33 EST


On Tue, Jan 14, 2014 at 03:31:01PM -0600, Chase Southwood wrote:
> This patch to ni_mio_common.c changes a simple while loop to a timeout,
> which is preferred.
>
> Signed-off-by: Chase Southwood <chase.southwood@xxxxxxxxx>
> ---
>
> I removed the extra counter variable this time. Greg, you mentioned that I could just look at the time that has expired to far, and exit and error out if that exceeds a limit. Right now I'm just tracking how many times udelay is called, since that seems to conform more to other timeouts in this file, but did you instead want me to use some time function to keep track of actual real time that expires?

Please wrap your lines of text, as that's one very long line :)

The best way to do this is to do:
unsigned long timeout;
...
timeout = jiffies + msec_to_jiffies(500); /* Or how ever long you want to wait */
do {
do something...
udelay(10);
} while (time_before(jiffies, timeout);

or switch the loop around and do:

while (something hasn't happened) {
if (time_after(jjiffies, timeout)) {
we timed out, handle it here;
break;
}
udelay(10);
}

That's much better than having to count up a "number" and have to look
to see how long you are sleeping for to determine just exactly how long
you are going to have until the loop times out, as your code did.

Make sense?

greg k-h
--
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/