|
77 | 77 | 8.4 [Scheduling in uasyncio](./TUTORIAL.md#84-scheduling-in-uasyncio)
|
78 | 78 | 8.5 [Why cooperative rather than pre-emptive?](./TUTORIAL.md#85-why-cooperative-rather-than-pre-emptive)
|
79 | 79 | 8.6 [Communication](./TUTORIAL.md#86-communication)
|
80 |
| - 8.7 [Polling](./TUTORIAL.md#87-polling) |
| 80 | +9. [Polling vs Interrupts](./TUTORIAL.md#9-polling-vs-interrupts) A common |
| 81 | +source of confusion. |
81 | 82 |
|
82 | 83 | ###### [Main README](../README.md)
|
83 | 84 |
|
@@ -2558,12 +2559,34 @@ communications; in a cooperative system these are seldom required.
|
2558 | 2559 |
|
2559 | 2560 | ###### [Contents](./TUTORIAL.md#contents)
|
2560 | 2561 |
|
2561 |
| -## 8.7 Polling |
2562 |
| - |
2563 |
| -Some hardware devices such as the Pyboard accelerometer don't support |
2564 |
| -interrupts, and therefore must be polled (i.e. checked periodically). Polling |
2565 |
| -can also be used in conjunction with interrupt handlers: the interrupt handler |
2566 |
| -services the hardware and sets a flag. A task polls the flag: if it's set it |
2567 |
| -handles the data and clears the flag. A better approach is to use an `Event`. |
| 2562 | +# 9. Polling vs Interrupts |
| 2563 | + |
| 2564 | +The role of interrupts in cooperative systems has proved to be a source of |
| 2565 | +confusion in the forum. The merit of an interrupt service routine (ISR) is that |
| 2566 | +it runs very soon after the event causing it. On a Pyboard, Python code may be |
| 2567 | +running 15μs after a hardware change, enabling prompt servicing of hardware and |
| 2568 | +accurate timing of signals. |
| 2569 | + |
| 2570 | +The question arises whether it is possible to use interrupts to cause a task to |
| 2571 | +be scheduled at reduced latency. It is easy to show that, in a cooperative |
| 2572 | +scheduler, interrupts offer no latency benefit compared to polling the hardware |
| 2573 | +directly. |
| 2574 | + |
| 2575 | +The reason for this is that a cooperative scheduler only schedules tasks when |
| 2576 | +another task has yielded control. Consider a system with a number of concurrent |
| 2577 | +tasks, where the longest any task blocks before yielding to the scheduler is |
| 2578 | +`N`ms. In such a system, even with an ideal scheduler, the worst-case latency |
| 2579 | +between a hardware event occurring and its handling task beingnscheduled is |
| 2580 | +`N`ms, assuming that the mechanism for detecting the event adds no latency of |
| 2581 | +its own. |
| 2582 | + |
| 2583 | +In practice `N` is likely to be on the order of many ms. On fast hardware there |
| 2584 | +will be a negligible performance difference between polling the hardware and |
| 2585 | +polling a flag set by an ISR. On hardware such as ESP8266 and ESP32 the ISR |
| 2586 | +approach will probably be slower owing to the long and variable interrupt |
| 2587 | +latency of these platforms. |
| 2588 | + |
| 2589 | +Using an ISR to set a flag is probably best reserved for situations where an |
| 2590 | +ISR is already needed for other reasons. |
2568 | 2591 |
|
2569 | 2592 | ###### [Contents](./TUTORIAL.md#contents)
|
0 commit comments