Skip to content

Commit

Permalink
Files can be send between tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Apr 23, 2020
1 parent 77ded47 commit bdabaf5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions common/littlefs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@ where
state: ManuallyDrop<Box<F>>,
}

// NOTE(unsafe) this is safe because `Box<F>` ("boxed FileState") owns its contents and is pinned,
// plus `FS` (handle to the filesystem) is marked as interrupt-safe (only true when "sync-cortex-a"
// is enabled)
unsafe impl<FS> Send for File<FS> where FS: Filesystem + Send {}

// NOTE(allow) `std::fs` version does not have an `is_empty` method
#[allow(clippy::len_without_is_empty)]
impl<FS> File<FS>
Expand Down
32 changes: 27 additions & 5 deletions firmware/examples/examples/rtfm-10-fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
#![no_main]
#![no_std]

use core::convert::TryInto;
use core::{convert::TryInto, str};

use exception_reset as _; // default exception handler
use panic_serial as _; // panic handler
use usbarmory::{
emmc::eMMC,
fs::{self, File, Fs},
fs::{self, File, Fs, Path},
println,
storage::MbrDevice,
};

fn filename() -> &'static Path {
b"foo.txt\0".try_into().unwrap()
}

#[rtfm::app]
const APP: () = {
struct Resources {
Expand All @@ -55,7 +59,7 @@ const APP: () = {

// NOTE `&f` denotes a "share-only" resource; this resource will always appear as a shared
// reference (`&-`) in tasks. One does not need to call `lock` on these resources to use them.
#[idle(resources = [&f], spawn = [foo, bar])]
#[idle(resources = [&f], spawn = [foo, bar, baz])]
fn idle(cx: idle::Context) -> ! {
// resource appears as a shared reference to the resource data: `&Fs`
let f: &Fs = cx.resources.f;
Expand All @@ -76,6 +80,13 @@ const APP: () = {
println!("[idle] {:?}", ent);
}

let filename = filename();
let file = File::open(*f, filename).unwrap();
println!("[idle] opened {}", filename);

// files can be send between tasks
cx.spawn.baz(file).ok().unwrap();

usbarmory::reset()
}

Expand All @@ -85,11 +96,12 @@ const APP: () = {
// makes a copy of the `Fs` handle
let f: Fs = *cx.resources.f;

let mut file = File::create(f, b"foo.txt\0".try_into().unwrap()).unwrap();
let filename = filename();
let mut file = File::create(f, filename).unwrap();
file.write(b"Hello!").unwrap();
file.close().unwrap();

println!("[foo] created file foo.txt");
println!("[foo] created file {}", filename);
}

// this task cannot perform FS operations because it doesn't have access to the `Fs` handle
Expand All @@ -98,4 +110,14 @@ const APP: () = {
fn bar(_cx: bar::Context) {
println!("[bar] no FS access");
}

#[task]
fn baz(_cx: baz::Context, mut f: File<Fs>) {
let filename = filename();
let mut buf = [0; 32];
let n = f.read(&mut buf).unwrap();
println!("[baz] read({}) -> {:?}", filename, str::from_utf8(&buf[..n]));
f.close().unwrap();
println!("[baz] closed {}", filename);
}
};

0 comments on commit bdabaf5

Please sign in to comment.