-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Windows thread-local keyless drop #90442
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
//! Implements thread-local destructors that are not associated with any | ||
//! particular data. | ||
|
||
#![unstable(feature = "thread_local_internals", issue = "none")] | ||
#![cfg(target_thread_local)] | ||
use super::c; | ||
|
||
// Using a per-thread list avoids the problems in synchronizing global state. | ||
#[thread_local] | ||
static mut DESTRUCTORS: Vec<(*mut u8, unsafe extern "C" fn(*mut u8))> = Vec::new(); | ||
|
||
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) { | ||
DESTRUCTORS.push((t, dtor)); | ||
} | ||
|
||
// See windows/thread_local_keys.rs for an explanation of this callback function. | ||
// The short version is that all the function pointers in the `.CRT$XL*` array | ||
// will be called whenever a thread or process starts or ends. | ||
|
||
#[link_section = ".CRT$XLD"] | ||
#[doc(hidden)] | ||
#[used] | ||
pub static TLS_CALLBACK: unsafe extern "system" fn(c::LPVOID, c::DWORD, c::LPVOID) = tls_callback; | ||
|
||
pub use crate::sys_common::thread_local_dtor::register_dtor_fallback as register_dtor; | ||
unsafe extern "system" fn tls_callback(_: c::LPVOID, reason: c::DWORD, _: c::LPVOID) { | ||
if reason == c::DLL_THREAD_DETACH || reason == c::DLL_PROCESS_DETACH { | ||
// Drop all the destructors. | ||
// | ||
// Note: While this is potentially an infinite loop, it *should* be | ||
// the case that this loop always terminates because we provide the | ||
// guarantee that a TLS key cannot be set after it is flagged for | ||
// destruction. | ||
while let Some((ptr, dtor)) = DESTRUCTORS.pop() { | ||
(dtor)(ptr); | ||
} | ||
// We're done so free the memory. | ||
DESTRUCTORS.shrink_to_fit(); | ||
ChrisDenton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all seems pretty reasonable to me! Could the previously-registered "at thread exit" function get a new hook to call into this module though? I think it 'd probably be best to avoid these
#[link_section]
annotations if we can and have them consolidated into one place if possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've used a normal Rust function for running the new dtors and simply made the exit function call it. Is this what you had in mind?