-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rp2: Fix recursive atomic sections when core1 is active.
mp_thread_begin_atomic_section() is expected to be recursive (i.e. for nested machine.disable_irq() calls, or if Python code calls disable_irq() and then the Python runtime calls mp_handle_pending() which also enters an atomic section to check the scheduler state). On rp2 when not using core1 the atomic sections are recursive. However when core1 was active (i.e. _thread) then there was a bug that caused the core to live-lock if an atomic section recursed. Adds a test case specifically for mutual exclusion and recursive atomic sections when using two threads. Without this fix the test immediately hangs on rp2. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
- Loading branch information
1 parent
d7d77d9
commit fef17c7
Showing
5 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Ensure that disabling IRQs creates mutual exclusion between threads | ||
# (also tests nesting of disable_irq across threads) | ||
import machine | ||
import time | ||
import _thread | ||
|
||
if not hasattr(machine, "disable_irq"): | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
count = 0 | ||
thread_done = False | ||
|
||
|
||
def inc_count(): | ||
global count | ||
a = machine.disable_irq() | ||
try: | ||
b = machine.disable_irq() | ||
try: | ||
count += 1 | ||
i = 0 | ||
while i < 100: | ||
count += 1 | ||
count -= 1 | ||
i += 1 | ||
finally: | ||
machine.enable_irq(b) | ||
pass | ||
finally: | ||
machine.enable_irq(a) | ||
pass | ||
|
||
|
||
def inc_count_multiple(times): | ||
for _ in range(times): | ||
inc_count() | ||
|
||
|
||
def thread_entry(inc_times): | ||
global thread_done | ||
inc_count_multiple(inc_times) | ||
thread_done = True | ||
|
||
|
||
_thread.start_new_thread(thread_entry, (1000,)) | ||
inc_count_multiple(1000) | ||
|
||
time.sleep(1) | ||
|
||
print("count", count, thread_done) | ||
if count == 2000: | ||
print("PASS") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
count 2000 True | ||
PASS |