pub struct SpinLockObjectPool<T> { /* private fields */ }
Expand description

ObjectPool use a spin lock over vector to secure multithread access to pull.

The spin lock works like std::sync::Mutex but

cf wikipedia for more information.

§Example

 use lockfree_object_pool::SpinLockObjectPool;

 let pool = SpinLockObjectPool::<u32>::new(
   ||  Default::default(),
   |v| {
     *v = 0;
   }
 );
 let mut item = pool.pull();

 *item = 5;
 let work = *item * 5;

Implementations§

source§

impl<T> SpinLockObjectPool<T>

source

pub fn new<R, I>(init: I, reset: R) -> Self
where R: Fn(&mut T) + Send + Sync + 'static, I: Fn() -> T + Send + Sync + 'static,

Create an new SpinLockObjectPool

§Arguments
  • init closure to create new item
  • reset closure to reset item before reusage
§Example
 use lockfree_object_pool::SpinLockObjectPool;

 let pool = SpinLockObjectPool::<u32>::new(
   ||  Default::default(),
   |v| {
     *v = 0;
   }
 );
source

pub fn pull(&self) -> SpinLockReusable<'_, T>

Create a new element. When the element is dropped, it returns in the pull.

§Example
 use lockfree_object_pool::SpinLockObjectPool;

 let pool = SpinLockObjectPool::<u32>::new(
   ||  Default::default(),
   |v| {
     *v = 0;
   }
 );
 let mut item = pool.pull();
source

pub fn pull_owned(self: &Arc<Self>) -> SpinLockOwnedReusable<T>

Create a new element. When the element is dropped, it returns in the pull.

§Example
 use lockfree_object_pool::SpinLockObjectPool;
 use std::sync::Arc;

 let pool = Arc::new(SpinLockObjectPool::<u32>::new(
   ||  Default::default(),
   |v| {
     *v = 0;
   }
 ));
 let mut item = pool.pull_owned();

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for SpinLockObjectPool<T>

§

impl<T> Send for SpinLockObjectPool<T>
where T: Send,

§

impl<T> Sync for SpinLockObjectPool<T>
where T: Send,

§

impl<T> Unpin for SpinLockObjectPool<T>
where T: Unpin,

§

impl<T> !UnwindSafe for SpinLockObjectPool<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.