-
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.
- Loading branch information
Showing
4 changed files
with
64 additions
and
21 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,35 @@ | ||
use std::{sync, thread}; | ||
|
||
pub(crate) type Task = dyn FnOnce() + Send; | ||
pub(crate) type BoxedTask = Box<Task>; | ||
|
||
pub(crate) struct ScopedPool<I>(I); | ||
|
||
impl<I> ScopedPool<I> | ||
where | ||
I: IntoIterator<Item = BoxedTask>, | ||
I::IntoIter: Send, | ||
{ | ||
pub(crate) fn new(tasks: I) -> Self { | ||
Self(tasks) | ||
} | ||
|
||
pub(crate) fn run(self, test_threads: Option<usize>) { | ||
let num_threads = test_threads | ||
.or_else(|| thread::available_parallelism().ok().map(Into::into)) | ||
.unwrap_or(1); | ||
let iter = sync::Mutex::new(self.0.into_iter()); | ||
|
||
thread::scope(|scope| { | ||
let work = || { | ||
let mut guard = iter.lock().unwrap(); | ||
for task in guard.by_ref() { | ||
task(); | ||
} | ||
}; | ||
for _ in 0..num_threads { | ||
scope.spawn(work); | ||
} | ||
}); | ||
} | ||
} |