Skip to content

Commit

Permalink
Target support: solaris (#48)
Browse files Browse the repository at this point in the history
* support new target(solaris) with conditional compilation

Signed-off-by: onur-ozkan <[email protected]>

* create compatible_unix_lock

Signed-off-by: onur-ozkan <[email protected]>

---------

Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan authored Dec 29, 2023
1 parent 01d5964 commit 195a95a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
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();
}
}

0 comments on commit 195a95a

Please sign in to comment.