On Fri, 18 Apr 2008, David Brownell wrote:
> > So the loop tries the write for the first time, fails (which is perfectly
> > acceptable), then sleeps for more than timeout jiffies. The timeout
> > expires and the write is never tried again.
>
> That specific scenario can't happen. It's forced to "re"try a few times,
> even if the vagaries of scheduling make it sleep "too long".
Yeah, I missed that. Though it's still possible that the last write
attempt will be only 3 ms after the loop started, even if the timeout is
supposed to be longer.
> > Generally, if you want to wait at least a certain amount time for a
> > condition to become true, you must be sure to check the condition at
> > least once AFTER the time limit.
>
> Fair enough. But if you're going to be concerned about that, you
> should also be concerned about the start point for this "timeout".
> It should begin closer to when the previous write's STOP got to the
> chip.
As long as the start point is after the last write, and we know it is, then
the timeout is correct. You want to be sure at least X ms have elapsed
since the previous write operation finished and the last failed attempt at
the current current. Starting the timeout closer to the previous write's
STOP would let you avoid waiting longer than necessary, but it's not needed
for correctness.
> > Here's two examples of a good way to do this, depending on how you want
> > the control flow to go.
> >
> > unsigned long timeout = ... ;
> > for (;;) {
> > unsigned long wtime = jiffies;
>
> Given preemption and other scheduling delays, "wtime" can be
> arbitrarily long before the bytes start to arrive at the chip.
> This suffers the same flaw you mentioned above.
I'm afraid I don't follow you. How is possible for this loop to timeout
before the chip has been given the minimum time needed to respond?
>
> > if (write() == success)
> > break;
> > if (time_after_eq(wtime, timeout)) {
> > ... failure ...
> > return -ESOMETHING;
> > }
> > msleep(1);
> > }
> > ... success ...
> >
> >
> > unsigned long timeout = ... ;
> > for (wtime = timeout; time_after(wtime, timeout);) {
this should be !time_after
> > wtime = jiffies;
> > if (write() == success) {
> > ... success ...
> > return 0;
> > }
> > msleep(1);
> > }
> > ... failure ...
>
> Neither of those loops ever tries more than once, note.
Sure they do. They will aways try *at least* once, maybe more times
depending on how many chances they get before enough time has elapsed.
_______________________________________________
i2c mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/i2c