Skip to content

Commit

Permalink
book: add section about creating threads
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Mar 7, 2024
1 parent b9a2817 commit 6c35cfd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion book/book.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[book]
authors = ["Kaspar Schleiser", "Emmanuel Baccelli"]
authors = ["Kaspar Schleiser", "Emmanuel Baccelli", "Elena Frank"]
language = "en"
multilingual = false
src = "src"
Expand Down
17 changes: 13 additions & 4 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ fn thread1() {
}
```

(*TODO*)

# Developer Guide

## riot-rs-threads
Expand Down Expand Up @@ -99,9 +97,20 @@ If a context switch is required, the following steps occur:
- updates the stack pointer
3. return from exception

### Thread Creation
### Creating Threads

Threads are created using `riot_rs_thread::thread_create`.
Apart from a pointer to the thread function, the first argument and the priority, it requires a pointer to the thread's stack as input.
The stack can be allocated using the [`static_cell`](https://docs.rs/static_cell/latest/static_cell/) crate, which allows to reserve memory at compile time that can then be initialized at runtime.

`thread_create` sets up the thread's stack and adds it to the runqueue.
Setting up the stack is arch specific and realized in the exact configuration as the CPU's interrupt-service routine does it when a running thread is interrupted by a context switch, so they can be restored after the ISR returns.
Apart from setting up the first argument and PC for the thread function, it also sets up the link-register with a cleanup function that will run once the user's function returned.

(*TODO*)
The user-side logic has to be implemented in a separate function and added to the `riot_rs_thread::THREAD_FNS` distributed slice.
A [`linkme::distributed_slice`](https://docs.rs/linkme/latest/linkme/struct.DistributedSlice.html) allows to declare a static slice of elements that is then linked into a contiguous section of the binary.
In `riot_rs`, distributed slices are used to inject initialization functions from the outside into the start-up code.
For convenience, the `riot_rs_macros::thread` macro is implements the above boilerplate code and can be used directly on the function that should run inside the thread.

- [Appendices](./appendices.md)
- [Coding Conventions](./coding-conventions.md)

0 comments on commit 6c35cfd

Please sign in to comment.