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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::sync::{ Arc, atomic::AtomicPtr, atomic::Ordering, atomic::AtomicUsize };
use std::ptr::null_mut;
use std::ops::{ Deref, DerefMut };
use std::fmt::{ Debug, Formatter, Result as FmtResult };

pub trait Poolable<T>: Send + Sync {
    fn new(context: &T) -> Self;

    fn reset(&mut self) -> bool; // true if still valid
}

pub struct PoolGuard<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> {
    // we are keeping the entire ItemNode here to prolong the lifetime outside of the `get` function.
    data: Option<Box<ItemNode<T>>>,
    pool: Arc<Pool<Y, T>>,
}

impl<Y: Send + Sync + Debug + 'static, T: Poolable<Y> + Debug + 'static> Debug for PoolGuard<Y, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        let result = self.data.as_ref().map(|item| item.item.fmt(f));
        match result {
            Some(x) => x,
            None => write!(f, "expired pool guard"),
        }
    }
}

impl<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> Drop for PoolGuard<Y, T> {
    fn drop(&mut self) {
        self.pool.readd_node(self.data.take().unwrap().item);
    }
}

impl<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> Deref for PoolGuard<Y, T> {
    type Target = T;

    fn deref(&self) -> &T {
        return &self.data.as_ref().unwrap().item;
    }
}

impl<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> DerefMut for PoolGuard<Y, T> {

    fn deref_mut(&mut self) -> &mut Self::Target {
        return &mut self.data.as_mut().unwrap().item;
    }
}

pub enum PoolScaleMode {
    Static { count: usize },
    AutoScale { maximum: Option<usize>, initial: usize, chunk_size: usize }, // chunk_size = 0 for 2^n
}

struct ItemNode<T> {
    item: T,
    next: *mut ItemNode<T>,
}

pub struct Pool<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> {
    scale_mode: PoolScaleMode,
    items: AtomicPtr<ItemNode<T>>,
    count: AtomicUsize,
    capacity: AtomicUsize,
    context: Y,
}

impl<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> Drop for Pool<Y, T> {
    fn drop(&mut self) {
        // at this point, no guards should be alive as they have references to Pool
        let mut items = self.items.swap(null_mut(), Ordering::Relaxed);
        while !items.is_null() {
            let next_items = unsafe { items.as_ref().unwrap() }.next;
            drop(unsafe { Box::from_raw(items) });
            items = next_items;
        }
    }
}

impl<Y: Send + Sync + 'static, T: Poolable<Y> + 'static> Pool<Y, T> {
    pub fn new(scale_mode: PoolScaleMode, context: Y) -> Arc<Pool<Y, T>> {
        let pool = Arc::new(Pool {
            scale_mode,
            items: AtomicPtr::default(),
            count: AtomicUsize::new(0),
            capacity: AtomicUsize::new(0),
            context,
        });
        pool.init_pool();
        pool
    }

    fn init_pool(&self) {
        match &self.scale_mode {
            PoolScaleMode::Static { count } | PoolScaleMode::AutoScale { initial: count, .. } => {
                for _ in 0..*count {
                    self.capacity.fetch_add(1, Ordering::Acquire);
                    self.add_node(T::new(&self.context));
                }
            },
        }
    }

    fn readd_node(&self, mut item: T) {
        if !item.reset() {
            match self.scale_mode {
                PoolScaleMode::Static { .. } => {
                    self.add_node(T::new(&self.context));
                },
                _ => (),
            }
            return;
        }
        self.add_node(item);
    }

    fn add_node(&self, item: T) {
        let item_node = Box::into_raw(Box::new(ItemNode {
            item,
            next: null_mut(),
        }));
        self.count.fetch_add(1, Ordering::Acquire);
        loop {
            let present_node = self.items.load(Ordering::Acquire);
            unsafe { item_node.as_mut() }.unwrap().next = present_node;
            if self.items.compare_and_swap(present_node, item_node, Ordering::AcqRel) == present_node {
                break;
            }
        }
    }

