10000 py/gc: Make gc_lock_depth have a count per thread. by dpgeorge · Pull Request #7238 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py/gc: Make gc_lock_depth have a count per thread. #7238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2021

Conversation

dpgeorge
Copy link
Member

This is an alternative to #7217.

This PR makes gc_lock_depth have one counter per thread, instead of one global counter. This makes threads properly independent with respect to the GC, in particular threads can now independently lock the GC for themselves without locking it for other threads. It also means a given thread can run a hard IRQ without temporarily locking the GC for all other threads and potentially making them have MemoryError exceptions at random locations (this really only occurs on MCUs with multiple cores and no GIL, eg on the rp2 port).

The PR also removes protection of the GC lock/unlock functions, which is no longer needed when the counter is per thread (and this also fixes the cas where a hard IRQ calling gc_lock() may stall waiting for the mutex).

It also puts the check for gc_lock_depth > 0 outside the GC mutex in gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from waiting on a mutex if it does attempt to allocate heap memory (and putting the check outside the GC mutex is now safe now that there is a gc_lock_depth per thread).

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label May 10, 2021
@dpgeorge
Copy link
Member Author

Should fix #6957.

dpgeorge added 2 commits May 10, 2021 13:07
This commit makes gc_lock_depth have one counter per thread, instead of one
global counter.  This makes threads properly independent with respect to
the GC, in particular threads can now independently lock the GC for
themselves without locking it for other threads.  It also means a given
thread can run a hard IRQ without temporarily locking the GC for all other
threads and potentially making them have MemoryError exceptions at random
locations (this really only occurs on MCUs with multiple cores and no GIL,
eg on the rp2 port).

The commit also removes protection of the GC lock/unlock functions, which
is no longer needed when the counter is per thread (and this also fixes the
cas where a hard IRQ calling gc_lock() may stall waiting for the mutex).

It also puts the check for `gc_lock_depth > 0` outside the GC mutex in
gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from
waiting on a mutex if it does attempt to allocate heap memory (and putting
the check outside the GC mutex is now safe now that there is a
gc_lock_depth per thread).

Signed-off-by: Damien George <damien@micropython.org>
The RP2040 has 2 cores and supports running at most 2 Python threads (the
main one plus another), and will raise OSError if a thread cannot be
created because core1 is already in use.  This commit adjusts some thread
tests to be robust against such OSError's.  These tests now pass on rp2
boards.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the gc-lock-per-thread branch from b63767a to 4cdcbdb Compare May 10, 2021 03:08
@robert-hh
Copy link
Contributor

In my test script this PR fixes #6957. No change however for #7124.

@dpgeorge
Copy link
Member Author

In my test script this PR fixes #6957. No change however for #7124.

Thanks for testing. I didn't yet get a chance to thoroughly investigate #7124.

@dpgeorge dpgeorge merged commit 4cdcbdb into micropython:master May 10, 2021
@dpgeorge dpgeorge deleted the gc-lock-per-thread branch May 10, 2021 06:56
@robert-hh
Copy link
Contributor

I didn't yet get a chance to thoroughly investigate #7124.

Thanks. It does not hurry. Take your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0