Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Nov 8, 2024
1 parent ac66068 commit 7c0a90c
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,26 +367,16 @@ impl File {
Ok(_) => Ok(()),
Err(err) => {
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
// Wait for the lock to be acquired. This can happen asynchronously,
// if the file handle was opened for async IO
let wait_result = c::WaitForSingleObject(overlapped.hEvent, c::INFINITE);
if wait_result == c::WAIT_OBJECT_0 {
// Wait completed successfully, get the lock operation status
let mut bytes_transferred = 0;
cvt(c::GetOverlappedResult(
self.handle.as_raw_handle(),
&mut overlapped,
&mut bytes_transferred,
c::TRUE,
))
.map(|_| ())
} else if wait_result == c::WAIT_FAILED {
// Wait failed
Err(io::Error::last_os_error())
} else {
// WAIT_ABANDONED and WAIT_TIMEOUT should not be possible
unreachable!()
}
// Wait for the lock to be acquired, and get the lock operation status.
// This can happen asynchronously, if the file handle was opened for async IO
let mut bytes_transferred = 0;
cvt(c::GetOverlappedResult(
self.handle.as_raw_handle(),
&mut overlapped,
&mut bytes_transferred,
c::TRUE,
))
.map(|_| ())
} else {
Err(err)
}
Expand Down Expand Up @@ -418,15 +408,16 @@ impl File {
)
});

if let Err(ref err) = result {
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32)
match result {
Ok(_) => Ok(true),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
return Ok(false);
Ok(false)
}
Err(err) => Err(err),
}
result?;
Ok(true)
}

pub fn try_lock_shared(&self) -> io::Result<bool> {
Expand All @@ -442,15 +433,16 @@ impl File {
)
});

if let Err(ref err) = result {
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32)
match result {
Ok(_) => Ok(true),
Err(err)
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32)
|| err.raw_os_error() == Some(c::ERROR_LOCK_VIOLATION as i32) =>
{
return Ok(false);
Ok(false)
}
Err(err) => Err(err),
}
result?;
Ok(true)
}

pub fn unlock(&self) -> io::Result<()> {
Expand Down

0 comments on commit 7c0a90c

Please sign in to comment.