Merge 'Thread-safe WindowsFile' from Levy A.

Prevents `BorrowMut` errors seen in #2217.

Closes #2295
This commit is contained in:
Pekka Enberg
2025-07-28 08:46:30 +03:00

View File

@@ -1,9 +1,10 @@
use super::MemoryIO;
use crate::{Clock, Completion, File, Instant, LimboError, OpenFlags, Result, IO};
use parking_lot::RwLock;
use std::cell::RefCell;
use std::io::{Read, Seek, Write};
use std::sync::Arc;
use tracing::{debug, trace};
use tracing::{debug, instrument, trace, Level};
pub struct WindowsIO {}
impl WindowsIO {
@@ -13,10 +14,8 @@ impl WindowsIO {
}
}
unsafe impl Send for WindowsIO {}
unsafe impl Sync for WindowsIO {}
impl IO for WindowsIO {
#[instrument(err, skip_all, level = Level::TRACE)]
fn open_file(&self, path: &str, flags: OpenFlags, direct: bool) -> Result<Arc<dyn File>> {
trace!("open_file(path = {})", path);
let mut file = std::fs::File::options();
@@ -29,10 +28,11 @@ impl IO for WindowsIO {
let file = file.open(path)?;
Ok(Arc::new(WindowsFile {
file: RefCell::new(file),
file: RwLock::new(file),
}))
}
#[instrument(err, skip_all, level = Level::TRACE)]
fn wait_for_completion(&self, c: Arc<Completion>) -> Result<()> {
while !c.is_completed() {
self.run_once()?;
@@ -40,16 +40,19 @@ impl IO for WindowsIO {
Ok(())
}
#[instrument(err, skip_all, level = Level::TRACE)]
fn run_once(&self) -> Result<()> {
Ok(())
}
#[instrument(skip_all, level = Level::TRACE)]
fn generate_random_number(&self) -> i64 {
let mut buf = [0u8; 8];
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
#[instrument(skip_all, level = Level::TRACE)]
fn get_memory_io(&self) -> Arc<MemoryIO> {
Arc::new(MemoryIO::new())
}
@@ -66,23 +69,23 @@ impl Clock for WindowsIO {
}
pub struct WindowsFile {
file: RefCell<std::fs::File>,
file: RwLock<std::fs::File>,
}
unsafe impl Send for WindowsFile {}
unsafe impl Sync for WindowsFile {}
impl File for WindowsFile {
#[instrument(err, skip_all, level = Level::TRACE)]
fn lock_file(&self, exclusive: bool) -> Result<()> {
unimplemented!()
}
#[instrument(err, skip_all, level = Level::TRACE)]
fn unlock_file(&self) -> Result<()> {
unimplemented!()
}
#[instrument(skip(self, c), level = Level::TRACE)]
fn pread(&self, pos: usize, c: Arc<Completion>) -> Result<Arc<Completion>> {
let mut file = self.file.borrow_mut();
let mut file = self.file.write();
file.seek(std::io::SeekFrom::Start(pos as u64))?;
let nr = {
let r = c.as_read();
@@ -95,13 +98,14 @@ impl File for WindowsFile {
Ok(c)
}
#[instrument(skip(self, c, buffer), level = Level::TRACE)]
fn pwrite(
&self,
pos: usize,
buffer: Arc<RefCell<crate::Buffer>>,
c: Arc<Completion>,
) -> Result<Arc<Completion>> {
let mut file = self.file.borrow_mut();
let mut file = self.file.write();
file.seek(std::io::SeekFrom::Start(pos as u64))?;
let buf = buffer.borrow();
let buf = buf.as_slice();
@@ -110,15 +114,17 @@ impl File for WindowsFile {
Ok(c)
}
#[instrument(err, skip_all, level = Level::TRACE)]
fn sync(&self, c: Arc<Completion>) -> Result<Arc<Completion>> {
let file = self.file.borrow_mut();
let file = self.file.write();
file.sync_all().map_err(LimboError::IOError)?;
c.complete(0);
Ok(c)
}
#[instrument(err, skip_all, level = Level::TRACE)]
fn size(&self) -> Result<u64> {
let file = self.file.borrow();
let file = self.file.read();
Ok(file.metadata().unwrap().len())
}
}