@@ -78,8 +78,13 @@ interface is via a thread safe class, usually `ThreadSafeFlag`.
78
78
79
79
1 . The ISR and the main program share the Python GIL. This ensures that built
80
80
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.
83
88
2 . An ISR will run to completion before the main program regains control. This
84
89
means that if the ISR updates multiple items, when the main program resumes,
85
90
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`.
95
100
96
101
#### locks
97
102
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
99
104
the lock is set.
100
105
101
106
#### consistency
@@ -133,7 +138,7 @@ This also includes code scheduled by `micropython.schedule()`.
133
138
134
139
1 . The common GIL ensures that built-in Python objects (` list ` , ` dict ` etc.)
135
140
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.
137
142
2 . This protection does not extend to user defined data structures. The fact
138
143
that a dictionary won't be corrupted by concurrent access does not imply that
139
144
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.
159
164
is only required if mutual consistency of the three values is essential.
160
165
3 . In the absence of a GIL some operations on built-in objects are not thread
161
166
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.
163
170
4 . The observations in 1.3 on user defined data structures and ` uasyncio `
164
171
interfacing apply.
165
172
5 . Code running on a core other than that running ` uasyncio ` may block for
0 commit comments