|
| 1 | +use crate::common::now; |
| 2 | +use corosensei::stack::{DefaultStack, Stack, StackPointer}; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::cmp::Ordering; |
| 5 | +use std::collections::BinaryHeap; |
| 6 | +use std::ops::Deref; |
| 7 | +use std::rc::Rc; |
| 8 | +use std::sync::atomic::{AtomicU64, AtomicUsize}; |
| 9 | + |
| 10 | +pub(crate) struct PooledStack { |
| 11 | + stack: Rc<DefaultStack>, |
| 12 | + create_time: u64, |
| 13 | +} |
| 14 | + |
| 15 | +impl Deref for PooledStack { |
| 16 | + type Target = Rc<DefaultStack>; |
| 17 | + |
| 18 | + fn deref(&self) -> &Rc<DefaultStack> { |
| 19 | + &self.stack |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl Clone for PooledStack { |
| 24 | + fn clone(&self) -> Self { |
| 25 | + Self { |
| 26 | + stack: self.stack.clone(), |
| 27 | + create_time: self.create_time, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl PartialEq<Self> for PooledStack { |
| 33 | + fn eq(&self, other: &Self) -> bool { |
| 34 | + Rc::strong_count(other).eq(&Rc::strong_count(self)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl Eq for PooledStack {} |
| 39 | + |
| 40 | +impl PartialOrd<Self> for PooledStack { |
| 41 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 42 | + Some(self.cmp(other)) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Ord for PooledStack { |
| 47 | + fn cmp(&self, other: &Self) -> Ordering { |
| 48 | + // BinaryHeap defaults to a large top heap, but we need a small top heap |
| 49 | + Rc::strong_count(other).cmp(&Rc::strong_count(self)) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +unsafe impl Stack for PooledStack { |
| 54 | + #[inline] |
| 55 | + fn base(&self) -> StackPointer { |
| 56 | + self.deref().base() |
| 57 | + } |
| 58 | + |
| 59 | + #[inline] |
| 60 | + fn limit(&self) -> StackPointer { |
| 61 | + self.deref().limit() |
| 62 | + } |
| 63 | + |
| 64 | + #[cfg(windows)] |
| 65 | + #[inline] |
| 66 | + fn teb_fields(&self) -> corosensei::stack::StackTebFields { |
| 67 | + self.deref().teb_fields() |
| 68 | + } |
| 69 | + |
| 70 | + #[cfg(windows)] |
| 71 | + #[inline] |
| 72 | + fn update_teb_fields(&mut self, stack_limit: usize, guaranteed_stack_bytes: usize) { |
| 73 | + while let Some(stack) = Rc::get_mut(self) { |
| 74 | + stack.update_teb_fields(stack_limit, guaranteed_stack_bytes); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl PooledStack { |
| 81 | + pub(crate) fn new(stack: Rc<DefaultStack>, create_time: u64) -> Self { |
| 82 | + Self { stack, create_time } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub(crate) struct StackPool { |
| 87 | + pool: RefCell<BinaryHeap<PooledStack>>, |
| 88 | + len: AtomicUsize, |
| 89 | + //最小内存数,即核心内存数 |
| 90 | + min_size: AtomicUsize, |
| 91 | + //非核心内存的最大存活时间,单位ns |
| 92 | + keep_alive_time: AtomicU64, |
| 93 | +} |
| 94 | + |
| 95 | +unsafe impl Send for StackPool {} |
| 96 | + |
| 97 | +unsafe impl Sync for StackPool {} |
| 98 | + |
| 99 | +impl Default for StackPool { |
| 100 | + fn default() -> Self { |
| 101 | + Self::new(0, 10_000_000_000) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl StackPool { |
| 106 | + pub(crate) fn new(min_size: usize, keep_alive_time: u64) -> Self { |
| 107 | + Self { |
| 108 | + pool: RefCell::new(BinaryHeap::default()), |
| 109 | + len: AtomicUsize::default(), |
| 110 | + min_size: AtomicUsize::new(min_size), |
| 111 | + keep_alive_time: AtomicU64::new(keep_alive_time), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + pub(crate) fn allocate(&self, stack_size: usize) -> std::io::Result<PooledStack> { |
| 116 | + loop { |
| 117 | + if let Ok(mut heap) = self.pool.try_borrow_mut() { |
| 118 | + if let Some(stack) = heap.peek() { |
| 119 | + if Rc::strong_count(stack) == 1 { |
| 120 | + // can use the stack |
| 121 | + if self.min_size() < self.len() |
| 122 | + && now() <= stack.create_time.saturating_add(self.keep_alive_time()) |
| 123 | + { |
| 124 | + drop(heap.pop()); |
| 125 | + self.len.store( |
| 126 | + self.len().saturating_sub(1), |
| 127 | + std::sync::atomic::Ordering::Release, |
| 128 | + ); |
| 129 | + continue; |
| 130 | + } |
| 131 | + return Ok(stack.clone()); |
| 132 | + } |
| 133 | + } |
| 134 | + let stack = PooledStack::new(Rc::new(DefaultStack::new(stack_size)?), now()); |
| 135 | + heap.push(stack.clone()); |
| 136 | + self.len.store( |
| 137 | + self.len().saturating_add(1), |
| 138 | + std::sync::atomic::Ordering::Release, |
| 139 | + ); |
| 140 | + return Ok(stack); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + pub(crate) fn keep_alive_time(&self) -> u64 { |
| 146 | + self.keep_alive_time |
| 147 | + .load(std::sync::atomic::Ordering::Acquire) |
| 148 | + } |
| 149 | + |
| 150 | + pub(crate) fn min_size(&self) -> usize { |
| 151 | + self.min_size.load(std::sync::atomic::Ordering::Acquire) |
| 152 | + } |
| 153 | + |
| 154 | + pub(crate) fn len(&self) -> usize { |
| 155 | + self.len.load(std::sync::atomic::Ordering::Acquire) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +#[cfg(test)] |
| 160 | +mod tests { |
| 161 | + use super::*; |
| 162 | + use crate::common::constants::DEFAULT_STACK_SIZE; |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn test_stack_pool() -> std::io::Result<()> { |
| 166 | + let pool = StackPool::default(); |
| 167 | + let stack = pool.allocate(DEFAULT_STACK_SIZE)?; |
| 168 | + assert_eq!(Rc::strong_count(&stack), 2); |
| 169 | + drop(stack); |
| 170 | + let stack = pool.allocate(DEFAULT_STACK_SIZE)?; |
| 171 | + assert_eq!(Rc::strong_count(&stack), 2); |
| 172 | + assert_eq!(pool.len(), 1); |
| 173 | + _ = pool.allocate(DEFAULT_STACK_SIZE)?; |
| 174 | + assert_eq!(pool.len(), 2); |
| 175 | + Ok(()) |
| 176 | + } |
| 177 | +} |
0 commit comments