Skip to content

Commit

Permalink
Implement #[cpu_local] attribute and API for primitive types (these…
Browse files Browse the repository at this point in the history
…us-os#1012)

* The new `cls` crate offers a `#[cpu_local]` attribute that can be applied
  to a static variable to declare it as Cpu-Local Storage (CLS),
  much like `#[thread_local]` and Thread-Local Storage (TLS).
  * The `cls_macro` crate provides a procedural macro that implements
    the `#[cpu_local]` attribute. Currently, this only supports basic
    primitive types: `u8`, `u16`, `u32`, `u64`. More coming later.
  * The API for accessing CPU-local primitive types is quite simple:
    only `load`, `fetch_add`, `fetch_sub`. Note that these functions are
    implemented as atomic with respect to ONLY the single current CPU,
    not atomic with respect to all memory accesses across all other CPUs.
    This is intentional as it is more efficient, and CPU-local variables are
    only accessible to a single CPU and not by another "foreign" CPU.
  * Currently this is only used for the preemption counter on each CPU,
    but it will be used for more variables in future PRs.

* This simplifies the implementation of `preemption` management,
  as we can rely on the `#[cpu_local]` API's semantics to be atomic
  with respect to the current CPU.
  * This also avoids a minor latent bug that existed in the old preemption
    implementation which made it theoretically possible for a task migration
    to occur between accessing the CPU-local preemption count and
    modifying that count. This wasn't a problem since Theseus does not yet
    perform task migration, but the new design makes this interleaving
    impossible and is thus future-proof for later task migration support.  

* Re-separate `preemption` and `cpu_local` into two different crates,
  as they previously were. This is possible because the preemption count
  now uses the new `#[cpu_local]` attribute, which allows `preemption`
  and `cpu_local` to not have cyclic dependencies on each other.
  * This is responsible for most of the changes in this PR: changing
    `cpu_local_preemption` back to `cpu_local` or `preemption`.

* Another current limitation is that we must use the `#[cpu_local]` attribute
  to _manually_ specify the offset of each CPU-local variable from the start
  of the `PerCpuData` storage type. This will be removed in a future PR.

Signed-off-by: Klimenty Tsoutsman <[email protected]>
Co-authored-by: Kevin Boos <[email protected]>
  • Loading branch information
tsoutsman and kevinaboos authored Jul 27, 2023
1 parent 38e585c commit 1f620ac
Show file tree
Hide file tree
Showing 29 changed files with 589 additions and 195 deletions.
118 changes: 88 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions applications/test_preemption_counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test_preemption_counter"
version = "0.1.0"
authors = ["Klim Tsoutsman <[email protected]>"]
description = "Application for testing the preemption counter"
edition = "2021"

[dependencies]
app_io = { path = "../../kernel/app_io" }
preemption = { path = "../../kernel/preemption" }
Loading

0 comments on commit 1f620ac

Please sign in to comment.