From ac98190782e7be1004d21a39f8cd71dd1b26d147 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 13 Dec 2023 20:30:06 +0300 Subject: [PATCH 1/2] support new target(solaris) with conditional compilation Signed-off-by: onur-ozkan --- src/sys/unix/read_guard.rs | 7 ++++-- src/sys/unix/rw_lock.rs | 44 +++++++++++++++++++++++++++---------- src/sys/unix/write_guard.rs | 7 ++++-- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/sys/unix/read_guard.rs b/src/sys/unix/read_guard.rs index 972bb56..0eb0f4e 100644 --- a/src/sys/unix/read_guard.rs +++ b/src/sys/unix/read_guard.rs @@ -1,5 +1,5 @@ use rustix::fd::AsFd; -use rustix::fs::{flock, FlockOperation}; +use rustix::fs::FlockOperation; use std::ops; use super::RwLock; @@ -27,6 +27,9 @@ impl ops::Deref for RwLockReadGuard<'_, T> { impl Drop for RwLockReadGuard<'_, T> { #[inline] fn drop(&mut self) { - let _ = flock(&self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + #[cfg(not(target_os = "solaris"))] + let _ = rustix::fs::flock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + #[cfg(target_os = "solaris")] + let _ = rustix::fs::fcntl_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); } } diff --git a/src/sys/unix/rw_lock.rs b/src/sys/unix/rw_lock.rs index 992925e..63e11d6 100644 --- a/src/sys/unix/rw_lock.rs +++ b/src/sys/unix/rw_lock.rs @@ -1,5 +1,5 @@ use rustix::fd::AsFd; -use rustix::fs::{flock, FlockOperation}; +use rustix::fs::FlockOperation; use std::io::{self, Error, ErrorKind}; use super::{RwLockReadGuard, RwLockWriteGuard}; @@ -17,32 +17,52 @@ impl RwLock { #[inline] pub fn write(&mut self) -> io::Result> { - flock(&self.inner.as_fd(), FlockOperation::LockExclusive)?; + #[cfg(not(target_os = "solaris"))] + rustix::fs::flock(self.inner.as_fd(), FlockOperation::LockExclusive)?; + #[cfg(target_os = "solaris")] + rustix::fs::fcntl_lock(self.inner.as_fd(), FlockOperation::LockExclusive)?; + Ok(RwLockWriteGuard::new(self)) } #[inline] pub fn try_write(&mut self) -> Result, Error> { - flock( - &self.inner.as_fd(), - FlockOperation::NonBlockingLockExclusive, - ) - .map_err(|err| match err.kind() { - ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), - _ => Error::from(err), - })?; + #[cfg(not(target_os = "solaris"))] + rustix::fs::flock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive).map_err( + |err| match err.kind() { + ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), + _ => Error::from(err), + }, + )?; + #[cfg(target_os = "solaris")] + rustix::fs::fcntl_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> { - flock(&self.inner.as_fd(), FlockOperation::LockShared)?; + #[cfg(not(target_os = "solaris"))] + rustix::fs::flock(self.inner.as_fd(), FlockOperation::LockShared)?; + #[cfg(target_os = "solaris")] + rustix::fs::fcntl_lock(self.inner.as_fd(), FlockOperation::LockShared)?; Ok(RwLockReadGuard::new(self)) } #[inline] pub fn try_read(&self) -> Result, Error> { - flock(&self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err( + #[cfg(not(target_os = "solaris"))] + rustix::fs::flock(self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err( + |err| match err.kind() { + ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), + _ => Error::from(err), + }, + )?; + #[cfg(target_os = "solaris")] + rustix::fs::fcntl_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err( |err| match err.kind() { ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), _ => Error::from(err), diff --git a/src/sys/unix/write_guard.rs b/src/sys/unix/write_guard.rs index 1bdc1a4..1896c38 100644 --- a/src/sys/unix/write_guard.rs +++ b/src/sys/unix/write_guard.rs @@ -1,5 +1,5 @@ use rustix::fd::AsFd; -use rustix::fs::{flock, FlockOperation}; +use rustix::fs::FlockOperation; use std::ops; use super::RwLock; @@ -34,6 +34,9 @@ impl ops::DerefMut for RwLockWriteGuard<'_, T> { impl Drop for RwLockWriteGuard<'_, T> { #[inline] fn drop(&mut self) { - let _ = flock(&self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + #[cfg(not(target_os = "solaris"))] + let _ = rustix::fs::flock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + #[cfg(target_os = "solaris")] + let _ = rustix::fs::fcntl_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); } } From ee2abc0a032ed963341c2764b0c846405a021848 Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 20 Dec 2023 14:20:36 +0300 Subject: [PATCH 2/2] create compatible_unix_lock Signed-off-by: onur-ozkan --- src/sys/unix/mod.rs | 13 +++++++++++++ src/sys/unix/read_guard.rs | 7 ++----- src/sys/unix/rw_lock.rs | 33 +++++---------------------------- src/sys/unix/write_guard.rs | 7 ++----- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/sys/unix/mod.rs b/src/sys/unix/mod.rs index 023e691..12544dd 100644 --- a/src/sys/unix/mod.rs +++ b/src/sys/unix/mod.rs @@ -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: 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); +} diff --git a/src/sys/unix/read_guard.rs b/src/sys/unix/read_guard.rs index 0eb0f4e..2e58366 100644 --- a/src/sys/unix/read_guard.rs +++ b/src/sys/unix/read_guard.rs @@ -2,7 +2,7 @@ use rustix::fd::AsFd; use rustix::fs::FlockOperation; use std::ops; -use super::RwLock; +use super::{compatible_unix_lock, RwLock}; #[derive(Debug)] pub struct RwLockReadGuard<'lock, T: AsFd> { @@ -27,9 +27,6 @@ impl ops::Deref for RwLockReadGuard<'_, T> { impl Drop for RwLockReadGuard<'_, T> { #[inline] fn drop(&mut self) { - #[cfg(not(target_os = "solaris"))] - let _ = rustix::fs::flock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); - #[cfg(target_os = "solaris")] - let _ = rustix::fs::fcntl_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + let _ = compatible_unix_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); } } diff --git a/src/sys/unix/rw_lock.rs b/src/sys/unix/rw_lock.rs index 63e11d6..e10b55c 100644 --- a/src/sys/unix/rw_lock.rs +++ b/src/sys/unix/rw_lock.rs @@ -2,7 +2,7 @@ use rustix::fd::AsFd; 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 { @@ -17,25 +17,13 @@ impl RwLock { #[inline] pub fn write(&mut self) -> io::Result> { - #[cfg(not(target_os = "solaris"))] - rustix::fs::flock(self.inner.as_fd(), FlockOperation::LockExclusive)?; - #[cfg(target_os = "solaris")] - rustix::fs::fcntl_lock(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, Error> { - #[cfg(not(target_os = "solaris"))] - rustix::fs::flock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive).map_err( - |err| match err.kind() { - ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), - _ => Error::from(err), - }, - )?; - #[cfg(target_os = "solaris")] - rustix::fs::fcntl_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive) + compatible_unix_lock(self.inner.as_fd(), FlockOperation::NonBlockingLockExclusive) .map_err(|err| match err.kind() { ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), _ => Error::from(err), @@ -45,24 +33,13 @@ impl RwLock { #[inline] pub fn read(&self) -> io::Result> { - #[cfg(not(target_os = "solaris"))] - rustix::fs::flock(self.inner.as_fd(), FlockOperation::LockShared)?; - #[cfg(target_os = "solaris")] - rustix::fs::fcntl_lock(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, Error> { - #[cfg(not(target_os = "solaris"))] - rustix::fs::flock(self.inner.as_fd(), FlockOperation::NonBlockingLockShared).map_err( - |err| match err.kind() { - ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(), - _ => Error::from(err), - }, - )?; - #[cfg(target_os = "solaris")] - rustix::fs::fcntl_lock(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), diff --git a/src/sys/unix/write_guard.rs b/src/sys/unix/write_guard.rs index 1896c38..7e61034 100644 --- a/src/sys/unix/write_guard.rs +++ b/src/sys/unix/write_guard.rs @@ -2,7 +2,7 @@ use rustix::fd::AsFd; use rustix::fs::FlockOperation; use std::ops; -use super::RwLock; +use super::{compatible_unix_lock, RwLock}; #[derive(Debug)] pub struct RwLockWriteGuard<'lock, T: AsFd> { @@ -34,9 +34,6 @@ impl ops::DerefMut for RwLockWriteGuard<'_, T> { impl Drop for RwLockWriteGuard<'_, T> { #[inline] fn drop(&mut self) { - #[cfg(not(target_os = "solaris"))] - let _ = rustix::fs::flock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); - #[cfg(target_os = "solaris")] - let _ = rustix::fs::fcntl_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); + let _ = compatible_unix_lock(self.lock.inner.as_fd(), FlockOperation::Unlock).ok(); } }