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

Kangrejos 2023 training solution #1

Draft
wants to merge 5 commits into
base: kangrejos-2023-training
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion samples/rust/rust_counting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

//! Rust counting example for Kangrejos

use kernel::{new_mutex, prelude::*, sync::Mutex};
use kernel::{
init::{self, PinnedDrop},
new_mutex,
prelude::*,
sync::Mutex,
};

module! {
type: RustCounting,
Expand Down Expand Up @@ -46,8 +51,63 @@ impl NamedCounter {
}
}

#[pin_data(PinnedDrop)]
struct MonitoredBuffer {
#[pin]
read: NamedCounter,
#[pin]
write: NamedCounter,
buf: Box<[u8; 100_000]>,
}

impl MonitoredBuffer {
fn new() -> impl PinInit<Self, Error> {
try_pin_init!(Self {
read <- NamedCounter::new("Read Counter", 0, i32::MAX),
write <- NamedCounter::new("Write Counter", 0, i32::MAX),
buf: Box::init(init::zeroed())?,
})
}

fn set(&mut self, idx: usize, val: u8) {
self.write.increment();
self.buf[idx] = val;
}

fn get(&self, idx: usize) -> u8 {
self.read.increment();
self.buf[idx]
}
}

#[pinned_drop]
impl PinnedDrop for MonitoredBuffer {
fn drop(self: Pin<&mut Self>) {
pr_info!(
"Monitored Buffer was read {} times and written to {} times.",
self.read.value(),
self.write.value()
)
}
}

#[derive(Zeroable, Debug)]
struct LotsOfData {
a_buf: [u8; 128],
b_buf: [u8; 256],
mode: i32,
count: usize,
data: *mut u8,
len: usize,
}

impl kernel::Module for RustCounting {
fn init(_module: &'static ThisModule) -> Result<Self> {
let data = Box::init(init!(LotsOfData {
mode: 8,
..Zeroable::zeroed()
}));
pr_info!("{data:?}");
Ok(Self)
}
}