Make Filesystem Send

`Filesystem` is still `!Sync`, so this allows putting the filesystem behind a `Mutex`
and sharing it across threads but still prevents concurrent operations from multiple threads.

See https://github.com/trussed-dev/littlefs2/issues/108#issuecomment-2975793283
This commit is contained in:
Sosthène Guédon
2025-06-19 10:12:45 +02:00
parent 95d53c8f80
commit d5317124f8
+30
View File
@@ -62,6 +62,19 @@ pub struct Allocation<Storage: driver::Storage> {
state: ll::lfs_t,
}
/// # Safety
///
/// All operations are done on `&mut Allocation, and the reference is held
/// during the entire lifetime of the filesystem, so once the reference is
/// available again, the filesystem is closed
unsafe impl<Storage: driver::Storage> Sync for Allocation<Storage> {}
/// # Safety
///
/// All operations are done on `&mut Allocation, and the reference is held
/// during the entire lifetime of the filesystem, so once the reference is
/// available again, the filesystem is closed
unsafe impl<Storage: driver::Storage> Send for Allocation<Storage> {}
#[derive(Default, Clone, Debug)]
#[non_exhaustive]
pub struct Config {
@@ -1574,4 +1587,21 @@ mod tests {
})
.unwrap();
}
#[allow(unreachable_code)]
fn _filesystem_is_sync() {
fn assert_is_sync<T: Sync, R>(_: &T) -> R {
todo!()
}
fn assert_is_send<T: Send, R>(_: &T) -> R {
todo!()
}
assert_is_sync::<Allocation<TestStorage>, ()>(todo!());
assert_is_send::<&mut Allocation<TestStorage>, ()>(todo!());
assert_is_send::<RefCell<&mut Allocation<TestStorage>>, ()>(todo!());
let mut test_storage = TestStorage::new();
Filesystem::mount_and_then(&mut test_storage, |fs| assert_is_send(fs)).unwrap()
}
}