Skip to content
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

riot-rs-threads: cortex-m: add compiler fence around wfi dance #312

Merged

Conversation

kaspar030
Copy link
Collaborator

Fixes Cortex-M sched() not seeing changes to the runqueue.

See #310 (comment).

As to why the fence is necessary, I cannot explain. The critical section should actually imply a fence. In this case, the runqueue accesses are heavily inlined, so maybe something is off with the optimizer. Or, we're doing something wrong with how we access THREADS, which is a lot more likely than a compiler bug. 😏

Copy link
Collaborator

@elenaf9 elenaf9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, we're doing something wrong with how we access THREADS, which is a lot more likely than a compiler bug.

 loop {
-        if let Some(res) = critical_section::with(|cs| {
-            let threads = unsafe { &mut *THREADS.as_ptr(cs) };
+        if let Some(res) = THREADS.with_mut(|mut threads| {
             let next_pid = match threads.runqueue.get_next() {

Would also fixes this issue. But as discussed in #187 (comment), this negatively impacts performance. My guess is that because it internally borrows the value instead of operating on the pointer, some optimizations are prevented?

Also, I tested using compiler_fence instead of fence, which didn't fix the issue. So I guess it's on the hardware side rather than the compiler?

Either way, I tested you fix and it works for me 👍.

@kaspar030 kaspar030 added this pull request to the merge queue Jun 7, 2024
@kaspar030
Copy link
Collaborator Author

Would also fixes this issue. But as discussed in #187 (comment), this negatively impacts performance. My guess is that because it internally borrows the value instead of operating on the pointer, some optimizations are prevented?

Could be ... We'll need to get back to this at some point.

Thanks for testing! 👍

Merged via the queue into future-proof-iot:main with commit b810bd6 Jun 7, 2024
19 checks passed
@kaspar030 kaspar030 deleted the cortex_m_add_sched_fence branch June 7, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants