Open
Description
std::thread_local
, std::thread::local
, std::thread::local_impl
, std::sys_common::thread_local_key
, std::sys_common::thread_local_dtor
, std::sys::thread_local_key
, etc. etc. are all messy and form quite a confusing maze. Just look at this map I tried to draw of it all:
AAAaaaaAaa
Time to clean it up.
And maybe also fix some bugs while we're at it. ^^
- Initial name and module cleanup without touching implementation: Restructure and rename std thread_local internals to make it less of a maze #110861Remove unused
Key
type: Remove unused std::sys_common::thread_local_key::Key #110898Stop calling everythingKey
, because most of those things are actually not keys, and dropLazyKeyInner
Reduce the amount of code in thethread_local!{}
expansion. Make TLS accessors closures that return pointers #125525Use*const T
rather than&'static T
when it's not really'static
: Make TLS accessors closures that return pointers #125525Store thread ID and stack guard range in thread locals without destructors (on platforms with#[thread_local]
)?- Stack guard range: Refactor stack overflow handling #123265
Makethread::current
thread local more efficient and make it available in (most) TLS destructors: std: makethread::current
available in allthread_local!
destructors #127912Detect stack overflow in TLS destructors: std: detect stack overflows in TLS destructors on UNIX #131282
Related issues to solve, maybe:
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
joboet commentedon Apr 27, 2023
LazyKeyInner
unfortunately can't just beOnceCell
because of reentrant initialization: BecauseOnceCell::get_or_init
returns a shared reference to the data that lives for the duration ofself
, it cannot change the value after the first inner initialization.LazyKeyInner
can do that (and does) because references cannot outliveLocalKey::with
.m-ou-se commentedon Apr 27, 2023
@joboet In what situation would that
mem::replace
call replace aSome
instead of aNone
? Is that the right behaviour? Do we have a test for that situation?joboet commentedon Apr 27, 2023
It's very cursed, but this works. I don't think it's currently tested, however.
m-ou-se commentedon Apr 27, 2023
That just seems like a bug to me. A (thread local)
static &'static str
(not mut, not a cell) shouldn't be able to have different values at different times (on the same thread).joboet commentedon Apr 27, 2023
Maybe 😄? It's sound however, as there can be no references to the data from the first initialization when the new value is written.
m-ou-se commentedon Apr 27, 2023
Sure, but so would OnceCell be. ^^ And I think users should be able to safely assume that an immutable
static
never changes. (And as a bonus we get to delete code.)m-ou-se commentedon Apr 27, 2023
I did some software archeology and found the original issue and PR that introduced that mem::replace call: #30228 and #30267.
The issue was originally about thread locals, but then most of the discussion was about drop on assignment in general. The issue description suggests:
Without a preference for any of these solutions. The PR also doesn't include a reason for picking one solution over the other.
So I don't think the mem::replace behaviour was purposely picked over another solution (like dropping the other value or panicking).
workingjubilee commentedon Apr 27, 2023
Rollup merge of rust-lang#110898 - m-ou-se:remove-unused-thread-local…
m-ou-se commentedon Apr 28, 2023
In this case I don't think anyone actually relies on LazyKeyInner being a OnceOrMaybeTwiceIfYouTryReallyHardCell instead of just a OnceCell. ^^
41 remaining items