Commit Graph

268 Commits

Author SHA1 Message Date
Linus Färnstrand
9fc0833235 Stop accessing module level int consts via crate::<Ty> 2020-04-20 23:38:06 +02:00
Thomas Bächler
c8140a88f6 Return NonZeroU64 from ThreadId::as_u64.
As discussed in #67939, this allows turning Option<ThreadId> into Option<NonZeroU64> which
can then be stored inside an AtomicU64.
2020-03-21 19:48:23 +01:00
Mark Rousskov
d9a7db901e Add an unstable conversion from thread ID to u64
We see multiple cases inside rustc and ecosystem code where ThreadId is
transmuted to u64, exploiting the underlying detail. This is suboptimal
(can break unexpectedly if we change things in std).

It is unlikely that ThreadId will ever need to be larger than u64 --
creating even 2^32 threads over the course of a program is quite hard,
2^64 is even harder. As such, we do not choose to return a larger sized
type (e.g. u128). If we choose to shrink ThreadId in the future, or
otherwise change its internals, it is likely that a mapping to u64 will
still be applicable (though may become more complex).
2020-01-06 10:56:13 -05:00
Mark Rousskov
a06baa56b9 Format the world 2019-12-22 17:42:47 -05:00
Ross MacArthur
f7256d28d1 Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
Pyry Kontio
8568204f4e Try with crate::error::Error 2019-11-07 01:45:46 +09:00
Pyry Kontio
4317263a31 Fix the Error linking. 2019-11-06 16:59:53 +09:00
Pyry Kontio
f1bc4ef170 Addressed review comments. 2019-11-06 14:48:23 +09:00
Pyry Kontio
002c1c74d9 Improve std:🧵:Result documentation 2019-11-05 19:23:12 +09:00
Alex Crichton
1d06058a77 std: Reduce checks for feature = "backtrace"
This is a stylistic change to libstd to reduce the number of checks of
`feature = "backtrace"` now that we unconditionally depend on the
`backtrace` crate and rely on it having an empty implementation.
otherwise.
2019-09-25 06:43:49 -07:00
Vadim Petrochenkov
42a317a1cd Remove run-pass test suites 2019-07-27 18:56:17 +03:00
Taiki Endo
ccb9dac5ed Fix intra-doc link resolution failure on re-exporting libstd 2019-05-04 23:48:57 +09:00
Aleksey Kladov
be69785ea2 Use more realistic example for thread builder
Stack size of 10 **bytes** does not make any sense: the minimal possible stack size is greater anyway.
2019-04-18 14:58:38 +03:00
CrLF0710
6635fbed4c Eliminate FnBox usages from libstd. 2019-04-10 09:40:44 +08:00
Mazdak Farrokhzad
379c380a60 libstd: deny(elided_lifetimes_in_paths) 2019-03-31 12:56:51 +02:00
Josh Stone
a2c4562690 Rollup merge of #59460 - xfix:include-id-in-thread-debug, r=Amanieu
Include id in Thread's Debug implementation

Since Rust 1.19.0, `id` is a stable method, so there is no reason to not include it in Debug implementation.
2019-03-27 18:15:44 -07:00
Konrad Borowski
ba21e0b368 Include id in Thread's Debug implementation
Since Rust 1.19.0, id is a stable method, so there is no reason to
not include it in Debug implementation.
2019-03-27 12:28:17 +01:00
bors
54479c624c Auto merge of #59136 - jethrogb:jb/sgx-std-test, r=sanxiyn
SGX target: fix std unit tests

This fixes some tests and some code in the SGX sys implementation to make the `std` unit test suite pass.

#59009 must be merged first.
2019-03-26 01:48:34 +00:00
Jethro Beekman
f229422cc1 SGX target: fix std unit tests 2019-03-25 11:31:19 -07:00
Simon Sapin
c1d9191fa5 Add a test for size_of Option<ThreadId> 2019-03-20 19:04:38 +01:00
Simon Sapin
8cf720bd19 Make Option<ThreadId> no larger than ThreadId, with NonZeroU64 2019-03-19 14:00:13 +01:00
Taiki Endo
93b6d9e086 libstd => 2018 2019-02-28 04:06:15 +09:00
Alexander Regueiro
b87363e763 tests: doc comments 2019-02-10 23:42:32 +00:00
Jethro Beekman
2ec0e85305 Print a slightly clearer message when failing to spawn a thread 2019-01-22 18:55:12 +05:30
Mark Rousskov
2a663555dd Remove licenses 2018-12-25 21:08:33 -07:00
bors
3a75e80557 Auto merge of #56157 - RalfJung:park, r=nagisa
expand thread::park explanation

