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

core: provide dummy implementation of thread and mutex for riotboot #17959

Merged
merged 1 commit into from May 4, 2022

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Apr 17, 2022

Contribution description

Riotboot does not enter thread mode.
It can still be desirable to use periph code in this environment, e.g. SPI drivers that make use of mutex.
Since there are no threads, the implementation can be very simple: Busy wait on lock with the expectation to be un-locked from e.g. the TX complete ISR.

mtd_spi_nor nakes use of thread_yield() so provide a dummy implementation of that which is simple a no-op.

Testing procedure

Add

DISABLE_MODULE += core_thread

to some single-threaded tests. You might have to bump ISR_STACKSIZE.

Issues/PRs references

split off #17379

@github-actions github-actions bot added Area: build system Area: Build system Area: core Area: RIOT kernel. Handle PRs marked with this with care! labels Apr 17, 2022
@benpicco benpicco requested a review from fjmolinas April 17, 2022 15:00
@chrysn
Copy link
Member

chrysn commented Apr 17, 2022

I think this is split out from #17379 and not 17341.

@fjmolinas fjmolinas requested a review from maribu April 19, 2022 09:59
@fjmolinas
Copy link
Contributor

There are some uncrustify comments, maybe @maribu could take a look at this one?

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 19, 2022
@maribu
Copy link
Member

maribu commented Apr 19, 2022

I would like to see some safeguards, e.g. a static_assert() on max kernel threads being 1 in the fallback implementations of mutex_lock() and thread_yield() so that dropping core_mutex and core_thread really fails to compile unless one is doing something like RIOT boot.

I think @kaspar030 has a strong opinion on approaches like this. I personally do like this, though ;)

core/include/mutex.h Outdated Show resolved Hide resolved
@kaspar030
Copy link
Contributor

I think @kaspar030 has a strong opinion on approaches like this. I personally do like this, though ;)

If this is like, +26 lines as in this PR, I think it is ok.
But, riotboot can't just use the regular code but "MAXTHRADS=1"?

@jue89
Copy link
Contributor

jue89 commented Apr 19, 2022

Ah sweet, I like this. We removed mutexes for RIOTBOOT, as well. ROM was tight and our hack was required to fit our RIOTBOOT in 8kB.

@kaspar030
Copy link
Contributor

Ah sweet, I like this. We removed mutexes for RIOTBOOT, as well. ROM was tight and our hack was required to fit our RIOTBOOT in 8kB.

oooooooooook. ;)

@jue89
Copy link
Contributor

jue89 commented Apr 19, 2022

Ah sweet, I like this. We removed mutexes for RIOTBOOT, as well. ROM was tight and our hack was required to fit our RIOTBOOT in 8kB.

oooooooooook. ;)

Yeah, we are using an external SPI NOR flash to copy over new app images. This riotboot_mtd (if we apply the common name schema that arose during the last month) is still on my list of stuff to be upstreamed.

@github-actions github-actions bot added the Area: Kconfig Area: Kconfig integration label Apr 19, 2022
@maribu maribu added Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Apr 19, 2022
@fjmolinas
Copy link
Contributor

I would like to see some safeguards, e.g. a static_assert() on max kernel threads being 1 in the fallback implementations of mutex_lock() and thread_yield() so that dropping core_mutex and core_thread really fails to compile unless one is doing something like RIOT boot.

I think @kaspar030 has a strong opinion on approaches like this. I personally do like this, though ;)

I think @maribu suggestion on providing static asserts is still missing.

@benpicco benpicco removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 22, 2022
@benpicco
Copy link
Contributor Author

Added the static_assert

@maribu
Copy link
Member

maribu commented May 1, 2022

Looks good to me, please squash.

One last bike-shedding item: Does it really make sense to make core_mutex a module? I'd argue that just makeing core_thread a module and simply providing optimised mutex implementations for the case with and without core_thread would reflect the behaviour better. After all, the mutex implementation would still work fully as expected in either case.

@benpicco
Copy link
Contributor Author

benpicco commented May 1, 2022

Well the dummy mutex still works with threads enabled if there is only a single thread (that's how I tested it with a modified tests/buttons).

Since SUBMODULES is set to 1 there is an implicit core_mutex module anyway that we have to disable in order to avoid compiling mutex.c, so it felt most natural to do it that way.

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 1, 2022
@maribu
Copy link
Member

maribu commented May 1, 2022

Since SUBMODULES is set to 1 there is an implicit core_mutex module anyway that we have to disable in order to avoid compiling mutex.c, so it felt most natural to do it that way.

I understand that, but this seems to be an implementation detail to me. From the users point of view the mutex is always provided and always fulfills the API agreements.

Maybe it would be better to haven an #if MAXTHREADS < 2 ... #else in the mutex code? (With the C file effectively being empty now in case of MAXTHREADS < 2 - but I still hope to have all builds except maybe native with LTO=1, so that we can get rid of all static inline stuff.)

@benpicco
Copy link
Contributor Author

benpicco commented May 1, 2022

Yes this works well.
The upside is that now to test this, just add

DISABLE_MODULE += core_thread

to some simple examples, e.g. for the riotboot use case tests/vfs_default with CFLAGS += -DISR_STACKSIZE=2048

core/include/mutex.h Outdated Show resolved Hide resolved
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

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

ACK, code looks good and I am trusting your test results.

I really like this one!

@maribu maribu added the Process: needs >1 ACK Integration Process: This PR requires more than one ACK label May 2, 2022
@benpicco benpicco requested a review from kfessel May 2, 2022 11:15
This is intended for the bootloader module where we don't enter thread
mode, so mutex must never attempt to switch context.

Instead use a simple busy wait that is enough to make the possible mutex
users (e.g. interrupt based SPI) in bootloader mode work.
@benpicco benpicco added this to Backlog / Proposals in RIOT Sprint Day via automation May 2, 2022
Copy link
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

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

ACK

Copy link
Contributor

@fjmolinas fjmolinas left a comment

Choose a reason for hiding this comment

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

ACK

RIOT Sprint Day automation moved this from Backlog / Proposals to Waiting For Murdock May 4, 2022
@benpicco benpicco enabled auto-merge May 4, 2022 12:36
@benpicco benpicco merged commit b1715fe into RIOT-OS:master May 4, 2022
RIOT Sprint Day automation moved this from Waiting For Murdock to Done May 4, 2022
@chrysn chrysn added this to the Release 2022.07 milestone Aug 25, 2022
@benpicco benpicco deleted the core-dummy branch May 17, 2023 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: Kconfig Area: Kconfig integration CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Process: needs >1 ACK Integration Process: This PR requires more than one ACK Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines
Projects
Development

Successfully merging this pull request may close these issues.

None yet

6 participants