1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::{
    spin_lock::SpinLock, spin_lock_owned_reusable::SpinLockOwnedReusable,
    spin_lock_reusable::SpinLockReusable,
};
use std::mem::ManuallyDrop;
use std::sync::Arc;

/// ObjectPool use a spin lock over vector to secure multithread access to pull.
///
/// The spin lock works like [`std::sync::Mutex`] but
/// * use [`std::sync::atomic::AtomicBool`] for synchro
/// * active waiting
///
/// cf [wikipedia](https://en.wikipedia.org/wiki/Spinlock) for more information.
///
/// # Example
/// ```rust
///  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;
/// ```
pub struct SpinLockObjectPool<T> {
    objects: SpinLock<Vec<T>>,
    reset: Box<dyn Fn(&mut T) + Send + Sync>,
    init: Box<dyn Fn() -> T + Send + Sync>,
}

impl<T> SpinLockObjectPool<T> {
    ///
    /// Create an new [`SpinLockObjectPool`]
    ///
    /// # Arguments
    /// * `init`  closure to create new item
    /// * `reset` closure to reset item before reusage
    ///
    /// # Example
    /// ```rust
    ///  use lockfree_object_pool::SpinLockObjectPool;
    ///
    ///  let pool = SpinLockObjectPool::<u32>::new(
    ///    ||  Default::default(),
    ///    |v| {
    ///      *v = 0;
    ///    }
    ///  );
    /// ```
    #[inline]
    pub fn new<R, I>(init: I, reset: R) -> Self
    where
        R: Fn(&mut T) + Send + Sync + 'static,
        I: Fn() -> T + Send + Sync + 'static,
    {
        Self {
            objects: SpinLock::new(Vec::new()),
            reset: Box::new(reset),
            init: Box::new(init),
        }
    }

    ///
    /// Create a new element. When the element is dropped, it returns in the pull.
    ///
    /// # Example
    /// ```rust
    ///  use lockfree_object_pool::SpinLockObjectPool;
    ///
    ///  let pool = SpinLockObjectPool::<u32>::new(
    ///    ||  Default::default(),
    ///    |v| {
    ///      *v = 0;
    ///    }
    ///  );
    ///  let mut item = pool.pull();
    /// ```
    #[inline]
    pub fn pull(&self) -> SpinLockReusable<T> {
        SpinLockReusable::new(
            self,
            ManuallyDrop::new(self.objects.lock().pop().unwrap_or_else(&self.init)),
        )
    }

    ///
    /// Create a new element. When the element is dropped, it returns in the pull.
    ///
    /// # Example
    /// ```rust
    ///  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();
    /// ```
    #[inline]
    pub fn pull_owned(self: &Arc<Self>) -> SpinLockOwnedReusable<T> {
        SpinLockOwnedReusable::new(
            self.clone(),
            ManuallyDrop::new(self.objects.lock().pop().unwrap_or_else(&self.init)),
        )
    }

    #[inline]
    pub(crate) fn attach(&self, mut data: T) {
        (self.reset)(&mut data);
        self.objects.lock().push(data);
    }
}