-
Notifications
You must be signed in to change notification settings - Fork 11
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
Start of Kobj support: Semaphores and Threads #12
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
fa00bea
zephyr::kconfig: Document module
d3zd3z 823b48a
zephyr: Add some missing doc comments
d3zd3z 45192f3
zephyr: Enforce documentation
d3zd3z ed875c2
zephyr: Add Error and Result types
d3zd3z e1196a0
zephyr: Make symbols from portable-atomic available
d3zd3z 594bbc4
zephyr: Introduce static kobject support
d3zd3z 3e362a7
zephyr: include portable atomic
d3zd3z 67e456b
zephyr: Add sys::Sempahore
d3zd3z d8fbb57
zephyr: `take` in static kernel object takes args
d3zd3z aed43fd
zephyr: Provide critical-section for Zephyr
d3zd3z 6598cd1
zephyr: Add alignment helper
d3zd3z a9ea9a1
zephyr: Add allocator support
d3zd3z 59907d7
zephyr-sys: Export stack alignment constants
d3zd3z 6b3e831
zephyr: Add sys::thread support
d3zd3z e71bd6a
samples: philosophers: Dining philosophers example
d3zd3z 096d34b
Upgrade crate versions to match Zephyr
d3zd3z 32b8fbf
zephyr: Add `k_uptime_get`
d3zd3z 907c044
zephyr: Implement Debug for Semaphore
d3zd3z 3d2477d
zephyr: StaticSemaphire needs to implement Sync
d3zd3z 3fd043a
zephyr: Semaphore methods should not be `mut`
d3zd3z c88307f
zephyr: hide docs of RealStaticThreadStack
d3zd3z 1b92ea5
zephyr: Expose some fields publicly
d3zd3z 1971feb
zephyr: StaticThread must be Sync
d3zd3z 97b217f
samples: hello_world: Fix dependency version
d3zd3z 2ccb628
zephyr: Fix some warnings by being conditional
d3zd3z e8eebb2
samples: philosophers: Fix yaml
d3zd3z a34507f
tests: time: Fix dependency
d3zd3z 91d6f33
zephyr-sys: Allow broken doc links in bindgen
d3zd3z 177a932
zephyr: object: Improve docs about safety
d3zd3z 1cd9299
zephyr: printk: Fix doc error
d3zd3z 266a7a7
zephyr: sys: sync: Fix some doc links
d3zd3z fdd97ae
zephyr: printk: Remove dependency on alloc
d3zd3z d01153a
zephyr: Create export type alias
d3zd3z 7375e44
zephyr: Use constructor for static thread stack
d3zd3z 8bbade3
zephyr: object: Add kboj_define support for arrays of stacks
d3zd3z 98a15fe
samples: philosophers: Convert to array of stacks
d3zd3z 861cb83
zephyr: docgen
d3zd3z 057e169
zephyr: Fix some broken doc comments
d3zd3z d11c5d2
module: Make sure all tests are run
d3zd3z File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../zephyr/build/rust/sample-cargo-config.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# In this case, we do want the symlink checked in. We'll assume we have the module in the standard | ||
# module place. | ||
# | ||
# On Windows, this symlink will just get checked out as a regular file and will have to be replaced | ||
# with a copy (or real symlinks enabled). But, this shouldn't affect CI runs of the documentation, | ||
# which are done on Linux. | ||
!.cargo/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
|
||
project(hello_rust_world) | ||
rust_cargo_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
[package] | ||
# This must be rustapp for now. | ||
name = "rustapp" | ||
version = "3.7.0" | ||
edition = "2021" | ||
description = "A small application to generate documentation" | ||
license = "Apache-2.0 or MIT" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
zephyr = "3.7.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
CONFIG_RUST=y | ||
CONFIG_RUST_ALLOC=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2024 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![no_std] | ||
|
||
use zephyr::printkln; | ||
|
||
// Reference the Zephyr crate so that the panic handler gets used. This is only needed if no | ||
// symbols from the crate are directly used. | ||
extern crate zephyr; | ||
|
||
#[no_mangle] | ||
extern "C" fn rust_main() { | ||
printkln!("Hello world from Rust on {}", | ||
zephyr::kconfig::CONFIG_BOARD); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(philosophers) | ||
|
||
rust_cargo_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
[package] | ||
# This must be rustapp for now. | ||
name = "rustapp" | ||
version = "3.7.0" | ||
edition = "2021" | ||
description = "A sample hello world application in Rust" | ||
license = "Apache-2.0 or MIT" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
zephyr = "3.7.0" | ||
|
||
# Dependencies that are used by build.rs. | ||
[build-dependencies] | ||
zephyr-build = "3.7.0" | ||
|
||
[profile.release] | ||
debug-assertions = true | ||
overflow-checks = true | ||
debug = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
mainmenu "Rust Dining Philosphers" | ||
|
||
source "Kconfig.zephyr" | ||
|
||
choice | ||
prompt "Select Synchronization implementation" | ||
default SYNC_SYS_SEMAPHORE | ||
|
||
config SYNC_SYS_SEMAPHORE | ||
bool "Use sys::Semaphore to synchronize forks" | ||
help | ||
Use to have the dining philosophers sample use sys::Semaphore, with one per form, | ||
to synchronize. | ||
|
||
endchoice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This board doesn't have a serial console, so use RTT. | ||
CONFIG_UART_CONSOLE=n | ||
CONFIG_RTT_CONSOLE=y | ||
CONFIG_USE_SEGGER_RTT=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) 2023 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// This crate needs access to kconfig variables. This is an example of how to do that. The | ||
// zephyr-build must be a build dependency. | ||
|
||
fn main() { | ||
zephyr_build::export_bool_kconfig(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) 2024 Linaro LTD | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
CONFIG_RUST=y | ||
CONFIG_RUST_ALLOC=y | ||
CONFIG_MAIN_STACK_SIZE=8192 | ||
|
||
# CONFIG_USERSPACE=y | ||
|
||
# Some debugging | ||
CONFIG_THREAD_MONITOR=y | ||
CONFIG_THREAD_ANALYZER=y | ||
CONFIG_THREAD_ANALYZER_USE_PRINTK=y | ||
CONFIG_THREAD_ANALYZER_AUTO=n | ||
# CONFIG_THREAD_ANALYZER_AUTO_INTERVAL=15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
sample: | ||
description: Philosphers, in Rust | ||
name: philosophers rust | ||
common: | ||
harness: console | ||
harness_config: | ||
type: one_line | ||
regex: | ||
# Match the statistics, and make sure that each philosopher has at least 10 (two digits) | ||
# meals. | ||
# - "^\\[\\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}, \\d{2,}\\]" | ||
# | ||
# Until the stastics have been implemented, just match on one of the children thinking | ||
- "^Child 5 thinking \\(\\d+ ticks.*" | ||
tags: rust | ||
filter: CONFIG_RUST_SUPPORTED | ||
tests: | ||
sample.rust.philosopher.semaphore: | ||
tags: introduction | ||
min_ram: 32 | ||
extra_configs: | ||
- CONFIG_SYNC_SYS_SEMAPHORE=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (c) 2023 Linaro LTD | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![no_std] | ||
|
||
// Cargo tries to detect configs that have typos in them. Unfortunately, the Zephyr Kconfig system | ||
// uses a large number of Kconfigs and there is no easy way to know which ones might conceivably be | ||
// valid. This prevents a warning about each cfg that is used. | ||
#![allow(unexpected_cfgs)] | ||
|
||
extern crate alloc; | ||
|
||
#[allow(unused_imports)] | ||
use alloc::boxed::Box; | ||
use alloc::vec::Vec; | ||
use zephyr::time::{Duration, sleep, Tick}; | ||
use zephyr::{ | ||
printkln, | ||
kobj_define, | ||
sys::uptime_get, | ||
sync::Arc, | ||
}; | ||
|
||
// These are optional, based on Kconfig, so allow them to be unused. | ||
#[allow(unused_imports)] | ||
use crate::semsync::semaphore_sync; | ||
|
||
mod semsync; | ||
|
||
/// How many philosophers. There will be the same number of forks. | ||
const NUM_PHIL: usize = 6; | ||
|
||
/// How much stack should each philosopher thread get. Worst case I've seen is riscv64, with 3336 | ||
/// bytes, when printing messages. Make a bit larger to work. | ||
const PHIL_STACK_SIZE: usize = 4096; | ||
|
||
// The dining philosophers problem is a simple example of cooperation between multiple threads. | ||
// This implementation use one of several different underlying mechanism to support this cooperation. | ||
|
||
// This example uses dynamic dispatch to allow multiple implementations. The intent is to be able | ||
// to periodically shut down all of the philosphers and start them up with a differernt sync | ||
// mechanism. This isn't implemented yet. | ||
|
||
/// The philosophers use a fork synchronization mechanism. Essentially, this is 6 locks, and will be | ||
/// implemented in a few different ways to demonstrate/test different mechanmism in Rust. All of | ||
/// them implement The ForkSync trait which provides this mechanism. | ||
trait ForkSync: core::fmt::Debug + Sync + Send { | ||
/// Take the given fork. The are indexed the same as the philosopher index number. This will | ||
/// block until the fork is released. | ||
fn take(&self, index: usize); | ||
|
||
/// Release the given fork. Index is the same as take. | ||
fn release(&self, index: usize); | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn rust_main() { | ||
printkln!("Hello world from Rust on {}", | ||
zephyr::kconfig::CONFIG_BOARD); | ||
printkln!("Time tick: {}", zephyr::time::SYS_FREQUENCY); | ||
|
||
let syncers = get_syncer(); | ||
|
||
printkln!("Pre fork"); | ||
|
||
for (i, syncer) in (0..NUM_PHIL).zip(syncers.into_iter()) { | ||
let thread = PHIL_THREADS[i].init_once(PHIL_STACKS[i].init_once(()).unwrap()).unwrap(); | ||
thread.spawn(move || { | ||
phil_thread(i, syncer); | ||
}); | ||
} | ||
|
||
let delay = Duration::secs_at_least(10); | ||
loop { | ||
// Periodically, printout the stats. | ||
zephyr::time::sleep(delay); | ||
} | ||
} | ||
|
||
#[cfg(CONFIG_SYNC_SYS_SEMAPHORE)] | ||
fn get_syncer() -> Vec<Arc<dyn ForkSync>> { | ||
semaphore_sync() | ||
} | ||
|
||
fn phil_thread(n: usize, syncer: Arc<dyn ForkSync>) { | ||
printkln!("Child {} started: {:?}", n, syncer); | ||
|
||
// Determine our two forks. | ||
let forks = if n == NUM_PHIL - 1 { | ||
// Per Dijkstra, the last phyilosopher needs to reverse forks, or we deadlock. | ||
(0, n) | ||
} else { | ||
(n, n+1) | ||
}; | ||
|
||
loop { | ||
{ | ||
printkln!("Child {} hungry", n); | ||
printkln!("Child {} take left fork", n); | ||
syncer.take(forks.0); | ||
printkln!("Child {} take right fork", n); | ||
syncer.take(forks.1); | ||
|
||
let delay = get_random_delay(n, 25); | ||
printkln!("Child {} eating ({} ms)", n, delay); | ||
sleep(delay); | ||
// stats.lock().unwrap().record_eat(n, delay); | ||
|
||
// Release the forks. | ||
printkln!("Child {} giving up forks", n); | ||
syncer.release(forks.1); | ||
syncer.release(forks.0); | ||
|
||
let delay = get_random_delay(n, 25); | ||
printkln!("Child {} thinking ({} ms)", n, delay); | ||
sleep(delay); | ||
// stats.lock().unwrap().record_think(n, delay); | ||
} | ||
} | ||
} | ||
|
||
/// Get a random delay, based on the ID of this user, and the current uptime. | ||
fn get_random_delay(id: usize, period: usize) -> Duration { | ||
let tick = (uptime_get() & (usize::MAX as i64)) as usize; | ||
let delay = (tick / 100 * (id + 1)) & 0x1f; | ||
|
||
// Use one greater to be sure to never get a delay of zero. | ||
Duration::millis_at_least(((delay + 1) * period) as Tick) | ||
} | ||
|
||
kobj_define! { | ||
static PHIL_THREADS: [StaticThread; NUM_PHIL]; | ||
static PHIL_STACKS: [ThreadStack<PHIL_STACK_SIZE>; NUM_PHIL]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good - certainly an improvement