Skip to content

Commit 9758ff3

Browse files
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 #108 (comment)
1 parent 3c22676 commit 9758ff3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/fs.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ pub struct Allocation<Storage: driver::Storage> {
7373
state: ll::lfs_t,
7474
}
7575

76+
/// # Safety
77+
///
78+
/// All operations are done on `&mut Allocation, and the reference is held
79+
/// during the entire lifetime of the filesystem, so once the reference is
80+
/// available again, the filesystem is closed
81+
unsafe impl<Storage: driver::Storage> Sync for Allocation<Storage> {}
82+
/// # Safety
83+
///
84+
/// All operations are done on `&mut Allocation, and the reference is held
85+
/// during the entire lifetime of the filesystem, so once the reference is
86+
/// available again, the filesystem is closed
87+
unsafe impl<Storage: driver::Storage> Send for Allocation<Storage> {}
88+
7689
// pub fn check_storage_requirements(
7790

7891
impl<Storage: driver::Storage> Default for Allocation<Storage> {
@@ -1547,4 +1560,21 @@ mod tests {
15471560
})
15481561
.unwrap();
15491562
}
1563+
1564+
#[allow(unreachable_code)]
1565+
fn _filesystem_is_sync() {
1566+
fn assert_is_sync<T: Sync, R>(_: &T) -> R {
1567+
todo!()
1568+
}
1569+
fn assert_is_send<T: Send, R>(_: &T) -> R {
1570+
todo!()
1571+
}
1572+
1573+
assert_is_sync::<Allocation<TestStorage>, ()>(todo!());
1574+
assert_is_send::<&mut Allocation<TestStorage>, ()>(todo!());
1575+
assert_is_send::<RefCell<&mut Allocation<TestStorage>>, ()>(todo!());
1576+
1577+
let mut test_storage = TestStorage::new();
1578+
Filesystem::mount_and_then(&mut test_storage, |fs| assert_is_send(fs)).unwrap()
1579+
}
15501580
}

0 commit comments

Comments
 (0)