-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit replaces the `threadpool` crate with a handcrafted solution based on scoped threads. This leaves `valgrind` much happier than before. We also lose some dependency baggage. A few tests are added to verify that the requested number of threads actually spawn and start pulling tasks.
- Loading branch information
Showing
4 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name = "libtest-mimic" | |
version = "0.7.3" | ||
authors = ["Lukas Kalbertodt <[email protected]>"] | ||
edition = "2021" | ||
rust-version = "1.60" | ||
rust-version = "1.63" | ||
|
||
description = """ | ||
Write your own test harness that looks and behaves like the built-in test \ | ||
|
@@ -20,7 +20,6 @@ exclude = [".github"] | |
|
||
[dependencies] | ||
clap = { version = "4.0.8", features = ["derive"] } | ||
threadpool = "1.8.1" | ||
termcolor = "1.0.5" | ||
escape8259 = "0.5.2" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::{sync, thread}; | ||
|
||
pub(crate) type Task = dyn FnOnce() + Send; | ||
pub(crate) type BoxedTask = Box<Task>; | ||
|
||
pub(crate) fn run_on_scoped_pool( | ||
tasks: impl IntoIterator<Item = BoxedTask, IntoIter: Send>, | ||
num_threads: usize, | ||
) { | ||
if num_threads < 2 { | ||
panic!("run_on_scoped_pool should not be called with num_threads less than 2"); | ||
} | ||
|
||
let sync_iter = sync::Mutex::new(tasks.into_iter()); | ||
let next_task = || sync_iter.lock().unwrap().next(); | ||
|
||
thread::scope(|scope| { | ||
for _ in 0..num_threads { | ||
scope.spawn(|| { | ||
while let Some(task) = next_task() { | ||
task(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::{ | ||
collections::HashSet, | ||
sync::{Arc, Mutex}, | ||
thread::ThreadId, | ||
}; | ||
|
||
use super::{run_on_scoped_pool, BoxedTask}; | ||
|
||
#[derive(Default, Clone)] | ||
struct TaskGenerator { | ||
threads: Arc<Mutex<HashSet<ThreadId>>>, | ||
} | ||
impl TaskGenerator { | ||
fn num_unique_threads(&self) -> usize { | ||
self.threads.lock().unwrap().len() | ||
} | ||
} | ||
impl Iterator for TaskGenerator { | ||
type Item = BoxedTask; | ||
fn next(&mut self) -> Option<Self::Item> { | ||
self.threads | ||
.lock() | ||
.unwrap() | ||
.insert(std::thread::current().id()); | ||
|
||
// Tell the calling thread that there are not tasks left for it | ||
// and it may exit now. | ||
None | ||
} | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn check_num_threads_0() { | ||
let tasks: TaskGenerator = Default::default(); | ||
run_on_scoped_pool(tasks.clone(), 0); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn check_num_threads_1() { | ||
let tasks: TaskGenerator = Default::default(); | ||
run_on_scoped_pool(tasks.clone(), 1); | ||
} | ||
|
||
#[test] | ||
fn check_num_threads_2() { | ||
let tasks: TaskGenerator = Default::default(); | ||
run_on_scoped_pool(tasks.clone(), 2); | ||
assert_eq!(tasks.num_unique_threads(), 2); | ||
} | ||
|
||
#[test] | ||
fn check_num_threads_13() { | ||
let tasks: TaskGenerator = Default::default(); | ||
run_on_scoped_pool(tasks.clone(), 13); | ||
assert_eq!(tasks.num_unique_threads(), 13); | ||
} | ||
} |