Skip to content
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
37 changes: 20 additions & 17 deletions src/buf/fixed/pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::handle::CheckedOutBuf;
use super::{FixedBuf, FixedBuffers};

use crate::runtime::driver::WeakHandle;
use crate::runtime::CONTEXT;
use libc::{iovec, UIO_MAXIOV};
use std::cell::RefCell;
Expand Down Expand Up @@ -84,7 +83,6 @@ use std::slice;
#[derive(Clone)]
pub struct FixedBufPool {
inner: Rc<RefCell<Inner>>,
driver: WeakHandle,
}

impl FixedBufPool {
Expand Down Expand Up @@ -116,10 +114,11 @@ impl FixedBufPool {
/// # let (memlock_limit, _) = getrlimit(Resource::RLIMIT_MEMLOCK)?;
/// # let NUM_BUFFERS = std::cmp::max(memlock_limit as usize / 4096 / 8, 1);
/// # let BUF_SIZE = 4096;
/// let pool = FixedBufPool::new(
/// iter::repeat(Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
///
/// tokio_uring::start(async {
/// let pool = FixedBufPool::new(
/// iter::repeat(Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
/// pool.register()?;
/// // ...
/// Ok(())
Expand All @@ -139,10 +138,11 @@ impl FixedBufPool {
/// # let (memlock_limit, _) = getrlimit(Resource::RLIMIT_MEMLOCK)?;
/// # let NUM_BUFFERS = std::cmp::max(memlock_limit as usize / 4096 / 8, 1);
/// # let BUF_SIZE = 4096;
/// let pool = FixedBufPool::new(
/// iter::repeat_with(|| Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
///
/// tokio_uring::start(async {
/// let pool = FixedBufPool::new(
/// iter::repeat_with(|| Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
/// pool.register()?;
/// // ...
/// Ok(())
Expand All @@ -152,7 +152,6 @@ impl FixedBufPool {
pub fn new(bufs: impl IntoIterator<Item = Vec<u8>>) -> Self {
FixedBufPool {
inner: Rc::new(RefCell::new(Inner::new(bufs.into_iter()))),
driver: CONTEXT.with(|x| x.weak().expect("Not in a runtime context")),
}
}

Expand All @@ -176,10 +175,12 @@ impl FixedBufPool {
/// of the `tokio-uring` runtime this call is made in, the function returns
/// an error.
pub fn register(&self) -> io::Result<()> {
self.driver
.upgrade()
.expect("Runtime context is no longer present")
.register_buffers(Rc::clone(&self.inner) as _)
CONTEXT.with(|x| {
x.handle()
.as_ref()
.expect("Not in a runtime context")
.register_buffers(Rc::clone(&self.inner) as _)
})
}

/// Unregisters this collection of buffers.
Expand All @@ -198,10 +199,12 @@ impl FixedBufPool {
/// an error. Calling `unregister` when no `FixedBufPool` is currently
/// registered on this runtime also returns an error.
pub fn unregister(&self) -> io::Result<()> {
self.driver
.upgrade()
.expect("Runtime context is no longer present")
.unregister_buffers(Rc::clone(&self.inner) as _)
CONTEXT.with(|x| {
x.handle()
.as_ref()
.expect("Not in a runtime context")
.unregister_buffers(Rc::clone(&self.inner) as _)
})
}

/// Returns a buffer of requested capacity from this pool
Expand Down
30 changes: 16 additions & 14 deletions src/buf/fixed/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::handle::CheckedOutBuf;
use super::{FixedBuf, FixedBuffers};

use crate::runtime::driver::WeakHandle;
use crate::runtime::CONTEXT;
use libc::{iovec, UIO_MAXIOV};
use std::cell::RefCell;
Expand Down Expand Up @@ -37,7 +36,6 @@ use std::slice;
#[derive(Clone)]
pub struct FixedBufRegistry {
inner: Rc<RefCell<Inner>>,
driver: WeakHandle,
}

impl FixedBufRegistry {
Expand Down Expand Up @@ -93,10 +91,11 @@ impl FixedBufRegistry {
/// # let (memlock_limit, _) = getrlimit(Resource::RLIMIT_MEMLOCK)?;
/// # let NUM_BUFFERS = std::cmp::max(memlock_limit as usize / 4096 / 8, 1);
/// # let BUF_SIZE = 4096;
/// let registry = FixedBufRegistry::new(
/// iter::repeat_with(|| Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
///
/// tokio_uring::start(async {
/// let registry = FixedBufRegistry::new(
/// iter::repeat_with(|| Vec::with_capacity(BUF_SIZE)).take(NUM_BUFFERS)
/// );
/// registry.register()?;
/// // ...
/// Ok(())
Expand All @@ -106,7 +105,6 @@ impl FixedBufRegistry {
pub fn new(bufs: impl IntoIterator<Item = Vec<u8>>) -> Self {
FixedBufRegistry {
inner: Rc::new(RefCell::new(Inner::new(bufs.into_iter()))),
driver: CONTEXT.with(|x| x.weak().expect("Not in a runtime context")),
}
}

Expand All @@ -130,10 +128,12 @@ impl FixedBufRegistry {
/// of the `tokio-uring` runtime this call is made in, the function returns
/// an error.
pub fn register(&self) -> io::Result<()> {
self.driver
.upgrade()
.expect("Runtime context is no longer present")
.register_buffers(Rc::clone(&self.inner) as _)
CONTEXT.with(|x| {
x.handle()
.as_ref()
.expect("Not in a runtime context")
.register_buffers(Rc::clone(&self.inner) as _)
})
}

/// Unregisters this collection of buffers.
Expand All @@ -152,10 +152,12 @@ impl FixedBufRegistry {
/// an error. Calling `unregister` when no `FixedBufRegistry` is currently
/// registered on this runtime also returns an error.
pub fn unregister(&self) -> io::Result<()> {
self.driver
.upgrade()
.expect("Runtime context is no longer present")
.unregister_buffers(Rc::clone(&self.inner) as _)
CONTEXT.with(|x| {
x.handle()
.as_ref()
.expect("Not in a runtime context")
.unregister_buffers(Rc::clone(&self.inner) as _)
})
}

/// Returns a buffer identified by the specified index for use by the
Expand Down
1 change: 1 addition & 0 deletions src/runtime/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl RuntimeContext {
self.driver.borrow().clone()
}

#[allow(dead_code)]
pub(crate) fn weak(&self) -> Option<WeakHandle> {
self.driver.borrow().as_ref().map(Into::into)
}
Expand Down