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

Document thread_local behavior with foreign-created threads #136546

Open
eloff opened this issue Feb 4, 2025 · 2 comments
Open

Document thread_local behavior with foreign-created threads #136546

eloff opened this issue Feb 4, 2025 · 2 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-thread-locals Area: Thread local storage (TLS) T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@eloff
Copy link

eloff commented Feb 4, 2025

Location

https://doc.rust-lang.org/std/macro.thread_local.html
https://doc.rust-lang.org/std/thread/struct.LocalKey.html

Summary

Rust can be used as a static or dynamic library in a host process in another language/runtime. The thread_local documentation makes no mention about how foreign threads behave with respect to to thread locals created via LocalKey or the !thread_local macro.

I think it is safe, with one possible exception. I think the const {} syntax allows Rust to avoid lazy initialization and instead use a more efficient implementation that may rely on statically allocated thread-local storage (TLS) instead of dynamically allocated TLS keys. I do not see how this would work with foreign-created threads that call into Rust.

Researching this has proved difficult, I think the docs should clarify the behavior and what is guaranteed and what isn't when it comes to foreign-created threads.

@eloff eloff added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Feb 4, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 4, 2025
@ChrisDenton
Copy link
Member

Rust doesn't really have a runtime to speak of so it can work inside other languages runtimes. With constant values it will use the TLS capabilities of the binary format itself, if supported by the target. So it should work whatever the "host" process.

@eloff
Copy link
Author

eloff commented Feb 5, 2025

That's better than I expected, could we document something to that effect for the thread_local crate, that it works regardless of whether Rust creates the threads or not?

@jieyouxu jieyouxu added A-thread-locals Area: Thread local storage (TLS) T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-thread-locals Area: Thread local storage (TLS) T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants