concurrent_pool/lib.rs
1//! A concurrent object pool.
2//!
3//! # Features
4//!
5//! - Configurable capacity and preallocation.
6//! - Thread-safe: Multiple threads can pull and recycle items concurrently.
7//! - Automatic reclamation of unused item when the continuous occurrence
8//! of `surplus-pull` reaches a certain threshold if `auto_reclaim` is enabled.
9//!
10//! # `surplus-pull`
11//!
12//! After pulling data from the memory pool, available allocated entities in the memory
13//! pool are exceed a certain threshold. We call this pull is a `surplus-pull`.
14//!
15//! # Examples
16//!
17//! ## Local memory pool
18//!
19//! ```rust
20//! use concurrent_pool::Pool;
21//!
22//! let pool: Pool<u32> = Pool::with_capacity(10);
23//! assert_eq!(pool.available(), 10);
24//! let item = pool.pull().unwrap();
25//! assert_eq!(*item, 0);
26//! assert_eq!(pool.available(), 9);
27//! let item_clone = item.clone();
28//! drop(item);
29//! assert_eq!(pool.available(), 9);
30//! drop(item_clone);
31//! assert_eq!(pool.available(), 10);
32//! ```
33//!
34//! ## Multiple threads shared memory pool
35//!
36//! ```rust
37//! use concurrent_pool::Pool;
38//! use std::sync::{Arc, mpsc};
39//!
40//! let pool: Arc<Pool<u32>> = Arc::new(Pool::with_capacity(10));
41//!
42//! let (tx, rx) = mpsc::channel();
43//! let clone_pool = pool.clone();
44//! let tx1 = tx.clone();
45//! let sender1 = std::thread::spawn(move || {
46//! let item = clone_pool.pull_owned_with(|x| *x = 1).unwrap();
47//! tx1.send((1, item)).unwrap();
48//! });
49//!
50//! let clone_pool = pool.clone();
51//! let sender2 = std::thread::spawn(move || {
52//! let item = clone_pool.pull_owned_with(|x| *x = 2).unwrap();
53//! tx.send((2, item)).unwrap();
54//! });
55//!
56//! let receiver = std::thread::spawn(move || {
57//! for _ in 0..2 {
58//! let (id, item) = rx.recv().unwrap();
59//! if id == 1 {
60//! assert_eq!(*item, 1);
61//! } else {
62//! assert_eq!(*item, 2);
63//! }
64//! }
65//! });
66//!
67//! sender1.join().unwrap();
68//! sender2.join().unwrap();
69//! receiver.join().unwrap();
70//! ```
71
72mod builder;
73mod entry;
74mod pool;
75
76pub use builder::Builder;
77pub use entry::{Entry, OwnedEntry};
78pub use pool::{Config, Pool};