Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure and rename std thread_local internals to make it less of a maze #110861

Merged
merged 3 commits into from
Apr 27, 2023

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Apr 26, 2023

Every time I try to work on std's thread local internals, it feels like I'm trying to navigate a confusing maze made of macros, deeply nested modules, and types with multiple names/aliases. Time to clean it up a bit.

This PR:

  • Exports Key with its own name (Key), instead of __LocalKeyInner
  • Uses pub macro to put __thread_local_inner into a (unstable, hidden) module, removing #[macro_export], removing it from the crate root.
  • Removes the __ from __thread_local_inner.
  • Removes a few unnecessary allow_internal_unstable features from the macros
  • Removes the libstd_thread_internals feature. (Merged with thread_local_internals.)
    • And removes it from the unstable book
  • Gets rid of the deeply nested modules for the Key definitions (mod fast / mod os / mod statik).
  • Turns a #[cfg] mess into a single cfg_if, now that there's no #[macro_export] anymore that breaks with cfg_if.
  • Simplifies the cfg_if conditions to not repeat the conditions.
  • Removes useless normalize-stderr-test, which were left over from when the Key types had different names on different platforms.
  • Removes a seemingly unnecessary realstd re-export on cfg(test).

This PR changes nothing about the thread local implementation. That's for a later PR. (Which should hopefully be easier once all this stuff is a bit cleaned up.)

@m-ou-se m-ou-se added C-cleanup Category: PRs that clean code up or issues documenting cleanup. A-thread-locals Area: Thread local storage (TLS) labels Apr 26, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 26, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 26, 2023
@rustbot

This comment was marked as off-topic.

@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 26, 2023

@bors rollup=never

(If there's breakage it is probably platform specific.)

@workingjubilee
Copy link
Member

I was asked to take a look at this. r? @workingjubilee

@rustbot rustbot assigned workingjubilee and unassigned thomcc Apr 26, 2023
@m-ou-se
Copy link
Member Author

m-ou-se commented Apr 26, 2023

(Btw, this patch is probably easier to review with whitespace changes hidden, because of the removed indentation for the nested modules: https://github.com/rust-lang/rust/pull/110861/files?diff=unified&w=1)

enum DtorState {
Unregistered,
Registered,
RunningOrHasRun,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BeRunning?
BeganRunning
RunningStart
WouldEnglishPleaseGetARealPerfectiveTense

( these are silly thoughts, pay me no mind. )

Comment on lines +125 to +129
// This data structure has been carefully constructed so that the fast path
// only contains one branch on x86. That optimization is necessary to avoid
// duplicated tls lookups on OSX.
//
// LLVM issue: https://bugs.llvm.org/show_bug.cgi?id=41722
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're doing spring cleaning: This comment is 4 years old. While the LLVM issue hasn't been closed, is this concern re: assembly generation still true? Well, I suppose that can be another issue. llvm/llvm-project#41067

@workingjubilee
Copy link
Member

Oh I see now, it's... ugh. Both of these views suck!

@bors r+

@bors
Copy link
Contributor

bors commented Apr 26, 2023

📌 Commit 12fee7b has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 26, 2023
@joboet
Copy link
Member

joboet commented Apr 26, 2023

The realstd export under #[cfg(test)] is not unnecessary, it was added in #100201 to avoid duplicating global state when testing std. While it's removal won't break anything, perhaps it should be added again in a new implementation.

@bors
Copy link
Contributor

bors commented Apr 26, 2023

⌛ Testing commit 12fee7b with merge cb9aa8c...

@bors
Copy link
Contributor

bors commented Apr 27, 2023

☀️ Test successful - checks-actions
Approved by: workingjubilee
Pushing cb9aa8c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 27, 2023
@bors bors merged commit cb9aa8c into rust-lang:master Apr 27, 2023
@rustbot rustbot added this to the 1.71.0 milestone Apr 27, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (cb9aa8c): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

@m-ou-se m-ou-se deleted the thread-local-restructure branch April 27, 2023 14:37
@RalfJung
Copy link
Member

Removes a seemingly unnecessary realstd re-export on cfg(test).

This was necessary, see #100201 and #106638. I guess I need to leave more detailed comments in the code.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 1, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? `@m-ou-se`
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 2, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? ``@m-ou-se``
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 3, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? ```@m-ou-se```
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 3, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? ````@m-ou-se````
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request May 3, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? `````@m-ou-se`````
JohnTitor added a commit to JohnTitor/rust that referenced this pull request May 5, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang#100201 and rust-lang#106638, which got reverted by rust-lang#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? ``````@m-ou-se``````
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request Jul 18, 2023
avoid duplicating TLS state between test std and realstd

This basically re-lands rust-lang/rust#100201 and rust-lang/rust#106638, which got reverted by rust-lang/rust#110861. This works around 2 Miri limitations:
- Miri doesn't support the magic linker section that our Windows TLS support relies on, and instead knows where in std to find the symbol that stores the thread callback.
- For macOS, Miri only supports at most one destructor to be registered per thread.

The 2nd would not be very hard to fix (though the intended destructor order is unclear); the first would be a lot of work to fix. Neither of these is a problem for regular Rust code, but in the std test suite we have essentially 2 copies of the std code and then these both become issues. To avoid that we have the std test crate import the TLS code from the real std instead of having its own copy.

r? ``````@m-ou-se``````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-thread-locals Area: Thread local storage (TLS) C-cleanup Category: PRs that clean code up or issues documenting cleanup. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants