#[cfg(not(feature = "shim"))]
pub use std::thread::*;
#[cfg(feature = "shim")]
pub use std::thread::{
AccessError, JoinHandle, LocalKey, Scope, ScopedJoinHandle, Thread, ThreadId,
available_parallelism, current, panicking, park, park_timeout, sleep, yield_now,
};
#[cfg(feature = "shim")]
pub use std::thread::Result;
#[cfg(feature = "shim")]
mod shimmed {
use std::io;
pub fn spawn<F, T>(f: F) -> std::thread::JoinHandle<T>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let parent = std::thread::current().id();
std::thread::spawn(move || {
crate::register_thread_child_of(parent);
f()
})
}
pub struct Builder {
inner: std::thread::Builder,
}
impl Builder {
pub fn new() -> Self {
Self {
inner: std::thread::Builder::new(),
}
}
pub fn name(mut self, name: String) -> Self {
self.inner = self.inner.name(name);
self
}
pub fn stack_size(mut self, size: usize) -> Self {
self.inner = self.inner.stack_size(size);
self
}
pub fn spawn<F, T>(self, f: F) -> io::Result<std::thread::JoinHandle<T>>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
let parent = std::thread::current().id();
self.inner.spawn(move || {
crate::register_thread_child_of(parent);
f()
})
}
pub fn spawn_scoped<'scope, 'env, F, T>(
self,
scope: &'scope std::thread::Scope<'scope, 'env>,
f: F,
) -> io::Result<std::thread::ScopedJoinHandle<'scope, T>>
where
F: FnOnce() -> T + Send + 'scope,
T: Send + 'scope,
{
let parent = std::thread::current().id();
self.inner.spawn_scoped(scope, move || {
crate::register_thread_child_of(parent);
f()
})
}
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
}
#[cfg(feature = "shim")]
pub use shimmed::{Builder, spawn};
#[cfg(feature = "shim")]
pub use std::thread::scope;