Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

rp2: fix atomic section#13312

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

dpgeorge
Copy link
Member

@dpgeorgedpgeorge commentedJan 1, 2024
edited
Loading

This is an attempt tofix#12980 and#13288.

There are two fixes to theMICROPY_BEGIN_ATOMIC_SECTION/ MICROPY_END_ATOMIC_SECTION on the rp2 port:

  • Use a recursive mutex to prevent deadlocks on the same thread, in the case an IRQ races against the thread level (on the same core) when acquiring the atomic section (mutex may be taken but IRQs not yet disabled, then the IRQ waits forever trying to acquire the mutux).
  • Unlock the mutex if it was locked, not ifcore1_entry is still non-null, to fix the case where the second core finishes (and setscore1_entry toNULL) while the first core is in the middle of an atomic operation.

Edit: the two changes are now:

  1. split out multicore lockout handling so it's only done when doing a flash erase/write
  2. change the ATOMIC_SECTION macros so they use new mutex enter/exit functions that also disable/restore interrupts (atomically).

@mendenm
Copy link

It works! At least the simple test script runs both processors and reports results form them.

Excellent.

@dpgeorge
Copy link
MemberAuthor

I have changed this to use a new set of mutex functions that also disable/restore interrupts when obtaining/releasing the mutex.

@projectgus
Copy link
Contributor

New approach looks good to me!

Using the multicore lockout feature in the general atomic section makes itmuch more difficult to get correct.Signed-off-by: Damien George <damien@micropython.org>
These allow entering/exiting a mutex and also disabling/restoringinterrupts, in an atomic way.Signed-off-by: Damien George <damien@micropython.org>
Prior to this commit there is a potential deadlock inmp_thread_begin_atomic_section(), when obtaining the atomic_mutex, in thefollowing situation:- main thread calls mp_thread_begin_atomic_section() (for whatever reason,  doesn't matter)- the second core is running so the main thread grabs the mutex via the  call mp_thread_mutex_lock(&atomic_mutex, 1), and this succeeds- before the main thread has a chance to run save_and_disable_interrupts()  a USB IRQ comes in and the main thread jumps off to process this IRQ- that USB processing triggers a call to the dcd_event_handler() wrapper  from commitbcbdee2- that then calls mp_sched_schedule_node()- that then attempts to obtain the atomic section, calling  mp_thread_begin_atomic_section()- that call then blocks trying to obtain atomic_mutex- core0 is now deadlocked on itself, because the main thread has the mutex  but the IRQ handler (which preempted the main thread) is blocked waiting  for the mutex, which will never be freeThe solution in this commit is to use mutex enter/exit functions that alsoatomically disable/restore interrupts.Fixes issuesmicropython#12980 andmicropython#13288.Signed-off-by: Damien George <damien@micropython.org>
@dpgeorgedpgeorge merged commitdc2a4e3 intomicropython:masterJan 3, 2024
@dpgeorgedpgeorge deleted the rp2-fix-atomic-section branchJanuary 3, 2024 05:17
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@projectgusprojectgusprojectgus left review comments

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

_thread module freezing whole device (RPI_PICO_W)
3 participants
@dpgeorge@mendenm@projectgus

[8]ページ先頭

©2009-2025 Movatter.jp