Skip to content

Commit 1a7f3ed

Browse files
committed
Add thread Builder::no_hooks().
1 parent e06d406 commit 1a7f3ed

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

library/std/src/thread/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ pub struct Builder {
261261
name: Option<String>,
262262
// The size of the stack for the spawned thread in bytes
263263
stack_size: Option<usize>,
264+
// Skip running and inheriting the thread spawn hooks
265+
no_hooks: bool,
264266
}
265267

266268
impl Builder {
@@ -284,7 +286,7 @@ impl Builder {
284286
/// ```
285287
#[stable(feature = "rust1", since = "1.0.0")]
286288
pub fn new() -> Builder {
287-
Builder { name: None, stack_size: None }
289+
Builder { name: None, stack_size: None, no_hooks: false }
288290
}
289291

290292
/// Names the thread-to-be. Currently the name is used for identification
@@ -340,6 +342,16 @@ impl Builder {
340342
self
341343
}
342344

345+
/// Disables running and inheriting [spawn hooks](add_spawn_hook).
346+
///
347+
/// Use this if the parent thread is in no way relevant for the child thread.
348+
/// For example, when lazily spawning threads for a thread pool.
349+
#[unstable(feature = "thread_spawn_hook", issue = "none")]
350+
pub fn no_hooks(mut self) -> Builder {
351+
self.no_hooks = true;
352+
self
353+
}
354+
343355
/// Spawns a new thread by taking ownership of the `Builder`, and returns an
344356
/// [`io::Result`] to its [`JoinHandle`].
345357
///
@@ -462,7 +474,7 @@ impl Builder {
462474
F: Send,
463475
T: Send,
464476
{
465-
let Builder { name, stack_size } = self;
477+
let Builder { name, stack_size, no_hooks } = self;
466478

467479
let stack_size = stack_size.unwrap_or_else(|| {
468480
static MIN: AtomicUsize = AtomicUsize::new(0);
@@ -488,7 +500,11 @@ impl Builder {
488500
None => Thread::new_unnamed(id),
489501
};
490502

491-
let hooks = spawnhook::run_spawn_hooks(&my_thread);
503+
let hooks = if no_hooks {
504+
spawnhook::ChildSpawnHooks::default()
505+
} else {
506+
spawnhook::run_spawn_hooks(&my_thread)
507+
};
492508

493509
let their_thread = my_thread.clone();
494510

library/std/src/thread/spawnhook.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ where
104104
/// Called on the parent thread.
105105
///
106106
/// Returns the functions to be called on the newly spawned thread.
107-
pub(super) fn run_spawn_hooks(thread: &Thread) -> SpawnHookResults {
107+
pub(super) fn run_spawn_hooks(thread: &Thread) -> ChildSpawnHooks {
108108
// Get a snapshot of the spawn hooks.
109109
// (Increments the refcount to the first node.)
110110
let hooks = SPAWN_HOOKS.with(|hooks| {
@@ -121,19 +121,20 @@ pub(super) fn run_spawn_hooks(thread: &Thread) -> SpawnHookResults {
121121
}
122122
// Pass on the snapshot of the hooks and the results to the new thread,
123123
// which will then run SpawnHookResults::run().
124-
SpawnHookResults { hooks, to_run }
124+
ChildSpawnHooks { hooks, to_run }
125125
}
126126

127127
/// The results of running the spawn hooks.
128128
///
129129
/// This struct is sent to the new thread.
130130
/// It contains the inherited hooks and the closures to be run.
131-
pub(super) struct SpawnHookResults {
131+
#[derive(Default)]
132+
pub(super) struct ChildSpawnHooks {
132133
hooks: SpawnHooks,
133134
to_run: Vec<Box<dyn FnOnce() + Send>>,
134135
}
135136

136-
impl SpawnHookResults {
137+
impl ChildSpawnHooks {
137138
// This is run on the newly spawned thread, directly at the start.
138139
pub(super) fn run(self) {
139140
SPAWN_HOOKS.set(self.hooks);

0 commit comments

Comments
 (0)