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
use crate::execution_manager::async_channel::AsyncChannel;
use std::cmp::max;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

pub trait PoolObjectTrait: Send + Sync + 'static {
    type InitData: Clone + Sync + Send;

    fn allocate_new(init_data: &Self::InitData) -> Self;
    fn reset(&mut self);
}

impl<T: PoolObjectTrait> PoolObjectTrait for Box<T> {
    type InitData = T::InitData;

    fn allocate_new(init_data: &Self::InitData) -> Self {
        Box::new(T::allocate_new(init_data))
    }
    fn reset(&mut self) {
        T::reset(self);
    }
}

pub struct ObjectsPool<T: Sync + Send + 'static> {
    queue: AsyncChannel<T>,
    pub(crate) returner: Arc<(AsyncChannel<T>, AtomicU64)>,
    allocate_fn: Box<dyn (Fn() -> T) + Sync + Send>,
    max_count: u64,
    temp_max_count: AtomicU64,
}

pub trait PoolReturner<T: Send + Sync>: Send + Sync {
    fn return_element(&self, el: T);
}

impl<T: PoolObjectTrait> PoolReturner<T> for (AsyncChannel<T>, AtomicU64) {
    fn return_element(&self, mut el: T) {
        self.1.fetch_sub(1, Ordering::Relaxed);
        el.reset();
        self.0.send(el, true);
    }
}

impl<T: PoolObjectTrait> ObjectsPool<T> {
    pub fn new(cap: usize, init_data: T::InitData) -> Self {
        let channel = AsyncChannel::new(cap);

        Self {
            queue: channel.clone(),
            returner: Arc::new((channel, AtomicU64::new(0))),
            allocate_fn: Box::new(move || T::allocate_new(&init_data)),
            max_count: cap as u64,
            temp_max_count: AtomicU64::new(0),
        }
    }

    pub fn set_size(&self, new_size: usize) {
        self.temp_max_count
            .store(new_size as u64, Ordering::Relaxed);
    }

    #[inline(always)]
    async fn alloc_wait(&self) -> Result<T, ()> {
        self.queue.recv().await
    }

    #[inline(always)]
    fn alloc_wait_blocking(&self) -> Result<T, ()> {
        self.queue.recv_blocking()
    }

    pub async fn alloc_object(&self) -> PoolObject<T> {
        let el_count = self.returner.1.fetch_add(1, Ordering::Relaxed);

        if el_count >= max(self.max_count, self.temp_max_count.load(Ordering::Relaxed)) {
            return PoolObject::from_element(self.alloc_wait().await.unwrap(), self);
        }

        match self.queue.try_recv() {
            Some(el) => PoolObject::from_element(el, self),
            None => PoolObject::from_element((self.allocate_fn)(), self),
        }
    }

    pub fn alloc_object_blocking(&self) -> PoolObject<T> {
        let el_count = self.returner.1.fetch_add(1, Ordering::Relaxed);

        if el_count >= max(self.max_count, self.temp_max_count.load(Ordering::Relaxed)) {
            return PoolObject::from_element(self.alloc_wait_blocking().unwrap(), self);
        }

        match self.queue.try_recv() {
            Some(el) => PoolObject::from_element(el, self),
            None => PoolObject::from_element((self.allocate_fn)(), self),
        }
    }

    pub fn alloc_object_force(&self) -> PoolObject<T> {
        self.returner.1.fetch_add(1, Ordering::Relaxed);
        match self.queue.try_recv() {
            Some(el) => PoolObject::from_element(el, self),
            None => PoolObject::from_element((self.allocate_fn)(), self),
        }
    }

    pub fn get_available_items(&self) -> i64 {
        (max(self.max_count, self.temp_max_count.load(Ordering::Relaxed)) as i64)
            - (self.returner.1.load(Ordering::Relaxed) as i64)
    }

    pub fn get_allocated_items(&self) -> i64 {
        self.returner.1.load(Ordering::Relaxed) as i64
    }

    // pub fn wait_for_item_timeout(&self, timeout: Duration) {
    //     if let Ok(recv) = self.queue.recv_timeout(timeout) {
    //         let _ = self.returner.0.try_send(recv);
    //     }
    // }
}

pub struct PoolObject<T: Send + Sync> {
    pub(crate) value: ManuallyDrop<T>,
    pub(crate) returner: Option<Arc<dyn PoolReturner<T>>>,
}

impl<T: PoolObjectTrait> PoolObject<T> {
    fn from_element(value: T, pool: &ObjectsPool<T>) -> Self {
        Self {
            value: ManuallyDrop::new(value),
            returner: Some(pool.returner.clone()),
        }
    }
}

impl<T: Send + Sync> PoolObject<T> {
    pub fn new_simple(value: T) -> Self {
        Self {
            value: ManuallyDrop::new(value),
            returner: None,
        }
    }
}

impl<T: Send + Sync> Deref for PoolObject<T> {
    type Target = T;
    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        self.value.deref()
    }
}

impl<T: Send + Sync> DerefMut for PoolObject<T> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value.deref_mut()
    }
}

impl<T: Send + Sync> Drop for PoolObject<T> {
    fn drop(&mut self) {
        if let Some(returner) = &self.returner {
            returner.return_element(unsafe { ManuallyDrop::take(&mut self.value) });
        } else {
            unsafe { ManuallyDrop::drop(&mut self.value) }
        }
    }
}