Skip to content

rp2: make GC lock work per CPU - #7217

Closed
dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:gc-lock-per-core
Closed

dpgeorge wants to merge 2 commits into
micropython:masterfrom
dpgeorge:gc-lock-per-core

Conversation

@dpgeorge

@dpgeorge dpgeorge commented May 4, 2021

Copy link
Copy Markdown
Member

The rp2 port is the first port with multithreading without a GIL, and hard interrupts. To make such IRQs work correctly some things need to be improved wrt locking the GC.

This PR takes a first step by making gc_lock_depth have one instance per CPU core. That means that a given core can run a hard IRQ independently to the other, ie not lock the GC for the other CPU core while the hard IRQ is running.

It also puts more code in RAM for the rp2 port to make Pin.irq() have less jitter.

Should fix #6957.

@dpgeorge dpgeorge added py-core Relates to py/ directory in source port-rp2 labels May 4, 2021
@dpgeorge
dpgeorge force-pushed the gc-lock-per-core branch from 6b23a4d to 8c2f11e Compare May 6, 2021 06:22
dpgeorge added 2 commits May 6, 2021 23:24
This commit makes gc_lock_depth have one counter per CPU core that the
MicroPython VM can run on.  This means a given core can run a hard IRQ
independently to the other cores, ie only lock the GC for itself while the
hard IRQ is running.

The commit also changes protection of lock/unlock to use atomic state
rather than a GC mutex (only enabled for GIL-less threading).  This fixes a
rare bug, on eg stm32, where a hard IRQ during a call to gc_lock() (eg 2
hard IRQs in quick succession, or a hard IRQ during a Python gc.lock()
call) would put gc_lock_depth in a bad state.

It also puts the check for gc_lock_depth outside the GC mutex in gc_alloc,
gc_realloc and gc_free, to prevent a hard IRQ from waiting on a mutex.

It also removes using the gc_lock during a collection:
- scheduled code is not run
- hard irqs should still be able to run
- with GIL enabled, GIL is not released
- with GIL disabled, other threads can continue to work until they need gc

Signed-off-by: Damien George <[email protected]>
So that hard IRQs can run correctly.

Fixes issue micropython#6957.

Signed-off-by: Damien George <[email protected]>
@dpgeorge

Copy link
Copy Markdown
Member Author

Closing in favour of #7238.

@dpgeorge dpgeorge closed this May 10, 2021
@dpgeorge
dpgeorge deleted the gc-lock-per-core branch May 10, 2021 02:50
tannewt added a commit to tannewt/circuitpython that referenced this pull request Nov 18, 2022
samd21: port_disable_tick() should disable event channel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

port-rp2 py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rp2: Crash with hard pin IRQ

1 participant