1
1
//! Utterly inefficient cross-platform preemptive user-mode scheduling
2
2
use once_cell:: sync:: OnceCell ;
3
+ use slab:: Slab ;
3
4
use std:: {
4
5
panic:: { catch_unwind, AssertUnwindSafe } ,
5
6
sync:: { mpsc, Arc , Mutex , MutexGuard } ,
6
7
thread:: Result ,
7
8
} ;
8
9
9
- use crate :: {
10
- threading,
11
- utils:: iterpool:: { Pool , PoolPtr } ,
12
- } ;
10
+ use crate :: threading;
11
+
12
+ type SlabPtr = usize ;
13
13
14
14
#[ cfg( test) ]
15
15
mod tests;
@@ -43,7 +43,7 @@ pub struct ThreadGroupLockGuard<'a, Sched: ?Sized> {
43
43
44
44
/// Identifies a thread in [`ThreadGroup`].
45
45
#[ derive( Debug , Clone , Copy , Eq , PartialEq , Hash ) ]
46
- pub struct ThreadId ( PoolPtr ) ;
46
+ pub struct ThreadId ( SlabPtr ) ;
47
47
48
48
/// Encapsulates the state of a client-supplied user-mode scheduler.
49
49
pub trait Scheduler : Send + ' static {
@@ -61,7 +61,7 @@ pub trait Scheduler: Send + 'static {
61
61
62
62
#[ derive( Debug ) ]
63
63
struct State < Sched : ?Sized > {
64
- threads : Pool < WorkerThread > ,
64
+ threads : Slab < WorkerThread > ,
65
65
num_threads : usize ,
66
66
cur_thread_id : Option < ThreadId > ,
67
67
shutting_down : bool ,
@@ -92,7 +92,7 @@ impl<Sched: Scheduler> ThreadGroup<Sched> {
92
92
let ( send, recv) = mpsc:: channel ( ) ;
93
93
94
94
let state = Arc :: new ( Mutex :: new ( State {
95
- threads : Pool :: new ( ) ,
95
+ threads : Slab :: new ( ) ,
96
96
num_threads : 0 ,
97
97
cur_thread_id : None ,
98
98
shutting_down : false ,
@@ -144,10 +144,10 @@ impl<'a, Sched: Scheduler> ThreadGroupLockGuard<'a, Sched> {
144
144
let state = Arc :: clone ( self . state_ref ) ;
145
145
146
146
// Allocate a `ThreadId`
147
- let ptr = self
147
+ let ptr: SlabPtr = self
148
148
. guard
149
149
. threads
150
- . allocate ( WorkerThread { join_handle : None } ) ;
150
+ . insert ( WorkerThread { join_handle : None } ) ;
151
151
let thread_id = ThreadId ( ptr) ;
152
152
self . guard . num_threads += 1 ;
153
153
@@ -312,7 +312,7 @@ fn finalize_thread(
312
312
// Delete the current thread
313
313
let mut state_guard = thread_group. lock ( ) . unwrap ( ) ;
314
314
state_guard. sched . thread_exited ( thread_id) ;
315
- state_guard. threads . deallocate ( thread_id. 0 ) . unwrap ( ) ;
315
+ state_guard. threads . remove ( thread_id. 0 ) ;
316
316
state_guard. num_threads -= 1 ;
317
317
318
318
if let Err ( e) = result {
0 commit comments