Repository navigation
Check target definitions to see if more should have cfg(target_thread_local) enabled. #91659
Description
Out of curiosity, I ran a script to print out the list of targets where cfg(target_thread_local) (indicating support for #[thread_local]) is not true.
The results (and script) are available here: https://gist.github.com/thomcc/766d76e4da35ce1a73b93ccd548fd143
This looks somewhat wrong to me — I believe several of these should have support for static/linker thread local (or at least __thread/__declspec(thread) has worked there for me). Here are some items in that list which are... fairly suspicious to me.
- All the x86_64 (and probably other arches) unix ELF targets, like the various BSDs.
- i686-pc-windows-msvc, x86_64-pc-windows-gnu and i686-pc-windows-gnu
- aarch64-apple-ios (probably), aarch64-apple-tvos (almost certainly)
- ...
Playing around with1 clang, gcc, and msvc a bit indicates that (the ones I can easily test of these) should have it, and probably others.
My suspicion is:
- Some of these are listed as not having thread locals by accident. It seems very easy to forget to change something like this when updating or adding a target
- Some of these are listed as unsupported because technically we might support very old versions.
If the issue is about old versions, I guess a question would be what should the cfg do in situations where the support is version-dependent...
This sounds like a tricky thing to decide! Worth noting: support for TLS is what caused us to choose 10.7 as the minimum supported version for macOS: #11927, but I guess back then2 we might not have had a #[cfg(target_thread_local)]?.
But if nothing else, libstd probably has more concrete ideas about what it supports, and this causes it to be far more pessimistic in std::thread_local!.
And more concretely: I'm pretty sure if I used #[thread_local] in a malloc, I'd be pretty annoyed if it couldn't support, say, FreeBSD, since I know that at least GCC/Clang support __thread on that target.
Footnotes
-
I think the correct thing to compare against would be
_Thread_local/__thread/__declspec(thread)rather than C++'sthread_local— these don't have ctor/dtor support, and thus map more directly to Rust's#[thread_local]. That said, I don't really see a difference in what I've checked. ↩ -
I don't feel doing that much spelunking, seems likely that given the numeric gap between the macOS issue and the
thread_localissue, that things probably worked very differently! ↩
Activity
thomcc commentedon Dec 8, 2021
@rustbot modify labels: +A-thread-locals +F-thread_local
ChrisDenton commentedon Dec 8, 2021
Hm, yeah. I'm pretty sure there's no reason for any Windows targets not to support it. Or at least it's very inconsistent that
i686-uwp-windows-msvcenablestarget_thread_localbuti686-pc-windows-msvcdoesn't. But perhaps there was a historic reason for being cautious here.Btw, the name of the spec configuration option confused me. I initially overlooked that
is_elf_tlscan be true on non-elf targets. Perhaps the name should match the feature name? Is renaming possible?nagisa commentedon Dec 17, 2021
Porting from the issue just above this comment…
We have a test
ui/thread-local/tls.rswhich only ignores emscripten targets and uses#[thread_local]unconditionally. This suggests to me that any target we test in CI actually supports#[thread_local]in some shape or form.Rollup merge of rust-lang#92042 - ChrisDenton:msvc-static-tls, r=nagisa
mati865 commentedon Jul 23, 2023
Fixing
*-windows-gnurequires some skills and time.Simply adding only
has_thread_local: trueto the spec will cause binaries to segfault (caught by multiple Rust ui tests) because LLVM uses native TLS and GCC uses emuTLS.Using
has_thread_local: true, force_emulated_tls: true(as it should be) looks better but symbols are not improperly exported. Linking of stage1 libstd will fail with:Contents of CGU (
codegen-units = 1):For this to work Rust would have to not try to export symbols without
__emutlsdecoration and instead export only those with it. Analogical change would have be made for symbols importing and of course dllexport/dllimport has to be taken into account as well.madsmtm commentedon Aug 21, 2024
Thread locals were enabled for all Apple targets in #104385.
@rustbot label -O-ios
ChrisDenton commentedon Aug 22, 2024
This is enabled for all windows-msvc and windows-gnullvm targets, I believe it's just windows-gnu that's left as far as Windows is concerned.
Rollup merge of rust-lang#159021 - mati865:mingw-native-tls, r=ChrisD…
Rollup merge of rust-lang#159021 - mati865:mingw-native-tls, r=ChrisD…
Unrolled build for #159021 in rollup 163609
Rollup merge of #159021 - mati865:mingw-native-tls, r=ChrisDenton
Rollup merge of #159021 - mati865:mingw-native-tls, r=ChrisDenton