Skip to content

Commit 99033af

Browse files
committed
refactor(port_std): replace the bespoke iterpool with slab
It's hard to find something like `slab` because such a data structure doesn't have a well-known common name.
1 parent ad136b1 commit 99033af

File tree

5 files changed

+12
-632
lines changed

5 files changed

+12
-632
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/r3_port_std/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ atomic_ref = { version = "0.2.0" }
1515
env_logger = { version = "0.8.4" }
1616
once_cell = { version = "1.4.0" }
1717
spin = { version = "0.9.2", default-features = false, features = ["spin_mutex"] }
18+
slab = { version = "0.4.5" }
1819
log = { version = "0.4.8" }
1920

2021
[dev-dependencies.r3_test_suite]

src/r3_port_std/src/ums.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Utterly inefficient cross-platform preemptive user-mode scheduling
22
use once_cell::sync::OnceCell;
3+
use slab::Slab;
34
use std::{
45
panic::{catch_unwind, AssertUnwindSafe},
56
sync::{mpsc, Arc, Mutex, MutexGuard},
67
thread::Result,
78
};
89

9-
use crate::{
10-
threading,
11-
utils::iterpool::{Pool, PoolPtr},
12-
};
10+
use crate::threading;
11+
12+
type SlabPtr = usize;
1313

1414
#[cfg(test)]
1515
mod tests;
@@ -43,7 +43,7 @@ pub struct ThreadGroupLockGuard<'a, Sched: ?Sized> {
4343

4444
/// Identifies a thread in [`ThreadGroup`].
4545
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
46-
pub struct ThreadId(PoolPtr);
46+
pub struct ThreadId(SlabPtr);
4747

4848
/// Encapsulates the state of a client-supplied user-mode scheduler.
4949
pub trait Scheduler: Send + 'static {
@@ -61,7 +61,7 @@ pub trait Scheduler: Send + 'static {
6161

6262
#[derive(Debug)]
6363
struct State<Sched: ?Sized> {
64-
threads: Pool<WorkerThread>,
64+
threads: Slab<WorkerThread>,
6565
num_threads: usize,
6666
cur_thread_id: Option<ThreadId>,
6767
shutting_down: bool,
@@ -92,7 +92,7 @@ impl<Sched: Scheduler> ThreadGroup<Sched> {
9292
let (send, recv) = mpsc::channel();
9393

9494
let state = Arc::new(Mutex::new(State {
95-
threads: Pool::new(),
95+
threads: Slab::new(),
9696
num_threads: 0,
9797
cur_thread_id: None,
9898
shutting_down: false,
@@ -144,10 +144,10 @@ impl<'a, Sched: Scheduler> ThreadGroupLockGuard<'a, Sched> {
144144
let state = Arc::clone(self.state_ref);
145145

146146
// Allocate a `ThreadId`
147-
let ptr = self
147+
let ptr: SlabPtr = self
148148
.guard
149149
.threads
150-
.allocate(WorkerThread { join_handle: None });
150+
.insert(WorkerThread { join_handle: None });
151151
let thread_id = ThreadId(ptr);
152152
self.guard.num_threads += 1;
153153

@@ -312,7 +312,7 @@ fn finalize_thread(
312312
// Delete the current thread
313313
let mut state_guard = thread_group.lock().unwrap();
314314
state_guard.sched.thread_exited(thread_id);
315-
state_guard.threads.deallocate(thread_id.0).unwrap();
315+
state_guard.threads.remove(thread_id.0);
316316
state_guard.num_threads -= 1;
317317

318318
if let Err(e) = result {

src/r3_port_std/src/utils.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::sync::atomic;
22

3-
pub mod iterpool;
4-
53
pub trait HasAtomicEquivalent {
64
type AtomicEquivalent;
75
}

0 commit comments

Comments
 (0)