Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target support: solaris #48

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ mod write_guard;
pub use read_guard::RwLockReadGuard;
pub use rw_lock::RwLock;
pub use write_guard::RwLockWriteGuard;

use rustix::{fd::AsFd, fs};

pub(crate) fn compatible_unix_lock<Fd: AsFd>(
fd: Fd,
operation: fs::FlockOperation,
) -> rustix::io::Result<()> {
#[cfg(not(target_os = "solaris"))]
return fs::flock(fd, operation);

#[cfg(target_os = "solaris")]
return fs::fcntl_lock(fd, operation);
}
6 changes: 3 additions & 3 deletions src/sys/unix/read_guard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustix::fd::AsFd;
use rustix::fs::{flock, FlockOperation};
use rustix::fs::FlockOperation;
use std::ops;

use super::RwLock;
use super::{compatible_unix_lock, RwLock};

#[derive(Debug)]
pub struct RwLockReadGuard<'lock, T: AsFd> {
Expand All @@ -27,6 +27,6 @@ impl<T: AsFd> ops::Deref for RwLockReadGuard<'_, T> {
impl<T: AsFd> Drop for RwLockReadGuard<'_, T> {
#[inline]
fn drop(&mut self) {
let _ = flock(&self.lock.inner.as_fd(), FlockOperation::Unlock).ok();
let _ = compatible_unix_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok();
}
}
23 changes: 10 additions & 13 deletions src/sys/unix/rw_lock.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustix::fd::AsFd;
use rustix::fs::{flock, FlockOperation};
use rustix::fs::FlockOperation;
use std::io::{self, Error, ErrorKind};

use super::{RwLockReadGuard, RwLockWriteGuard};
use super::{compatible_unix_lock, RwLockReadGuard, RwLockWriteGuard};

#[derive(Debug)]
pub struct RwLock<T: AsFd> {
Expand All @@ -17,32 +17,29 @@ impl<T: AsFd> RwLock<T> {

#[inline]
pub fn write(&mut self) -> io::Result<RwLockWriteGuard<'_, T>> {
flock(&self.inner.as_fd(), FlockOperation::LockExclusive)?;
compatible_unix_lock(self.inner.as_fd(), FlockOperation::LockExclusive)?;
Ok(RwLockWriteGuard::new(self))
}

#[inline]
pub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>, Error> {
flock(
&self.inner.as_fd(),
FlockOperation::NonBlockingLockExclusive,
)
.map_err(|err| match err.kind() {
ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
_ => Error::from(err),
})?;
compatible_unix_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive)
.map_err(|err| match err.kind() {
ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
_ => Error::from(err),
})?;
Ok(RwLockWriteGuard::new(self))
}

#[inline]
pub fn read(&self) -> io::Result<RwLockReadGuard<'_, T>> {
flock(&self.inner.as_fd(), FlockOperation::LockShared)?;
compatible_unix_lock(self.inner.as_fd(), FlockOperation::LockShared)?;
Ok(RwLockReadGuard::new(self))
}

#[inline]
pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, Error> {
flock(&self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err(
compatible_unix_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err(
|err| match err.kind() {
ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
_ => Error::from(err),
Expand Down
6 changes: 3 additions & 3 deletions src/sys/unix/write_guard.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustix::fd::AsFd;
use rustix::fs::{flock, FlockOperation};
use rustix::fs::FlockOperation;
use std::ops;

use super::RwLock;
use super::{compatible_unix_lock, RwLock};

#[derive(Debug)]
pub struct RwLockWriteGuard<'lock, T: AsFd> {
Expand Down Expand Up @@ -34,6 +34,6 @@ impl<T: AsFd> ops::DerefMut for RwLockWriteGuard<'_, T> {
impl<T: AsFd> Drop for RwLockWriteGuard<'_, T> {
#[inline]
fn drop(&mut self) {
let _ = flock(&self.lock.inner.as_fd(), FlockOperation::Unlock).ok();
let _ = compatible_unix_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok();
}
}
Loading