Skip to content

Commit

Permalink
feat: support cursor for lmr (datenlord#114)
Browse files Browse the repository at this point in the history
* add cursor for lmr

* add a little example

* Update comment
  • Loading branch information
my-vegetable-has-exploded committed Sep 8, 2023
1 parent 4daf1ad commit 6033ac2
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/memory_region/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sealed::sealed;
use std::{
alloc::{dealloc, Layout},
fmt::Debug,
io::Cursor,
ops::Range,
slice,
sync::Arc,
Expand Down Expand Up @@ -91,6 +92,37 @@ pub unsafe trait LocalMrReadAccess: MrAccess {
)
}

/// Get the memory region as slice until it is readable warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// If this mr is being used in RDMA ops, the thread may be blocked
#[inline]
#[allow(clippy::as_conversions)]
fn as_slice_cursor(&self) -> MappedRwLockReadGuard<Cursor<&[u8]>> {
// SAFETY: memory of this mr should have been initialized
MappedRwLockReadGuard::map(self.as_ptr(), |ptr| unsafe {
Cursor::new(slice::from_raw_parts(ptr, self.length()))
})
}

/// Try to get the memory region as slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// Return `None` if this mr is being used in RDMA ops without blocking thread
#[allow(clippy::as_conversions)]
#[inline]
fn try_as_slice_cursor(&self) -> Option<MappedRwLockReadGuard<Cursor<&[u8]>>> {
self.try_as_ptr().map_or_else(
|| None,
|guard| {
// SAFETY: memory of this mr should have been initialized
return Some(MappedRwLockReadGuard::map(guard, |ptr| unsafe {
Cursor::new(slice::from_raw_parts(ptr, self.length()))
}));
},
)
}

/// Get the memory region as slice without lock
///
/// # Safety
Expand Down Expand Up @@ -275,6 +307,38 @@ pub unsafe trait LocalMrWriteAccess: MrAccess + LocalMrReadAccess {
)
}

/// Try to get the memory region as mutable slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// If this mr is being used in RDMA ops, the thread may be blocked
#[inline]
#[allow(clippy::as_conversions)]
fn as_mut_slice_cursor(&mut self) -> MappedRwLockWriteGuard<Cursor<&mut [u8]>> {
let len = self.length();
// SAFETY: memory of this mr should have been initialized
MappedRwLockWriteGuard::map(self.as_mut_ptr(), |ptr| unsafe {
Cursor::new(slice::from_raw_parts_mut(ptr, len))
})
}

/// Try to get the memory region as mutable slice warppered with cursor
/// user can use cursor to read or write data without maintaining the index manually
///
/// Return `None` if this mr is being used in RDMA ops without blocking thread
#[inline]
#[allow(clippy::as_conversions)]
fn try_as_mut_slice_cursor(&mut self) -> Option<MappedRwLockWriteGuard<Cursor<&mut [u8]>>> {
self.try_as_mut_ptr().map_or_else(
|| None,
|guard| {
// SAFETY: memory of this mr should have been initialized
return Some(MappedRwLockWriteGuard::map(guard, |ptr| unsafe {
Cursor::new(slice::from_raw_parts_mut(ptr, self.length()))
}));
},
)
}

/// Get the memory region as mut slice without lock
///
/// # Safety
Expand Down

0 comments on commit 6033ac2

Please sign in to comment.