Commit d840971
authored
machine: [rp2] discount scheduling delays in I2C timeouts (#4876)
The `gosched` call introduce arbitrary long delays in general, and in
TinyGo particular because the goroutine scheduler is cooperative and
doesn't preempt busy (e.g. compute-heavy) goroutines.
Before this change, the timeout logic would read, simplified:
deadline := now() + timeout
startTX()
for !txDone() {
if now() > deadline { return timeoutError }
gosched() // (1)
}
startRx() // (2)
for !rxDone() {
// (3)
if now() > deadline { return timeoutError }
gosched()
}
What could happen in a busy system is:
- The gosched marked (1) would push now() to be > than deadline.
- startRx is called (2), but the call to rxDone immediately after would
report it not yet done.
- The check marked (3) would fail, even though only a miniscule amount
of time has passed between startRx and the check.
This change ensures that the timeout clock discounts time spent in
`gosched`. The logic now reads, around every call to `gosched`:
deadline := now() + timeout
startTX()
for !txDone() {
if now() > deadline { return timeoutError }
before := now()
gosched()
deadline += now() - before
}
I tested this change by simulating a busy goroutine:
go func() {
for {
// Busy.
before := time.Now()
for time.Since(before) < 100*time.Millisecond {
}
// Sleep.
time.Sleep(100 * time.Millisecond)
}
}()
and testing that I2C transfers would no longer time out.1 parent ba3b3e8 commit d840971
1 file changed
+13
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
| 89 | + | |
93 | 90 | | |
94 | 91 | | |
95 | 92 | | |
| |||
218 | 215 | | |
219 | 216 | | |
220 | 217 | | |
221 | | - | |
222 | | - | |
| 218 | + | |
| 219 | + | |
223 | 220 | | |
224 | 221 | | |
225 | 222 | | |
| |||
285 | 282 | | |
286 | 283 | | |
287 | 284 | | |
288 | | - | |
| 285 | + | |
| 286 | + | |
289 | 287 | | |
290 | 288 | | |
291 | 289 | | |
| |||
338 | 336 | | |
339 | 337 | | |
340 | 338 | | |
| 339 | + | |
341 | 340 | | |
| 341 | + | |
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| |||
361 | 361 | | |
362 | 362 | | |
363 | 363 | | |
| 364 | + | |
364 | 365 | | |
| 366 | + | |
365 | 367 | | |
366 | 368 | | |
367 | 369 | | |
| |||
382 | 384 | | |
383 | 385 | | |
384 | 386 | | |
| 387 | + | |
385 | 388 | | |
| 389 | + | |
386 | 390 | | |
387 | 391 | | |
388 | 392 | | |
| |||
399 | 403 | | |
400 | 404 | | |
401 | 405 | | |
| 406 | + | |
402 | 407 | | |
| 408 | + | |
403 | 409 | | |
404 | 410 | | |
405 | 411 | | |
| |||
0 commit comments