about summary refs log tree commit diff
path: root/library/std/src/thread
AgeCommit message (Collapse)AuthorLines
2023-03-10Moved thread_local implementation to sys::commonAyush Singh-409/+1
This allows removing all the platform-dependent code from `library/std/src/thread/local.rs` and `library/std/src/thread/mod.rs` Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2023-03-10Move __thread_local_inner to sysAyush Singh-194/+0
Move __thread_local_inner macro in crate::thread::local to crate::sys. Currently, the tidy check does not fail for `library/std/src/thread/local.rs` even though it contains platform specific code. This is beacause target_family did not exist at the time the tidy checks were written [1]. [1]: https://github.com/rust-lang/rust/pull/105861#discussion_r1125841678 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2023-02-19Rollup merge of #104659 - tshepang:reflow, r=workingjubileeDylan DPC-2/+4
reflow the stack size story
2023-01-17refactor[std]: do not use box syntaxjoboet-2/+1
2023-01-12Rollup merge of #106638 - RalfJung:realstd, r=thomccMatthias Krüger-7/+15
std tests: use __OsLocalKeyInner from realstd This is basically the same as https://github.com/rust-lang/rust/pull/100201, but for __OsLocalKeyInner: Some std tests are failing in Miri on Windows because [this static](https://github.com/rust-lang/rust/blob/a377893da2cd7124e5a18c7116cbb70e16dd5541/library/std/src/sys/windows/thread_local_key.rs#L234-L239) is getting duplicated, and Miri does not handle that properly -- Miri does not support this magic `.CRT$XLB` linker section, but instead just looks up this particular hard-coded static in the standard library. This PR lets the test suite use the std static instead of having its own copy. Fixes https://github.com/rust-lang/miri/issues/2754 r? `@thomcc`
2023-01-09std tests: use __OsLocalKeyInner from realstdRalf Jung-7/+15
2023-01-09std test: better type name, clarifying commentRalf Jung-11/+11
2022-12-30Rollup merge of #105903 - joboet:unify_parking, r=m-ou-seMichael Goulet-2/+2
Unify id-based thread parking implementations Multiple platforms currently use thread-id-based parking implementations (NetBSD and SGX[^1]). Even though the strategy does not differ, these are duplicated for each platform, as the id is encoded into an atomic thread variable in different ways for each platform. Since `park` is only called by one thread, it is possible to move the thread id into a separate field. By ensuring that the field is only written to once, before any other threads access it, these accesses can be unsynchronized, removing any restrictions on the size and niches of the thread id. This PR also renames the internal `thread_parker` modules to `thread_parking`, as that name now better reflects their contents. I hope this does not add too much reviewing noise. r? `@m-ou-se` `@rustbot` label +T-libs [^1]: SOLID supports this as well, I will switch it over in a follow-up PR.
2022-12-30Auto merge of #106296 - matthiaskrgr:rollup-ukdbqwx, r=matthiaskrgrbors-1/+1
Rollup of 4 pull requests Successful merges: - #99244 (doc: clearer and more correct Iterator::scan) - #103707 (Replace libstd, libcore, liballoc terminology in docs) - #104182 (`IN6ADDR_ANY_INIT` and `IN6ADDR_LOOPBACK_INIT` documentation.) - #106273 (rustdoc: remove redundant CSS `.source .content { overflow: visible }`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2022-12-30std: rename `Parker::new` to `Parker::new_in_place`, add safe `Parker::new` ↵joboet-1/+1
constructor for SGX
2022-12-30Replace libstd, libcore, liballoc in line comments.jonathanCogan-1/+1
2022-12-29std: unify id-based thread parking implementationsjoboet-1/+1
2022-12-28Catch panics in destruction of TLS valuesFlorian Bartels-7/+14
`destroy_value` is/can be called from C code (libc). Unwinding from Rust to C code is undefined behavior, which is why unwinding is caught here.
2022-12-05fix dupe word typosRageking8-1/+1
2022-11-24Revert "Forbid inlining `thread_local!`'s `__getit` function on Windows"Thom Chiovoloni-16/+9
This reverts commit 3099dfdd9fc1a331eb9c53200b310fa1a06e1573.
2022-11-23Rollup merge of #101368 - thomcc:wintls-noinline, r=ChrisDentonYuki Okushi-9/+16
Forbid inlining `thread_local!`'s `__getit` function on Windows Sadly, this will make things slower to avoid UB in an edge case, but it seems hard to avoid... and really whenever I look at this code I can't help but think we're asking for trouble. It's pretty dodgy for us to leave this as a normal function rather than `#[inline(never)]`, given that if it *does* get inlined into a dynamically linked component, it's extremely unsafe (you get some other thread local, or if you're lucky, crash). Given that it's pretty rare for people to use dylibs on Windows, the fact that we haven't gotten bug reports about it isn't really that convincing. Ideally we'd come up with some kind of compiler solution (that avoids paying for this cost when static linking, or *at least* for use within the same crate...), but it's not clear what that looks like. Oh, and because all this is only needed when we're implementing `thread_local!` with `#[thread_local]`, this patch adjusts the `cfg_attr` to be `all(windows, target_thread_local)` as well. r? ``@ChrisDenton`` See also #84933, which is about improving the situation.
2022-11-22Forbid inlining `thread_local!`'s `__getit` function on WindowsThom Chiovoloni-9/+16
2022-11-21reflow the stack size storyTshepang Mbambo-2/+4
2022-11-20Improve documentation of Stack sizeFabian Hintringer-3/+2
2022-11-12avoid using channels in thread-local testsIbraheem Ahmed-22/+44
2022-10-23Only test pthread_getname_np on linux-gnuJosh Stone-1/+7
2022-10-21Move truncation next to other thread tests for tidyJosh Stone-0/+25
2022-10-15Auto merge of #100579 - joboet:sync_mutex_everywhere, r=thomccbors-15/+12
std: use `sync::Mutex` for internal statics Since `sync::Mutex` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticMutex`. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740. I changed the program argument implementation on Hermit, it does not need `Mutex` but can use atomics like some UNIX systems (ping `@mkroening` `@stlankes).`
2022-10-15Rollup merge of #102773 - joboet:apple_parker, r=thomccDylan DPC-0/+22
Use semaphores for thread parking on Apple platforms Currently we use a mutex-condvar pair for thread parking on Apple systems. Unfortunately, `pthread_cond_timedwait` uses the real-time clock for measuring time, which causes problems when the system time changes. The parking implementation in this PR uses a semaphore instead, which measures monotonic time by default, avoiding these issues. As a further benefit, this has the potential to improve performance a bit, since `unpark` does not need to wait for a lock to be released. Since the Mach semaphores are poorly documented (I could not find availability or stability guarantees for instance), this uses a [dispatch semaphore](https://developer.apple.com/documentation/dispatch/dispatch_semaphore?language=objc) instead. While it adds a layer of indirection (it uses Mach semaphores internally), the overhead is probably negligible. Tested on macOS 12.5. r? ``````@thomcc``````
2022-10-14Auto merge of #102783 - RalfJung:tls, r=thomccbors-12/+25
sync thread_local key conditions exactly with what the macro uses This makes the `cfg` in `mod.rs` syntactically the same as those in `local.rs`. I don't think this should actually change anything, but seems better to be consistent? I looked into this due to https://github.com/rust-lang/rust/issues/102549, but this PR would make it *less* likely that `__OsLocalKeyInner` is going to get provided, so this cannot help with that issue. r? `@thomcc`
2022-10-13smarter way to avoid 'unused' warning when building for testsRalf Jung-9/+2
2022-10-13sync thread_local key conditions exactly with what the macro usesRalf Jung-10/+30
2022-10-13std: use `sync::Mutex` for internal staticsjoboet-15/+12
2022-10-11Rollup merge of #102589 - RalfJung:scoped-threads-dangling, r=m-ou-seYuki Okushi-0/+33
scoped threads: pass closure through MaybeUninit to avoid invalid dangling references The `main` function defined here looks roughly like this, if it were written as a more explicit stand-alone function: ```rust // Not showing all the `'lifetime` tracking, the point is that // this closure might live shorter than `thread`. fn thread(control: ..., closure: impl FnOnce() + 'lifetime) { closure(); control.signal_done(); // A lot of time can pass here. } ``` Note that `thread` continues to run even after `signal_done`! Now consider what happens if the `closure` captures a reference of lifetime `'lifetime`: - The type of `closure` is a struct (the implicit unnameable closure type) with a `&'lifetime mut T` field. References passed to a function are marked with `dereferenceable`, which is LLVM speak for *this reference will remain live for the entire duration of this function*. - The closure runs, `signal_done` runs. Then -- potentially -- this thread gets scheduled away and the main thread runs, seeing the signal and returning to the user. Now `'lifetime` ends and the memory the reference points to might be deallocated. - Now we have UB! The reference that as passed to `thread` with the promise of remaining live for the entire duration of the function, actually got deallocated while the function still runs. Oops. Long-term I think we should be able to use `ManuallyDrop` to fix this without `unsafe`, or maybe a new `MaybeDangling` type. I am working on an RFC for that. But in the mean time it'd be nice to fix this so that Miri with `-Zmiri-retag-fields` (which is needed for "full enforcement" of all the LLVM flags we generate) stops erroring on scoped threads. Fixes https://github.com/rust-lang/rust/issues/101983 r? `@m-ou-se`
2022-10-11Rollup merge of #102412 - joboet:dont_panic, r=m-ou-seYuki Okushi-2/+20
Never panic in `thread::park` and `thread::park_timeout` fixes #102398 `@rustbot` label +T-libs +T-libs-api
2022-10-06std: add thread parking testsjoboet-0/+22
2022-10-03scoped threads: pass closure through MaybeUninit to avoid invalid dangling ↵Ralf Jung-0/+33
references
2022-10-02Rollup merge of #102313 - anirudh24seven:update_sleep_ms_doc, r=Mark-SimulacrumMatthias Krüger-0/+2
Update docs so that deprecated method points to relevant method The docs for the deprecated 'park_timeout_ms' method suggests that the user 'use park_timeout' method instead (at https://doc.rust-lang.org/std/thread/index.html). Making a similar change so that the docs for the deprecated `sleep_ms` method suggest that the user `use sleep` method instead.
2022-09-28std: never panic in `thread::park` and `thread::park_timeout`joboet-2/+20
2022-09-27Address feedbackmejrs-1/+1
2022-09-27Wrapper suggestionsmejrs-0/+1
2022-09-26Update docs so that deprecated method points to relevant methodAnirudh-0/+2
2022-09-09doc: fix minor typoAkhilesh Singhania-1/+1
2022-08-22update and extend some comments, and cfg-out some unused codeRalf Jung-7/+10
2022-08-22std: use realstd fast key when building testsRalf Jung-0/+10
2022-08-18add some Miri-only testsRalf Jung-0/+19
2022-08-10std: optimize thread ID generationjoboet-18/+41
2022-07-23Auto merge of #97925 - the8472:cgroupv1, r=joshtriplettbors-1/+6
Add cgroupv1 support to available_parallelism Fixes #97549 My dev machine uses cgroup v2 so I was only able to test that code path. So the v1 code path is written only based on documentation. I could use some help testing that it works on a machine with cgroups v1: ``` $ x.py build --stage 1 # quota.rs fn main() { println!("{:?}", std::thread::available_parallelism()); } # assuming stage1 is linked in rustup $ rust +stage1 quota.rs # spawn a new cgroup scope for the current user $ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS # should print Ok(3) $ ./quota ``` If it doesn't work as expected an strace, the contents of `/proc/self/cgroups` and the structure of `/sys/fs/cgroups` would help.
2022-07-22[review] mention that runtime may scale with # of mountpointsthe8472-0/+5
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-07-15Fix typo in mod.rsIkko Ashimine-1/+1
constuct -> construct
2022-06-27fix data race in thread::scopeRalf Jung-10/+17
2022-06-20Improve docs for `is_running` to explain use caseJosh Triplett-6/+8
2022-06-15Test NLL fix of bad lifetime inference for reference captured in closure.Frank Steffahn-0/+13
2022-06-11Stabilize scoped threads.Mara Bos-10/+12
2022-06-09add cgroupv1 support to available_parallelismThe 8472-1/+1