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: always set threads.current_thread in sched() #262

Merged
merged 1 commit into from
Apr 16, 2024

Conversation

kaspar030
Copy link
Collaborator

This is taken from #182:

Setting threads.current_id to the very first running thread is now done within sched when the thread actually starts running. Before, this was done already in start_threading, but it caused some problems in the RISC-V implementation because then we couldn't differentiate between the thread running for the first time and current_id == next_id. I've tested on both arch'es and it still works with this change, so I think it should be alright?

This changes makes the number of cycles needed for one context switch go up from 254 to 259 (according to tests/benchmarks/bench_sched_yield on nrf52840dk). With the added line in sched() after the loop (instead of putting it in both cases), it'd go up to 261 cycles.

Fixes #148.

@kaspar030 kaspar030 requested a review from elenaf9 April 16, 2024 14:36
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.

Seems like the compiler previously optimized the else path away, which is now not possible anymore. I played around a bit, and when doing (on main) current_high_regs = core::hint::black_box(core::ptr::null()) in the else path it also takes 259 cycles.

Either way, as you also said in our out-of-band discussion @kaspar030, with 2% I think this is acceptable.

With the added line in sched() after the loop (instead of putting it in both cases), it'd go up to 261 cycles.

I kinda dislike that we now set it in both branches, but agree that performance should take priority over readability.

@kaspar030
Copy link
Collaborator Author

Agreed. I also think this code will change soon (let's see what #187 brings performance wise).
Merging this PR will get us the behavior needed for #182.

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

Thanks for taking a look!

Merged via the queue into future-proof-iot:main with commit 2084409 Apr 16, 2024
19 checks passed
@kaspar030 kaspar030 deleted the fix-no-threads branch April 16, 2024 20:55
elenaf9 added a commit to elenaf9/RIOT-rs that referenced this pull request Jul 26, 2024
With future-proof-iot#262, the `current_pid` is not set before the scheduler is triggered
for the first time, and therefore `None` on the first run.
The pointer to `current_high_regs` is therefore set to a null pointer.
This causes nrf52840 to hard fault when trying to store registers r4-r11.

To fix this, we now skip storing/ loading of r4-r11 if the pointer is
zero, i.e. when there was no previously running thread whose context has
to be saved.
elenaf9 added a commit to elenaf9/RIOT-rs that referenced this pull request Sep 13, 2024
With future-proof-iot#262, the `current_pid` is not set before the scheduler is triggered
for the first time, and therefore `None` on the first run.
The pointer to `current_high_regs` is therefore set to a null pointer.
This causes nrf52840 to hard fault when trying to store registers r4-r11.

To fix this, we now skip storing/ loading of r4-r11 if the pointer is
zero, i.e. when there was no previously running thread whose context has
to be saved.
elenaf9 added a commit to elenaf9/RIOT-rs that referenced this pull request Sep 27, 2024
With future-proof-iot#262, the `current_pid` is not set before the scheduler is triggered
for the first time, and therefore `None` on the first run.
The pointer to `current_high_regs` is therefore set to a null pointer.
This causes nrf52840 to hard fault when trying to store registers r4-r11.

To fix this, we now skip storing/ loading of r4-r11 if the pointer is
zero, i.e. when there was no previously running thread whose context has
to be saved.
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.

rt: panic if the threading feature is enabled but no threads exists
2 participants