1- use crate :: common:: beans:: BeanFactory ;
2- use crate :: common:: constants:: STACK_POOL_BEAN ;
31use crate :: common:: now;
2+ use crate :: config:: Config ;
43use corosensei:: stack:: { DefaultStack , Stack , StackPointer } ;
4+ use once_cell:: sync:: OnceCell ;
55use std:: cell:: UnsafeCell ;
66use std:: cmp:: Ordering ;
77use std:: collections:: BinaryHeap ;
@@ -138,35 +138,49 @@ impl PooledStack {
138138 }
139139}
140140
141- pub ( crate ) struct StackPool {
141+ static STACK_POOL : OnceCell < MemoryPool > = OnceCell :: new ( ) ;
142+
143+ /// A memory pool for reusing stacks.
144+ #[ derive( educe:: Educe ) ]
145+ #[ educe( Debug ) ]
146+ pub struct MemoryPool {
147+ #[ educe( Debug ( ignore) ) ]
142148 pool : UnsafeCell < BinaryHeap < PooledStack > > ,
143149 len : AtomicUsize ,
144150 //最小内存数,即核心内存数
145- min_size : AtomicUsize ,
151+ min_count : AtomicUsize ,
146152 //非核心内存的最大存活时间,单位ns
147153 keep_alive_time : AtomicU64 ,
148154}
149155
150- unsafe impl Send for StackPool { }
156+ unsafe impl Send for MemoryPool { }
151157
152- unsafe impl Sync for StackPool { }
158+ unsafe impl Sync for MemoryPool { }
153159
154- impl Default for StackPool {
160+ impl Default for MemoryPool {
155161 fn default ( ) -> Self {
156162 Self :: new ( 0 , 10_000_000_000 )
157163 }
158164}
159165
160- impl StackPool {
166+ impl MemoryPool {
167+ /// Init the `MemoryPool`.
168+ pub fn init ( config : & Config ) -> Result < ( ) , MemoryPool > {
169+ STACK_POOL . set ( MemoryPool :: new (
170+ config. min_memory_count ( ) ,
171+ config. memory_keep_alive_time ( ) ,
172+ ) )
173+ }
174+
161175 pub ( crate ) fn get_instance < ' m > ( ) -> & ' m Self {
162- BeanFactory :: get_or_default ( STACK_POOL_BEAN )
176+ STACK_POOL . get_or_init ( MemoryPool :: default )
163177 }
164178
165- pub ( crate ) fn new ( min_size : usize , keep_alive_time : u64 ) -> Self {
179+ pub ( crate ) fn new ( min_count : usize , keep_alive_time : u64 ) -> Self {
166180 Self {
167181 pool : UnsafeCell :: new ( BinaryHeap :: default ( ) ) ,
168182 len : AtomicUsize :: default ( ) ,
169- min_size : AtomicUsize :: new ( min_size ) ,
183+ min_count : AtomicUsize :: new ( min_count ) ,
170184 keep_alive_time : AtomicU64 :: new ( keep_alive_time) ,
171185 }
172186 }
@@ -194,7 +208,7 @@ impl StackPool {
194208 stack. update_stack_teb_fields ( ) ;
195209 return Ok ( stack) ;
196210 }
197- if self . min_size ( ) < self . len ( )
211+ if self . min_count ( ) < self . len ( )
198212 && now ( ) <= stack. create_time . saturating_add ( self . keep_alive_time ( ) )
199213 {
200214 // clean the expired stack
@@ -221,13 +235,13 @@ impl StackPool {
221235 }
222236
223237 #[ allow( dead_code) ]
224- pub ( crate ) fn set_min_size ( & self , min_size : usize ) {
225- self . min_size
226- . store ( min_size , std:: sync:: atomic:: Ordering :: Release ) ;
238+ pub ( crate ) fn set_min_count ( & self , min_count : usize ) {
239+ self . min_count
240+ . store ( min_count , std:: sync:: atomic:: Ordering :: Release ) ;
227241 }
228242
229- pub ( crate ) fn min_size ( & self ) -> usize {
230- self . min_size . load ( std:: sync:: atomic:: Ordering :: Acquire )
243+ pub ( crate ) fn min_count ( & self ) -> usize {
244+ self . min_count . load ( std:: sync:: atomic:: Ordering :: Acquire )
231245 }
232246
233247 pub ( crate ) fn len ( & self ) -> usize {
@@ -264,7 +278,7 @@ impl StackPool {
264278 }
265279 }
266280 for stack in maybe_free {
267- if self . min_size ( ) < self . len ( )
281+ if self . min_count ( ) < self . len ( )
268282 && now ( ) <= stack. create_time . saturating_add ( self . keep_alive_time ( ) )
269283 {
270284 // free the stack
@@ -283,7 +297,7 @@ mod tests {
283297
284298 #[ test]
285299 fn test_stack_pool ( ) -> std:: io:: Result < ( ) > {
286- let pool = StackPool :: default ( ) ;
300+ let pool = MemoryPool :: default ( ) ;
287301 let stack = pool. allocate ( DEFAULT_STACK_SIZE ) ?;
288302 assert_eq ! ( Rc :: strong_count( & stack. stack) , 2 ) ;
289303 drop ( stack) ;
0 commit comments