rust/src/libstd
bors bcb05a0ab2 Auto merge of #55043 - oliver-giersch:unchecked_thread_spawning, r=alexcrichton
Unchecked thread spawning

# Summary

Add an unsafe interface for spawning lifetime-unrestricted threads for
library authors to build less-contrived, less-hacky safe abstractions
on.

# Motivation

So a few years back scoped threads were entirely removed from the Rust
stdlib, the reason being that it was possible to leak the scoped thread's
join guards without resorting to unsafe code, which meant the concept
was not completely safe, either.
Only a maximally-restrictive safe API for thread spawning was kept in the
stdlib, that requires `'static` lifetime bounds on both the thread closure
and its return type.
A number of 3rd party libraries sprung up to offer their implementations
for safe scoped threads implementations.
These work by essentially hiding the join guards from the user, thus
forcing them to join at the end of an (internal) function scope.

However, since these libraries have to use the maximally restrictive
thread spawning API, they have to resort to some very contrived manipulations
and subversions of Rust's type system to basically achieve what this commit does
with some minimal restructuring of the current code and exposing a new unsafe
function signature for spawning threads without lifetime restrictions.
Obviously this is unsafe, but its main use would be to allow library authors
to write safe abstractions with and around it.
To further illustrate my point, here's a quick summary of the hoops that,
for instance `crossbeam`, has to jump through to spawn a lifetime unrestricted
thread, all of which would not be necessary if an unsafe API existed as part
of the stdlib:

1. Allocate an `Arc<Option<T>>` on the heap where the result with type
`T: 'a` will go (in practice requires `Mutex` or `UnsafeCell` as well).

2. Wrap the desired thread closure with lifetime bound `'a` into another
closure (also `..: 'a`) that returns `()`, executes the inner closure and
writes its result into the pre-allocated `Option<T>`.

3. Box the wrapping closure, cast it to a trait object (`FnBox`) and
(unsafely) transmute its lifetime bound from `'a` to `'static`.

So while this new `spawn_unchecked` function is certainly not very relevant
for general use, since scoped threads are so common I think it makes sense
to expose an interface for libraries implementing these to build on.
The changes implemented are also very minimal: The current `spawn` function
(which internally contains unsafe code) is moved into an unsafe `spawn_unchecked`
function, which the safe function then wraps around.

# Issues

- ~~so far, no documentation for the new function (yet)~~
- the name of the function might be controversial, as `*_unchecked` more commonly
indicates that some sort of runtime check is omitted (`unrestricted` may be
more fitting)
- if accepted, it might make sense to add a freestanding `thread::spawn_unchecked`
function similar to the current `thread::spawn` for convenience.
2018-10-28 21:34:12 +00:00
..
collections Auto merge of #53804 - RalfJung:ptr-invalid, r=nagisa 2018-09-16 18:03:39 +00:00
ffi make CStr::from_bytes_with_nul_unchecked() a const fn 2018-10-02 04:25:40 -07:00
io Auto merge of #53517 - phungleson:fix-impl-from-for-error, r=frewsxcv 2018-10-07 22:00:04 +00:00
net Small changes to fix documentation auto compile issues 2018-10-11 15:35:48 -04:00
os Move std::os::raw::c_void into libcore and re-export in libstd 2018-09-14 16:19:59 +01:00
prelude
sync fix typos in various places 2018-10-23 15:56:25 +02:00
sys Prefer unwrap_or_else to unwrap_or in case of function calls/allocations 2018-10-19 09:45:45 +02:00
sys_common Improve output if no_lookup_host_duplicates fails 2018-09-15 17:17:35 +02:00
tests
thread Auto merge of #55043 - oliver-giersch:unchecked_thread_spawning, r=alexcrichton 2018-10-28 21:34:12 +00:00
alloc.rs Bump to 1.31.0 and bootstrap from 1.30 beta 2018-09-27 20:52:53 -07:00
ascii.rs
build.rs Prefer unwrap_or_else to unwrap_or in case of function calls/allocations 2018-10-19 09:45:45 +02:00
Cargo.toml std: Implement TLS for wasm32-unknown-unknown 2018-10-11 09:57:55 -07:00
env.rs
error.rs Rollup merge of #53523 - phungleson:fix-impl-from-for-std-error, r=GuillaumeGomez 2018-10-04 12:20:05 +02:00
f32.rs Fix doc for new copysign functions 2018-10-24 15:19:23 -07:00
f64.rs Fix doc for new copysign functions 2018-10-24 15:19:23 -07:00
fs.rs "(using ..." doesn't have the matching ")" 2018-10-10 01:09:18 -07:00
future.rs Remove spawning from task::Context 2018-09-19 15:01:19 -07:00
keyword_docs.rs Hopefully fix compile error 2018-10-23 22:27:02 +02:00
lib.rs Stabilize min_const_fn 2018-10-05 10:36:14 +02:00
macros.rs Fix incorrect link in println! documentation 2018-10-14 08:14:21 -04:00
memchr.rs
num.rs
panic.rs it's auto traits that make for automatic implementations 2018-10-08 17:44:33 +02:00
panicking.rs rustc: Allow #[no_mangle] anywhere in a crate 2018-10-06 13:57:30 -07:00
path.rs Implement FromStr for PathBuf 2018-10-17 15:54:00 +02:00
primitive_docs.rs Rollup merge of #55247 - peterjoel:peterjoel-prim-char-doc-example, r=joshtriplett 2018-10-25 14:31:07 +02:00
process.rs Documents From implementations for Stdio 2018-10-24 22:02:32 +02:00
rt.rs
time.rs