From 770bd57ea5bb369067103841004dfe595ab22490 Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Sat, 13 May 2017 20:34:40 +0200 Subject: Add `'static` and `Send` constraints explanations to `thread::spawn` Part of #29378. --- src/libstd/thread/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/libstd/thread') diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 200368be275..d807424129e 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -367,6 +367,26 @@ impl Builder { /// want to specify the stack size or the name of the thread, use this API /// instead. /// +/// As you can see in the signature of `spawn` there are two constraints on +/// both the closure given to `spawn` and its return value, let's explain them: +/// +/// - The `'static` constraint means that the closure and its return value +/// must have a lifetime of the whole program execution. The reason for this +/// is that threads can `detach` and outlive the lifetime they have been +/// created in. +/// Indeed if the thread, and by extension its return value, can outlive their +/// caller, we need to make sure that they will be valid afterwards, and since +/// we *can't* know when it will return we need to have them valid as long as +/// possible, that is until the end of the program, hence the `'static` +/// lifetime. +/// - The [`Send`] constraint is because the closure will need to be passed +/// *by value* from the thread where it is spawned to the new thread. Its +/// return value will need to be passed from the new thread to the thread +/// where it is `join`ed. +/// As a reminder, the [`Send`] marker trait, expresses that it is safe to be +/// passed from thread to thread. [`Sync`] expresses that it is safe to have a +/// reference be passed from thread to thread. +/// /// # Panics /// /// Panics if the OS fails to create a thread; use [`Builder::spawn`] @@ -433,6 +453,8 @@ impl Builder { /// [`panic`]: ../../std/macro.panic.html /// [`Builder::spawn`]: ../../std/thread/struct.Builder.html#method.spawn /// [`Builder`]: ../../std/thread/struct.Builder.html +/// [`Send`]: ../../std/marker/trait.Send.html +/// [`Sync`]: ../../std/marker/trait.Sync.html #[stable(feature = "rust1", since = "1.0.0")] pub fn spawn(f: F) -> JoinHandle where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static -- cgit 1.4.1-3-g733a5 From 66237afce454db9235525dc715238568c492f54b Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Sun, 21 May 2017 12:38:07 -0700 Subject: Fix building without backtrace feature, which was broken in ca8b754 Fixes #42139 --- src/libstd/rt.rs | 5 +++++ src/libstd/thread/mod.rs | 3 +++ 2 files changed, 8 insertions(+) (limited to 'src/libstd/thread') diff --git a/src/libstd/rt.rs b/src/libstd/rt.rs index acff7faf8a7..06fd838ea06 100644 --- a/src/libstd/rt.rs +++ b/src/libstd/rt.rs @@ -35,6 +35,8 @@ fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize { use sys_common; use sys_common::thread_info; use thread::Thread; + #[cfg(not(feature = "backtrace"))] + use mem; sys::init(); @@ -53,9 +55,12 @@ fn lang_start(main: fn(), argc: isize, argv: *const *const u8) -> isize { sys::args::init(argc, argv); // Let's run some code! + #[cfg(feature = "backtrace")] let res = panic::catch_unwind(|| { ::sys_common::backtrace::__rust_begin_short_backtrace(main) }); + #[cfg(not(feature = "backtrace"))] + let res = panic::catch_unwind(mem::transmute::<_, fn()>(main)); sys_common::cleanup(); res.is_err() }; diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 154406a1d8b..75717abb4ad 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -359,9 +359,12 @@ impl Builder { } unsafe { thread_info::set(imp::guard::current(), their_thread); + #[cfg(feature = "backtrace")] let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| { ::sys_common::backtrace::__rust_begin_short_backtrace(f) })); + #[cfg(not(feature = "backtrace"))] + let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f)); *their_packet.get() = Some(try_result); } }; -- cgit 1.4.1-3-g733a5