Skip to content

Commit a378fec

Browse files
committed
THREADING.md: Add information re hard ISR hazards.
1 parent c3b61d3 commit a378fec

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

v3/docs/THREADING.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ interface is via a thread safe class, usually `ThreadSafeFlag`.
7878

7979
1. The ISR and the main program share the Python GIL. This ensures that built
8080
in Python objects (`list`, `dict` etc.) will not be corrupted if an ISR runs
81-
while the object is being modified. This guarantee is quite limited: the code
82-
will not crash, but there may be consistency problems. See consistency below.
81+
while the object's contents are being modified. This guarantee is limited: the
82+
code will not crash, but there may be consistency problems. See consistency
83+
below. Further, failure can occur if the object's _structure_ is modified, for
84+
example by the main program adding or deleting a dictionary entry. Note that
85+
globals are implemented as a `dict`. Globals should be declared before an ISR
86+
starts to run. Alternatively interrupts should be disabled while adding or
87+
deleting a global.
8388
2. An ISR will run to completion before the main program regains control. This
8489
means that if the ISR updates multiple items, when the main program resumes,
8590
those items will be mutually consistent. The above code fragment will provide
@@ -95,7 +100,7 @@ interface is via a thread safe class, usually `ThreadSafeFlag`.
95100

96101
#### locks
97102

98-
there is a valid case where a hard ISR checks the status of a lock, aborting if
103+
There is a valid case where a hard ISR checks the status of a lock, aborting if
99104
the lock is set.
100105

101106
#### consistency
@@ -133,7 +138,7 @@ This also includes code scheduled by `micropython.schedule()`.
133138

134139
1. The common GIL ensures that built-in Python objects (`list`, `dict` etc.)
135140
will not be corrupted if a read on one thread occurs while the object's
136-
contents are being updated.
141+
contents or the object's structure are being updated.
137142
2. This protection does not extend to user defined data structures. The fact
138143
that a dictionary won't be corrupted by concurrent access does not imply that
139144
its contents will be mutually consistent. In the code sample in section 1, if
@@ -159,7 +164,9 @@ thread safe classes offered here do not yet support Unix.
159164
is only required if mutual consistency of the three values is essential.
160165
3. In the absence of a GIL some operations on built-in objects are not thread
161166
safe. For example adding or deleting items in a `dict`. This extends to global
162-
variables which are implemented as a `dict`.
167+
variables which are implemented as a `dict`. Creating a new global on one core
168+
while another core reads a different global could fail in the event that the
169+
write operation triggered a re-hash. A lock should be used in such cases.
163170
4. The observations in 1.3 on user defined data structures and `uasyncio`
164171
interfacing apply.
165172
5. Code running on a core other than that running `uasyncio` may block for

0 commit comments

Comments
 (0)