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

thread_local: be excruciatingly explicit in dtor code #124387

Merged

Conversation

workingjubilee
Copy link
Member

@workingjubilee workingjubilee commented Apr 25, 2024

Use raw pointers to accomplish internal mutability, and clearly split references where applicable. This reduces the likelihood that any of these parts are misunderstood, either by humans or the compiler's optimizations.

Fixes #124317

r? @joboet

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 25, 2024
Instead, use raw pointers to accomplish internal mutability throughout.
@workingjubilee workingjubilee force-pushed the use-raw-pointers-in-thread-locals branch from 141bba9 to 496fa7f Compare April 25, 2024 19:33
@workingjubilee workingjubilee changed the title Rewrite LazyKeyInner::take to use less &mut Ts thread_local: be excruciatingly explicit in dtor code Apr 25, 2024
@workingjubilee workingjubilee changed the title thread_local: be excruciatingly explicit in dtor code thread_local: be excruciatingly explicit in dtor code Apr 25, 2024
@workingjubilee workingjubilee force-pushed the use-raw-pointers-in-thread-locals branch from 496fa7f to 43f21a6 Compare April 25, 2024 19:45
Comment on lines +241 to 243
let value = inner.take();
dtor_state.set(DtorState::RunningOrHasRun);
drop(value);
Copy link
Member Author

Choose a reason for hiding this comment

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

something that's not clear to me is whether we can simply rearrange this: do dtor_state.set(DtorState::RunningOrHasRun) and then call the more lightweight drop_in_place?

Copy link
Contributor

Choose a reason for hiding this comment

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

We could, and there are some other improvements possible as well, see #116123.

Comment on lines +240 to +241
let Key { inner, dtor_state } = &*ptr;
let value = inner.take();
Copy link
Member Author

Choose a reason for hiding this comment

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

The purpose of adding this two-step is to make it clear, combined with the other change, that we are using internal mutability and not relying on upgrading the pointer to &mut.

library/std/src/sys/thread_local/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@joboet joboet left a comment

Choose a reason for hiding this comment

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

👍

r=me with the updated safety comment

Comment on lines +241 to 243
let value = inner.take();
dtor_state.set(DtorState::RunningOrHasRun);
drop(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

We could, and there are some other improvements possible as well, see #116123.

library/std/src/sys/thread_local/mod.rs Outdated Show resolved Hide resolved
Co-authored-by: joboet <jonasboettiger@icloud.com>
@workingjubilee
Copy link
Member Author

@bors r=joboet rollup

@bors
Copy link
Contributor

bors commented Apr 27, 2024

📌 Commit c63b0ce has been approved by joboet

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 27, 2024
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 27, 2024
…-thread-locals, r=joboet

thread_local: be excruciatingly explicit in dtor code

Use raw pointers to accomplish internal mutability, and clearly split references where applicable. This reduces the likelihood that any of these parts are misunderstood, either by humans or the compiler's optimizations.

Fixes rust-lang#124317

r? `@joboet`
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2024
Rollup of 3 pull requests

Successful merges:

 - rust-lang#124370 (Fix substitution parts having a shifted underline in some cases)
 - rust-lang#124382 (ast: Generalize item kind visiting)
 - rust-lang#124387 (thread_local: be excruciatingly explicit in dtor code)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2024
…iaskrgr

Rollup of 3 pull requests

Successful merges:

 - rust-lang#124382 (ast: Generalize item kind visiting)
 - rust-lang#124387 (thread_local: be excruciatingly explicit in dtor code)
 - rust-lang#124427 (Add missing tests for an ICE)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7c5213c into rust-lang:master Apr 27, 2024
10 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2024
Rollup merge of rust-lang#124387 - workingjubilee:use-raw-pointers-in-thread-locals, r=joboet

thread_local: be excruciatingly explicit in dtor code

Use raw pointers to accomplish internal mutability, and clearly split references where applicable. This reduces the likelihood that any of these parts are misunderstood, either by humans or the compiler's optimizations.

Fixes rust-lang#124317

r? ``@joboet``
@workingjubilee workingjubilee deleted the use-raw-pointers-in-thread-locals branch April 27, 2024 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

The implementation in fast_local.rs seems to violate pointer aliasing rule
4 participants