-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7654fdd
commit 940a8fb
Showing
6 changed files
with
54 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,25 @@ | ||
use std::marker::PhantomData; | ||
use std::ops::Deref; | ||
use rustix::{ | ||
fs::{flock, FlockOperation}, | ||
fd::AsFd, | ||
fd::AsFd | ||
}; | ||
|
||
pub struct FrwLockReadGuard<'a, T: AsFd> { | ||
pub(crate) data: *const T, | ||
pub(crate) _marker: PhantomData<&'a T>, // Ensuring lifetime and immutability semantics. | ||
pub struct FrwLockReadGuard<T: AsFd> { | ||
pub(crate) data: *const T | ||
} | ||
|
||
impl<T: AsFd> Deref for FrwLockReadGuard<'_, T> { | ||
impl<T: AsFd> Deref for FrwLockReadGuard<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { &*self.data } | ||
} | ||
} | ||
|
||
impl<T: AsFd> Drop for FrwLockReadGuard<'_, T> { | ||
impl<T: AsFd> Drop for FrwLockReadGuard<T> { | ||
fn drop(&mut self) { | ||
flock( | ||
unsafe { &*self.data }, | ||
FlockOperation::Unlock, | ||
).expect("Failed to unlock file"); | ||
unsafe { &*self.data }, | ||
FlockOperation::Unlock).expect("Failed to unlock file"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,31 @@ | ||
use std::marker::PhantomData; | ||
use std::ops::{Deref, DerefMut}; | ||
use rustix::{ | ||
fs::{flock, FlockOperation}, | ||
fd::AsFd, | ||
fd::AsFd | ||
}; | ||
|
||
pub struct FrwLockWriteGuard<'a, T: AsFd> { | ||
pub(crate) data: *mut T, | ||
pub(crate) _marker: PhantomData<&'a mut T>, // Ensuring lifetime and mutability semantics. | ||
pub struct FrwLockWriteGuard<T: AsFd> { | ||
pub(crate) data: *mut T | ||
} | ||
|
||
impl<T: AsFd> Deref for FrwLockWriteGuard<'_, T> { | ||
impl<T: AsFd> Deref for FrwLockWriteGuard<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
unsafe { &*self.data } | ||
} | ||
} | ||
|
||
impl<T: AsFd> DerefMut for FrwLockWriteGuard<'_, T> { | ||
impl<T: AsFd> DerefMut for FrwLockWriteGuard<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
unsafe { &mut *self.data } | ||
} | ||
} | ||
|
||
impl<T: AsFd> Drop for FrwLockWriteGuard<'_, T> { | ||
impl <T: AsFd> Drop for FrwLockWriteGuard<T> { | ||
fn drop(&mut self) { | ||
flock( | ||
unsafe { &*self.data }, | ||
FlockOperation::Unlock, | ||
).expect("Failed to unlock file"); | ||
FlockOperation::Unlock).expect("Failed to unlock file"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,23 @@ | ||
use std::ops::Deref; | ||
use tokio::sync::RwLockReadGuard; | ||
use rustix::fd::AsFd; | ||
use crate::frwlock::{FrwLock, FrwLockReadGuard, FrwLockError}; | ||
use crate::frwlock::{FrwLock, FrwLockReadGuard}; | ||
|
||
pub struct TfrwLockReadGuard<'a, T: AsFd> { | ||
_outer: RwLockReadGuard<'a, FrwLock<T>>, | ||
inner: Option<FrwLockReadGuard<'a, T>>, | ||
inner: FrwLockReadGuard<T>, | ||
} | ||
|
||
impl<'a, T: AsFd> TfrwLockReadGuard<'a, T> { | ||
pub(crate) fn new(mut outer: RwLockReadGuard<'a, FrwLock<T>>) -> Result<Self, FrwLockError> { | ||
let inner = outer.try_read().ok().map(|inner_guard| Self { | ||
_outer: outer, | ||
inner: Some(inner_guard), | ||
}); | ||
match inner { | ||
Some(guard) => Ok(guard), | ||
None => Err(FrwLockError::LockNotAvailable), | ||
} | ||
pub(crate) fn new(outer: RwLockReadGuard<'a, FrwLock<T>>, inner: FrwLockReadGuard<T>) -> Self { | ||
Self { _outer : outer, inner } | ||
} | ||
} | ||
|
||
impl<'a, T: AsFd> Deref for TfrwLockReadGuard<'a, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&*self.inner.as_ref().unwrap() | ||
&*self.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,29 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use tokio::sync::RwLockWriteGuard; | ||
use rustix::fd::AsFd; | ||
use crate::frwlock::{FrwLock, FrwLockWriteGuard, FrwLockError}; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
use crate::frwlock::{FrwLock, FrwLockWriteGuard}; | ||
|
||
pub struct TfrwLockWriteGuard<'a, T: AsFd> { | ||
_outer: RwLockWriteGuard<'a, FrwLock<T>>, | ||
inner: Option<FrwLockWriteGuard<'a, T>>, | ||
inner: FrwLockWriteGuard<T>, | ||
} | ||
|
||
impl<'a, T: AsFd> TfrwLockWriteGuard<'a, T> { | ||
pub(crate) fn new(mut outer: RwLockWriteGuard<'a, FrwLock<T>>) -> Result<Self, FrwLockError> { | ||
let inner = outer.try_write().ok().map(|inner_guard| Self { | ||
_outer: outer, | ||
inner: Some(inner_guard), | ||
}); | ||
match inner { | ||
Some(guard) => Ok(guard), | ||
None => Err(FrwLockError::LockNotAvailable), | ||
} | ||
pub(crate) fn new(outer: RwLockWriteGuard<'a, FrwLock<T>>, inner: FrwLockWriteGuard<T>) -> Self { | ||
Self { _outer : outer, inner } | ||
} | ||
} | ||
|
||
impl<'a, T: AsFd> Deref for TfrwLockWriteGuard<'a, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&*self.inner.as_ref().unwrap() | ||
&*self.inner | ||
} | ||
} | ||
|
||
impl<'a, T: AsFd> DerefMut for TfrwLockWriteGuard<'a, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut *self.inner.as_mut().unwrap() | ||
&mut *self.inner | ||
} | ||
} |