-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Allow the global allocator to use thread-local storage and std::thread::current() #144465
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
base: master
Are you sure you want to change the base?
Conversation
I think this merits some libs-api discussion (unless that's already happened? I didn't quickly find it). Landing this IMO implies at least an implicit guarantee (even if not necessarily stable without actual docs) that we don't introduce global allocator usage in thread local code. I think we should have some discussion and either commit to that or not. And we should discuss where we draw the line (e.g., is other runtime-like code in std supposed to do this? For example, environment variables or other bits that need allocation early, potentially even before main starts -- is that OK to call into the allocator for?) OTOH, if we can make a weaker guarantee here while still serving the purpose, that may also be good to look at? For example, can we guarantee that a pattern like dhat's for ignoring re-entrant alloc/dealloc calls is safe? i.e., that we don't need allocations for non-Drop thread locals? Or if we can only do that for a limited set, perhaps std could define a public thread local for allocators to use? |
Yes, this should probably be properly discussed.
You make a good point here about environment variables. It is very common to allow configuration and debugging of allocators through them. I'd like to at least guarantee that
I think it's good to brainstorm a bit what would be needed for a global allocator, although I don't think an allocator needs all that much from
I don't think there's much difference in constraint for the stdlib between guaranteeing this for all
Not without putting undue burden on the allocator. |
I did some investigation into some of the problems of making
The above seems feasible to me, although I also had another thought: I don't know if if !alloc_is_init() {
init_alloc();
// Now the allocator works and may be re-entrant.
spawn_background_threads();
} So I'm perfectly happy if we don't make While investigating the above I also found the following additional blocker for making
To answer my own question: |
I've added a commit to use a spinlock for I also investigated environment variables and... it's hopeless without a new API. The current API returns owned |
Currently the thread-local storage implementation uses the
Global
allocator if it needs to allocate memory in some places. This effectively means the global allocator can not use thread-local variables. This is a shame as an allocator is precisely one of the locations where you'd really want to use thread-locals. We also see that this lead to hacks such as #116402, where we detect re-entrance and abort.So I've made the places where I could find allocation happening in the TLS implementation use the
System
allocator instead. I also applied this change to the storage allocated for aThread
handle so that it may be used care-free in the global allocator as well, for e.g. registering it to a central place or parking primitives.r? @joboet