    pub fn get(self: &Arc<Pool<Y, T>>) -> Option<PoolGuard<Y, T>> {
        loop {
            let present_node = self.items.load(Ordering::Acquire);
            if present_node.is_null() {
                match self.scale_mode {
                    PoolScaleMode::Static { .. } => {
                        // nothing we can do to get more right now
                    },
                    PoolScaleMode::AutoScale { maximum, chunk_size, .. } => {
                        let capacity = self.capacity.load(Ordering::Acquire);
                        if maximum.is_none() || capacity < maximum.unwrap() {
                            let new_capacity = capacity + if chunk_size == 0 {
                                if capacity == 0 {
                                    1
                                } else {
                                    capacity
                                }
                            } else {
                                chunk_size
                            };
                            let new_capacity = if maximum.is_some() && new_capacity > maximum.unwrap() {
                                maximum.unwrap()
                            } else {
                                new_capacity
                            };
                            while self.capacity.load(Ordering::Acquire) < new_capacity {
                                self.capacity.fetch_add(1, Ordering::Release);
                                self.add_node(T::new(&self.context));
                            }
                            continue;
                        } else {
                            // already at capacity
                        }
                    },
                }
                return None;
            }
            let present_node_ref = unsafe { present_node.as_mut() }.unwrap();
            if self.items.compare_and_swap(present_node, present_node_ref.next, Ordering::AcqRel) == present_node {
                let present_node_ref = unsafe { Box::from_raw(present_node) }; // take ownership / enforce we drop
                self.count.fetch_sub(1, Ordering::Release);
                let guard = PoolGuard {
                    data: Some(present_node_ref),
                    pool: self.clone(),
                };
                return Some(guard);
            }
        }
        
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::thread;

    #[derive(Debug)]
    struct TestContext {
        test: &'static str,
    }

    #[derive(Debug)]
    struct TestItem {
        test: String,
    }

    impl Poolable<TestContext> for TestItem {
        fn new(context: &TestContext) -> TestItem {
            TestItem {
                test: format!("{}_{}", context.test, "testing item"),
            }
        }

        fn reset(&mut self) -> bool {
            return true;
        }
    }

    #[test]
    fn test_creation() {
        let _: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::Static { count: 10 }, TestContext { test: "testing context" });
    }

    #[test]
    fn test_get() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::Static { count: 10 }, TestContext { test: "testing context" });
        for _ in 0..10 {
            let item = pool.get().expect("didn't find another item in pool");
            assert_eq!(item.test, "testing context_testing item");
            std::mem::forget(item);
        }
        assert!(pool.get().is_none());
    }

    #[test]
    fn test_get_refreshed() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::Static { count: 10 }, TestContext { test: "testing context" });
        for _ in 0..1000 {
            let item = pool.get().expect("didn't find another item in pool");
            assert_eq!(item.test, "testing context_testing item");
        }
        assert!(pool.get().is_some());
    }

    #[test]
    fn test_grow() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::AutoScale { maximum: None, initial: 0, chunk_size: 1 }, TestContext { test: "testing context" });
        for _ in 0..100 {
            let item = pool.get().expect("didn't find another item in pool");
            assert_eq!(item.test, "testing context_testing item");
            std::mem::forget(item);
        }
        assert_eq!(pool.capacity.load(Ordering::Relaxed), 100);
        assert!(pool.get().is_some());
    }

    #[test]
    fn test_grow_exponential() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::AutoScale { maximum: None, initial: 0, chunk_size: 0 }, TestContext { test: "testing context" });
        for _ in 0..100 {
            let item = pool.get().expect("didn't find another item in pool");
            assert_eq!(item.test, "testing context_testing item");
            std::mem::forget(item);
        }
        assert_eq!(pool.capacity.load(Ordering::Relaxed), 128);
        assert!(pool.get().is_some());
    }

    #[test]
    fn test_grow_capped() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::AutoScale { maximum: Some(10), initial: 0, chunk_size: 1 }, TestContext { test: "testing context" });
        for _ in 0..10 {
            let item = pool.get().expect("didn't find another item in pool");
            assert_eq!(item.test, "testing context_testing item");
            std::mem::forget(item);
        }
        assert_eq!(pool.capacity.load(Ordering::Relaxed), 10);
        assert!(pool.get().is_none());
    }

    #[test]
    fn test_race_readonly() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::Static { count: 1000 }, TestContext { test: "testing context" });
        let mut handles: Vec<thread::JoinHandle<_>> = vec![];
        for _ in 0..100 {
            let thread_pool = pool.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..10 {
                    let item = thread_pool.get().expect("didn't find another item in pool");
                    assert_eq!(item.test, "testing context_testing item");
                    std::mem::forget(item);
                }        
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }
        assert_eq!(pool.capacity.load(Ordering::Relaxed), 1000);
        assert_eq!(pool.count.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn test_race_read_return() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::Static { count: 1000 }, TestContext { test: "testing context" });
        let mut handles: Vec<thread::JoinHandle<_>> = vec![];
        for _ in 0..100 {
            let thread_pool = pool.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..10 {
                    let item = thread_pool.get().expect("didn't find another item in pool");
                    assert_eq!(item.test, "testing context_testing item");
                }        
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }
        assert_eq!(pool.capacity.load(Ordering::Relaxed), 1000);
        assert_eq!(pool.count.load(Ordering::Relaxed), 1000);
    }

    #[test]
    fn test_race_read_grow() {
        let pool: Arc<Pool<TestContext, TestItem>> = Pool::new(PoolScaleMode::AutoScale { maximum: None, initial: 0, chunk_size: 1 }, TestContext { test: "testing context" });
        let mut handles: Vec<thread::JoinHandle<_>> = vec![];
        for _ in 0..1000 {
            let thread_pool = pool.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..110 {
                    let item = thread_pool.get().expect("didn't find another item in pool");
                    assert_eq!(item.test, "testing context_testing item");
                    std::mem::forget(item);
                }        
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }
        assert_eq!(pool.count.load(Ordering::Relaxed), 0);
        assert!(pool.capacity.load(Ordering::Relaxed) >= 110000); // 1100+ due to racing creation vs counting, which is not a problem.
    }
}