use std::fs::File;
use std::io;
use std::sync::{Arc, Mutex};
#[derive(Clone, Copy, Debug)]
struct View {
address: usize,
bytes: usize,
}
#[derive(Debug)]
enum Mapping {
Unmapped,
Ready(View),
Invalidated,
Closed,
}
#[derive(Debug)]
struct State {
file: Option<File>,
mapping: Mapping,
}
#[derive(Clone, Debug)]
pub struct MappedFlush(Arc<Mutex<State>>);
impl MappedFlush {
pub(super) fn new(file: File) -> Self {
Self(Arc::new(Mutex::new(State {
file: Some(file),
mapping: Mapping::Unmapped,
})))
}
pub fn flush(&self) -> io::Result<()> {
self.flush_with(&SystemIo)
}
pub(super) fn clone_file(&self) -> io::Result<File> {
let state = self
.0
.lock()
.map_err(|_| io::Error::other("mapped flush ownership is poisoned"))?;
state
.file
.as_ref()
.ok_or_else(|| {
io::Error::new(io::ErrorKind::BrokenPipe, "mapped flush file is closed")
})?
.try_clone()
}
fn flush_with(&self, io: &impl FlushIo) -> io::Result<()> {
let state = self
.0
.lock()
.map_err(|_| io::Error::other("mapped flush ownership is poisoned"))?;
flush(&state, io)
}
pub(super) fn invalidate(&self) {
let mut state = self.0.lock().unwrap_or_else(|error| error.into_inner());
state.mapping = Mapping::Invalidated;
}
pub(super) fn publish<T>(&self, items: &[T]) {
let mut state = self.0.lock().unwrap_or_else(|error| error.into_inner());
state.mapping = if items.is_empty() {
Mapping::Unmapped
} else {
Mapping::Ready(View {
address: items.as_ptr() as usize,
bytes: std::mem::size_of_val(items),
})
};
}
pub(super) fn close(&self) {
let mut state = self.0.lock().unwrap_or_else(|error| error.into_inner());
if !self.0.is_poisoned() {
let _ = flush(&state, &SystemIo);
}
state.mapping = Mapping::Closed;
state.file.take();
}
}
trait FlushIo {
fn view(&self, view: View) -> io::Result<()>;
fn file(&self, file: &File) -> io::Result<()>;
}
fn flush(state: &State, io: &impl FlushIo) -> io::Result<()> {
match state.mapping {
Mapping::Ready(view) => io.view(view)?,
Mapping::Unmapped => {}
Mapping::Invalidated => {
return Err(io::Error::other(
"mapped flush owner has an unfinished remap",
));
}
Mapping::Closed => {
return Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"mapped flush owner is closed",
));
}
}
let file = state
.file
.as_ref()
.ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "mapped flush file is closed"))?;
io.file(file)
}
struct SystemIo;
impl FlushIo for SystemIo {
fn view(&self, view: View) -> io::Result<()> {
#[cfg(windows)]
{
if unsafe {
windows_sys::Win32::System::Memory::FlushViewOfFile(
view.address as *const _,
view.bytes,
)
} == 0
{
return Err(io::Error::last_os_error());
}
}
#[cfg(not(windows))]
let _ = (view.address, view.bytes);
Ok(())
}
fn file(&self, file: &File) -> io::Result<()> {
file.sync_all()
}
}
#[cfg(test)]
#[path = "mapped_flush_tests.rs"]
mod tests;