Cc @carllerche @parched @stjepang
2018-12-10 12:19:47 +00:00
Alexander Regueiro
ee89c088b0 Various minor/cosmetic improvements to code 2018-12-07 23:53:34 +00:00
Jethro Beekman
c559216ad0 Change sys::Thread::new to take the thread entry as Box<dyn FnBox() + 'static>̣ 2018-12-06 20:37:15 +05:30
Ralf Jung
76cd8f0594 improve wording 2018-12-03 15:14:31 +01:00
Marius Nuennerich
73b656bbb3 Fix small typo in comment 2018-11-27 18:57:55 +01:00
Ralf Jung
7b6ad7a960 make park/unpark example more realistic 2018-11-23 11:04:16 +01:00
Ralf Jung
2d46ae7c37 expand thread::park explanation 2018-11-22 10:54:04 +01:00
Andy Russell
4e35cbb22e fix various typos in doc comments 2018-11-13 14:45:31 -05:00
James Duley
d3e71e4986 thread::unpark: Avoid notifying with mutex locked.
This means when the other thread wakes it can continue right away
instead of having to wait for the mutex.

Also add some comments explaining why the mutex needs to be locked in
the first place.
2018-10-30 22:54:35 +00:00
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
kennytm
e4ac447851 Rollup merge of #54646 - vn971:fix_std_thread_sleep, r=frewsxcv
improve documentation on std:🧵:sleep
2018-10-18 12:54:42 +08:00
oliver-giersch
7849aeddb9 adds tracking issue number 2018-10-16 22:42:14 +02:00
oliver-giersch
30bfdc8720 Merge pull request #5 from oliver-giersch/master
sync with upstream
2018-10-15 14:38:34 +02:00
oliver-giersch
ebb9d289db adds feature gate to doc-test (example) 2018-10-15 14:14:17 +02:00
oliver-giersch
ee5703cbbc adds missing method call parentheses 2018-10-15 13:47:27 +02:00
oliver-giersch
9d7a83862b fixes misplaced semicolon 2018-10-15 13:22:39 +02:00
oliver-giersch
986549e9f5 adds doc for Builder::spawn_unchecked 2018-10-15 12:48:24 +02:00
oliver-giersch
bf9dc98655 remove unnecessary lifetime bounds
generic lifetime bound `'a` can be inferred.
2018-10-14 14:28:01 +02:00
oliver-giersch
a52b474b52 Update mod.rs
removes trailing whitespaces, replaces TODO with FIXME
2018-10-13 18:24:47 +02:00
oliver-giersch
719a59586a Update mod.rs
removes unnecessary `unsafe`, adds `unstable` attribute
2018-10-13 17:28:47 +02:00
oliver-giersch
fbb95689d6 adds unsafe thread::Builder::spawn_unchecked function
moves code for `thread::Builder::spawn` into new public unsafe function `spawn_unchecked` and transforms `spawn` into a safe wrapper.
2018-10-13 14:34:31 +02:00
Vasya Novikov
7a0fa95336 improve docs on thread::sleep 2018-10-11 21:37:30 +03:00
Alex Crichton
cbe9f33b8b std: Implement TLS for wasm32-unknown-unknown
This adds an implementation of thread local storage for the
`wasm32-unknown-unknown` target when the `atomics` feature is
implemented. This, however, comes with a notable caveat of that it
requires a new feature of the standard library, `wasm-bindgen-threads`,
to be enabled.

Thread local storage for wasm (when `atomics` are enabled and there's
actually more than one thread) is powered by the assumption that an
external entity can fill in some information for us. It's not currently
clear who will fill in this information nor whose responsibility it
should be long-term. In the meantime there's a strategy being gamed out
in the `wasm-bindgen` project specifically, and the hope is that we can
continue to test and iterate on the standard library without committing
to a particular strategy yet.

As to the details of `wasm-bindgen`'s strategy, LLVM doesn't currently
have the ability to emit custom `global` values (thread locals in a
`WebAssembly.Module`) so we leverage the `wasm-bindgen` CLI tool to do
it for us. To that end we have a few intrinsics, assuming two global values:

* `__wbindgen_current_id` - gets the current thread id as a 32-bit
  integer. It's `wasm-bindgen`'s responsibility to initialize this
  per-thread and then inform libstd of the id. Currently `wasm-bindgen`
  performs this initialization as part of the `start` function.
* `__wbindgen_tcb_{get,set}` - in addition to a thread id it's assumed
  that there's a global available for simply storing a pointer's worth
  of information (a thread control block, which currently only contains
  thread local storage). This would ideally be a native `global`
  injected by LLVM, but we don't have a great way to support that right
  now.

To reiterate, this is all intended to be unstable and purely intended
for testing out Rust on the web with threads. The story is very likely
to change in the future and we want to make sure that we're able to do
that!
2018-10-11 09:57:55 -07:00
Artem Varaksa
ddcec08a54 Fix typo in libstd/thread/mod.rs: remove unnecessary comma 2018-10-06 20:09:54 +03:00
Vasya Novikov
b63517a2c3 update wording for thread::sleep 2018-10-01 11:01:15 +03:00