Add thread_local macro#16173
Merged
ysbaddaden merged 2 commits intocrystal-lang:masterfrom Oct 3, 2025
Merged
Conversation
This is a simple abstraction over the `ThreadLocal` annotation (for quick access) that also injects a reference into `Thread.current` to keep the value visible to the GC (that can't scan `ThreadLocal` values). There is no support for destructors. When needed just use a class with a finalizer: when the Thread is collected the thread local value will also be collected, which will run the finalizer.
This was referenced Sep 27, 2025
straight-shoota
approved these changes
Sep 29, 2025
Member
straight-shoota
left a comment
There was a problem hiding this comment.
I suppose we can still merge this into 1.18.
It adds an undocumented internal helper macro that's never used so far. So it should not have any effect for the release, but we have it in tree so we can move on with the follow-ups (they'll be merged after 1.18 though).
Merged
ysbaddaden
added a commit
that referenced
this pull request
Oct 3, 2025
This reverts commit 8fda88f. It's an internal type, we don't plan to use it anymore, and #16173 is meant to replace it. It's main feature was the ability to work on every target (unlike the current `@[ThreadLocal]` annotation) and to register destructors... but the annotation can be fixed using the EmulatedTLS LLVM option, and the destructor is crashing on x86_64-darwin when mixing pthreads, unwind and pcre2.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
After different attempts (#15616, #16029, #16168) that exhibited different issues, I finally came up with a dumb abstraction over the
ThreadLocalannotation (for quick access) that also injects a reference intoThread.currentto keep the value visible to the GC (it can't scanThreadLocalvalues).For targets where the
ThreadLocalannotation doesn't work (they could, but that's another issue) we just use the value onThread.current.It has the same drawback as #16168 in that once a thread local is declared, the ivar will always be declared on
Threadand the compiler won't optimize them away if they're unused (maybe it could, someday). We don't expect dozens of thread locals so a few wasted pointers shouldn't be an issue.The macro is mostly useful to avoid repeated boilerplate.
There is no support for destructors. If needed, just use a class with a finalizer. When the Thread is collected the thread local value will also be collected, the finalizer will run, hence acting as a destructor.
Related:
Thread::Local(T);See #16174 and #16175 for use cases (PCRE2, default Random).