1//! Threads that can borrow variables from the stack.
2//!
3//! Create a scope when spawned threads need to access variables on the stack:
4//!
5//! ```
6//! use crossbeam_utils::thread;
7//!
8//! let people = vec![
9//! "Alice".to_string(),
10//! "Bob".to_string(),
11//! "Carol".to_string(),
12//! ];
13//!
14//! thread::scope(|s| {
15//! for person in &people {
16//! s.spawn(move |_| {
17//! println!("Hello, {}!", person);
18//! });
19//! }
20//! }).unwrap();
21//! ```
22//!
23//! # Why scoped threads?
24//!
25//! Suppose we wanted to re-write the previous example using plain threads:
26//!
27//! ```compile_fail,E0597
28//! use std::thread;
29//!
30//! let people = vec![
31//! "Alice".to_string(),
32//! "Bob".to_string(),
33//! "Carol".to_string(),
34//! ];
35//!
36//! let mut threads = Vec::new();
37//!
38//! for person in &people {
39//! threads.push(thread::spawn(move || {
40//! println!("Hello, {}!", person);
41//! }));
42//! }
43//!
44//! for thread in threads {
45//! thread.join().unwrap();
46//! }
47//! ```
48//!
49//! This doesn't work because the borrow checker complains about `people` not living long enough:
50//!
51//! ```text
52//! error[E0597]: `people` does not live long enough
53//! --> src/main.rs:12:20
54//! |
55//! 12 | for person in &people {
56//! | ^^^^^^ borrowed value does not live long enough
57//! ...
58//! 21 | }
59//! | - borrowed value only lives until here
60//! |
61//! = note: borrowed value must be valid for the static lifetime...
62//! ```
63//!
64//! The problem here is that spawned threads are not allowed to borrow variables on stack because
65//! the compiler cannot prove they will be joined before `people` is destroyed.
66//!
67//! Scoped threads are a mechanism to guarantee to the compiler that spawned threads will be joined
68//! before the scope ends.
69//!
70//! # How scoped threads work
71//!
72//! If a variable is borrowed by a thread, the thread must complete before the variable is
73//! destroyed. Threads spawned using [`std::thread::spawn`] can only borrow variables with the
74//! `'static` lifetime because the borrow checker cannot be sure when the thread will complete.
75//!
76//! A scope creates a clear boundary between variables outside the scope and threads inside the
77//! scope. Whenever a scope spawns a thread, it promises to join the thread before the scope ends.
78//! This way we guarantee to the borrow checker that scoped threads only live within the scope and
79//! can safely access variables outside it.
80//!
81//! # Nesting scoped threads
82//!
83//! Sometimes scoped threads need to spawn more threads within the same scope. This is a little
84//! tricky because argument `s` lives *inside* the invocation of `thread::scope()` and as such
85//! cannot be borrowed by scoped threads:
86//!
87//! ```compile_fail,E0373,E0521
88//! use crossbeam_utils::thread;
89//!
90//! thread::scope(|s| {
91//! s.spawn(|_| {
92//! // Not going to compile because we're trying to borrow `s`,
93//! // which lives *inside* the scope! :(
94//! s.spawn(|_| println!("nested thread"));
95//! });
96//! });
97//! ```
98//!
99//! Fortunately, there is a solution. Every scoped thread is passed a reference to its scope as an
100//! argument, which can be used for spawning nested threads:
101//!
102//! ```
103//! use crossbeam_utils::thread;
104//!
105//! thread::scope(|s| {
106//! // Note the `|s|` here.
107//! s.spawn(|s| {
108//! // Yay, this works because we're using a fresh argument `s`! :)
109//! s.spawn(|_| println!("nested thread"));
110//! });
111//! }).unwrap();
112//! ```
113
114use std::fmt;
115use std::io;
116use std::marker::PhantomData;
117use std::mem;
118use std::panic;
119use std::sync::{Arc, Mutex};
120use std::thread;
121
122use crate::sync::WaitGroup;
123use cfg_if::cfg_if;
124
125type SharedVec<T> = Arc<Mutex<Vec<T>>>;
126type SharedOption<T> = Arc<Mutex<Option<T>>>;
127
128/// Creates a new scope for spawning threads.
129///
130/// All child threads that haven't been manually joined will be automatically joined just before
131/// this function invocation ends. If all joined threads have successfully completed, `Ok` is
132/// returned with the return value of `f`. If any of the joined threads has panicked, an `Err` is
133/// returned containing errors from panicked threads. Note that if panics are implemented by
134/// aborting the process, no error is returned; see the notes of [std::panic::catch_unwind].
135///
136/// **Note:** Since Rust 1.63, this function is soft-deprecated in favor of the more efficient [`std::thread::scope`].
137///
138/// # Examples
139///
140/// ```
141/// use crossbeam_utils::thread;
142///
143/// let var = vec![1, 2, 3];
144///
145/// thread::scope(|s| {
146/// s.spawn(|_| {
147/// println!("A child thread borrowing `var`: {:?}", var);
148/// });
149/// }).unwrap();
150/// ```
151pub fn scope<'env, F, R>(f: F) -> thread::Result<R>
152where
153 F: FnOnce(&Scope<'env>) -> R,
154{
155 struct AbortOnPanic;
156 impl Drop for AbortOnPanic {
157 fn drop(&mut self) {
158 if thread::panicking() {
159 std::process::abort();
160 }
161 }
162 }
163
164 let wg = WaitGroup::new();
165 let scope = Scope::<'env> {
166 handles: SharedVec::default(),
167 wait_group: wg.clone(),
168 _marker: PhantomData,
169 };
170
171 // Execute the scoped function, but catch any panics.
172 let result = panic::catch_unwind(panic::AssertUnwindSafe(|| f(&scope)));
173
174 // If an unwinding panic occurs before all threads are joined
175 // promote it to an aborting panic to prevent any threads from escaping the scope.
176 let guard = AbortOnPanic;
177
178 // Wait until all nested scopes are dropped.
179 drop(scope.wait_group);
180 wg.wait();
181
182 // Join all remaining spawned threads.
183 let panics: Vec<_> = scope
184 .handles
185 .lock()
186 .unwrap()
187 // Filter handles that haven't been joined, join them, and collect errors.
188 .drain(..)
189 .filter_map(|handle| handle.lock().unwrap().take())
190 .filter_map(|handle| handle.join().err())
191 .collect();
192
193 mem::forget(guard);
194
195 // If `f` has panicked, resume unwinding.
196 // If any of the child threads have panicked, return the panic errors.
197 // Otherwise, everything is OK and return the result of `f`.
198 match result {
199 Err(err) => panic::resume_unwind(err),
200 Ok(res) => {
201 if panics.is_empty() {
202 Ok(res)
203 } else {
204 Err(Box::new(panics))
205 }
206 }
207 }
208}
209
210/// A scope for spawning threads.
211pub struct Scope<'env> {
212 /// The list of the thread join handles.
213 handles: SharedVec<SharedOption<thread::JoinHandle<()>>>,
214
215 /// Used to wait until all subscopes all dropped.
216 wait_group: WaitGroup,
217
218 /// Borrows data with invariant lifetime `'env`.
219 _marker: PhantomData<&'env mut &'env ()>,
220}
221
222unsafe impl Sync for Scope<'_> {}
223
224impl<'env> Scope<'env> {
225 /// Spawns a scoped thread.
226 ///
227 /// This method is similar to the [`spawn`] function in Rust's standard library. The difference
228 /// is that this thread is scoped, meaning it's guaranteed to terminate before the scope exits,
229 /// allowing it to reference variables outside the scope.
230 ///
231 /// The scoped thread is passed a reference to this scope as an argument, which can be used for
232 /// spawning nested threads.
233 ///
234 /// The returned [handle](ScopedJoinHandle) can be used to manually
235 /// [join](ScopedJoinHandle::join) the thread before the scope exits.
236 ///
237 /// This will create a thread using default parameters of [`ScopedThreadBuilder`], if you want to specify the
238 /// stack size or the name of the thread, use this API instead.
239 ///
240 /// [`spawn`]: std::thread::spawn
241 ///
242 /// # Panics
243 ///
244 /// Panics if the OS fails to create a thread; use [`ScopedThreadBuilder::spawn`]
245 /// to recover from such errors.
246 ///
247 /// # Examples
248 ///
249 /// ```
250 /// use crossbeam_utils::thread;
251 ///
252 /// thread::scope(|s| {
253 /// let handle = s.spawn(|_| {
254 /// println!("A child thread is running");
255 /// 42
256 /// });
257 ///
258 /// // Join the thread and retrieve its result.
259 /// let res = handle.join().unwrap();
260 /// assert_eq!(res, 42);
261 /// }).unwrap();
262 /// ```
263 pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
264 where
265 F: FnOnce(&Scope<'env>) -> T,
266 F: Send + 'env,
267 T: Send + 'env,
268 {
269 self.builder()
270 .spawn(f)
271 .expect("failed to spawn scoped thread")
272 }
273
274 /// Creates a builder that can configure a thread before spawning.
275 ///
276 /// # Examples
277 ///
278 /// ```
279 /// use crossbeam_utils::thread;
280 ///
281 /// thread::scope(|s| {
282 /// s.builder()
283 /// .spawn(|_| println!("A child thread is running"))
284 /// .unwrap();
285 /// }).unwrap();
286 /// ```
287 pub fn builder<'scope>(&'scope self) -> ScopedThreadBuilder<'scope, 'env> {
288 ScopedThreadBuilder {
289 scope: self,
290 builder: thread::Builder::new(),
291 }
292 }
293}
294
295impl fmt::Debug for Scope<'_> {
296 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297 f.pad("Scope { .. }")
298 }
299}
300
301/// Configures the properties of a new thread.
302///
303/// The two configurable properties are:
304///
305/// - [`name`]: Specifies an [associated name for the thread][naming-threads].
306/// - [`stack_size`]: Specifies the [desired stack size for the thread][stack-size].
307///
308/// The [`spawn`] method will take ownership of the builder and return an [`io::Result`] of the
309/// thread handle with the given configuration.
310///
311/// The [`Scope::spawn`] method uses a builder with default configuration and unwraps its return
312/// value. You may want to use this builder when you want to recover from a failure to launch a
313/// thread.
314///
315/// # Examples
316///
317/// ```
318/// use crossbeam_utils::thread;
319///
320/// thread::scope(|s| {
321/// s.builder()
322/// .spawn(|_| println!("Running a child thread"))
323/// .unwrap();
324/// }).unwrap();
325/// ```
326///
327/// [`name`]: ScopedThreadBuilder::name
328/// [`stack_size`]: ScopedThreadBuilder::stack_size
329/// [`spawn`]: ScopedThreadBuilder::spawn
330/// [`io::Result`]: std::io::Result
331/// [naming-threads]: std::thread#naming-threads
332/// [stack-size]: std::thread#stack-size
333#[derive(Debug)]
334pub struct ScopedThreadBuilder<'scope, 'env> {
335 scope: &'scope Scope<'env>,
336 builder: thread::Builder,
337}
338
339impl<'scope, 'env> ScopedThreadBuilder<'scope, 'env> {
340 /// Sets the name for the new thread.
341 ///
342 /// The name must not contain null bytes (`\0`).
343 ///
344 /// For more information about named threads, see [here][naming-threads].
345 ///
346 /// # Examples
347 ///
348 /// ```
349 /// use crossbeam_utils::thread;
350 /// use std::thread::current;
351 ///
352 /// thread::scope(|s| {
353 /// s.builder()
354 /// .name("my thread".to_string())
355 /// .spawn(|_| assert_eq!(current().name(), Some("my thread")))
356 /// .unwrap();
357 /// }).unwrap();
358 /// ```
359 ///
360 /// [naming-threads]: std::thread#naming-threads
361 pub fn name(mut self, name: String) -> ScopedThreadBuilder<'scope, 'env> {
362 self.builder = self.builder.name(name);
363 self
364 }
365
366 /// Sets the size of the stack for the new thread.
367 ///
368 /// The stack size is measured in bytes.
369 ///
370 /// For more information about the stack size for threads, see [here][stack-size].
371 ///
372 /// # Examples
373 ///
374 /// ```
375 /// use crossbeam_utils::thread;
376 ///
377 /// thread::scope(|s| {
378 /// s.builder()
379 /// .stack_size(32 * 1024)
380 /// .spawn(|_| println!("Running a child thread"))
381 /// .unwrap();
382 /// }).unwrap();
383 /// ```
384 ///
385 /// [stack-size]: std::thread#stack-size
386 pub fn stack_size(mut self, size: usize) -> ScopedThreadBuilder<'scope, 'env> {
387 self.builder = self.builder.stack_size(size);
388 self
389 }
390
391 /// Spawns a scoped thread with this configuration.
392 ///
393 /// The scoped thread is passed a reference to this scope as an argument, which can be used for
394 /// spawning nested threads.
395 ///
396 /// The returned handle can be used to manually join the thread before the scope exits.
397 ///
398 /// # Errors
399 ///
400 /// Unlike the [`Scope::spawn`] method, this method yields an
401 /// [`io::Result`] to capture any failure to create the thread at
402 /// the OS level.
403 ///
404 /// [`io::Result`]: std::io::Result
405 ///
406 /// # Panics
407 ///
408 /// Panics if a thread name was set and it contained null bytes.
409 ///
410 /// # Examples
411 ///
412 /// ```
413 /// use crossbeam_utils::thread;
414 ///
415 /// thread::scope(|s| {
416 /// let handle = s.builder()
417 /// .spawn(|_| {
418 /// println!("A child thread is running");
419 /// 42
420 /// })
421 /// .unwrap();
422 ///
423 /// // Join the thread and retrieve its result.
424 /// let res = handle.join().unwrap();
425 /// assert_eq!(res, 42);
426 /// }).unwrap();
427 /// ```
428 pub fn spawn<F, T>(self, f: F) -> io::Result<ScopedJoinHandle<'scope, T>>
429 where
430 F: FnOnce(&Scope<'env>) -> T,
431 F: Send + 'env,
432 T: Send + 'env,
433 {
434 // The result of `f` will be stored here.
435 let result = SharedOption::default();
436
437 // Spawn the thread and grab its join handle and thread handle.
438 let (handle, thread) = {
439 let result = Arc::clone(&result);
440
441 // A clone of the scope that will be moved into the new thread.
442 let scope = Scope::<'env> {
443 handles: Arc::clone(&self.scope.handles),
444 wait_group: self.scope.wait_group.clone(),
445 _marker: PhantomData,
446 };
447
448 // Spawn the thread.
449 let handle = {
450 let closure = move || {
451 // Make sure the scope is inside the closure with the proper `'env` lifetime.
452 let scope: Scope<'env> = scope;
453
454 // Run the closure.
455 let res = f(&scope);
456
457 // Store the result if the closure didn't panic.
458 *result.lock().unwrap() = Some(res);
459 };
460
461 // Allocate `closure` on the heap and erase the `'env` bound.
462 let closure: Box<dyn FnOnce() + Send + 'env> = Box::new(closure);
463 let closure: Box<dyn FnOnce() + Send + 'static> =
464 unsafe { mem::transmute(closure) };
465
466 // Finally, spawn the closure.
467 self.builder.spawn(closure)?
468 };
469
470 let thread = handle.thread().clone();
471 let handle = Arc::new(Mutex::new(Some(handle)));
472 (handle, thread)
473 };
474
475 // Add the handle to the shared list of join handles.
476 self.scope.handles.lock().unwrap().push(Arc::clone(&handle));
477
478 Ok(ScopedJoinHandle {
479 handle,
480 result,
481 thread,
482 _marker: PhantomData,
483 })
484 }
485}
486
487unsafe impl<T> Send for ScopedJoinHandle<'_, T> {}
488unsafe impl<T> Sync for ScopedJoinHandle<'_, T> {}
489
490/// A handle that can be used to join its scoped thread.
491///
492/// This struct is created by the [`Scope::spawn`] method and the
493/// [`ScopedThreadBuilder::spawn`] method.
494pub struct ScopedJoinHandle<'scope, T> {
495 /// A join handle to the spawned thread.
496 handle: SharedOption<thread::JoinHandle<()>>,
497
498 /// Holds the result of the inner closure.
499 result: SharedOption<T>,
500
501 /// A handle to the the spawned thread.
502 thread: thread::Thread,
503
504 /// Borrows the parent scope with lifetime `'scope`.
505 _marker: PhantomData<&'scope ()>,
506}
507
508impl<T> ScopedJoinHandle<'_, T> {
509 /// Waits for the thread to finish and returns its result.
510 ///
511 /// If the child thread panics, an error is returned. Note that if panics are implemented by
512 /// aborting the process, no error is returned; see the notes of [std::panic::catch_unwind].
513 ///
514 /// # Panics
515 ///
516 /// This function may panic on some platforms if a thread attempts to join itself or otherwise
517 /// may create a deadlock with joining threads.
518 ///
519 /// # Examples
520 ///
521 /// ```
522 /// use crossbeam_utils::thread;
523 ///
524 /// thread::scope(|s| {
525 /// let handle1 = s.spawn(|_| println!("I'm a happy thread :)"));
526 /// let handle2 = s.spawn(|_| panic!("I'm a sad thread :("));
527 ///
528 /// // Join the first thread and verify that it succeeded.
529 /// let res = handle1.join();
530 /// assert!(res.is_ok());
531 ///
532 /// // Join the second thread and verify that it panicked.
533 /// let res = handle2.join();
534 /// assert!(res.is_err());
535 /// }).unwrap();
536 /// ```
537 pub fn join(self) -> thread::Result<T> {
538 // Take out the handle. The handle will surely be available because the root scope waits
539 // for nested scopes before joining remaining threads.
540 let handle = self.handle.lock().unwrap().take().unwrap();
541
542 // Join the thread and then take the result out of its inner closure.
543 handle
544 .join()
545 .map(|()| self.result.lock().unwrap().take().unwrap())
546 }
547
548 /// Returns a handle to the underlying thread.
549 ///
550 /// # Examples
551 ///
552 /// ```
553 /// use crossbeam_utils::thread;
554 ///
555 /// thread::scope(|s| {
556 /// let handle = s.spawn(|_| println!("A child thread is running"));
557 /// println!("The child thread ID: {:?}", handle.thread().id());
558 /// }).unwrap();
559 /// ```
560 pub fn thread(&self) -> &thread::Thread {
561 &self.thread
562 }
563}
564
565cfg_if! {
566 if #[cfg(unix)] {
567 use std::os::unix::thread::{JoinHandleExt, RawPthread};
568
569 impl<T> JoinHandleExt for ScopedJoinHandle<'_, T> {
570 fn as_pthread_t(&self) -> RawPthread {
571 // Borrow the handle. The handle will surely be available because the root scope waits
572 // for nested scopes before joining remaining threads.
573 let handle = self.handle.lock().unwrap();
574 handle.as_ref().unwrap().as_pthread_t()
575 }
576 fn into_pthread_t(self) -> RawPthread {
577 self.as_pthread_t()
578 }
579 }
580 } else if #[cfg(windows)] {
581 use std::os::windows::io::{AsRawHandle, IntoRawHandle, RawHandle};
582
583 impl<T> AsRawHandle for ScopedJoinHandle<'_, T> {
584 fn as_raw_handle(&self) -> RawHandle {
585 // Borrow the handle. The handle will surely be available because the root scope waits
586 // for nested scopes before joining remaining threads.
587 let handle = self.handle.lock().unwrap();
588 handle.as_ref().unwrap().as_raw_handle()
589 }
590 }
591
592 impl<T> IntoRawHandle for ScopedJoinHandle<'_, T> {
593 fn into_raw_handle(self) -> RawHandle {
594 self.as_raw_handle()
595 }
596 }
597 }
598}
599
600impl<T> fmt::Debug for ScopedJoinHandle<'_, T> {
601 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
602 f.pad("ScopedJoinHandle { .. }")
603 }
604}
605