-
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 |
We talked about this in today's @rust-lang/libs-api meeting. We were supportive of doing this, and we agreed that using TLS in an allocator seems desirable. We also decided there's no point in doing this if we don't make it a guarantee of support. Thus, labeling this libs-api. @rfcbot merge Also, before merging this we'd like to see the documentation for TLS updated to make this guarantee. |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Separately, it would also be useful for the global allocator documentation to provide an explicit safelist of other things you're allowed to use. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Done.
Where? On the |
So with this, for the first time, a program with a At the very least, this should be documented with that attribute. |
Much like Ralf, I am quite surprised at the "allocator fork" occurring here. Now, I would like to note that there is precedent for this. One of the early pains I encountered in replacing the system allocator with a custom allocator was that the loader will use the system allocator for loading the code (and constants, etc...) regardless. Thus, to an extent, there are already allocations bypassing the global allocator. Still, up until now, this was a well-defined set. Loaded sections of library/binary would be allocated with the system allocator, all further allocations would be allocated with the global allocator. The divide is clear. And one can (try to) write their own loader if they wish to change the statu quo. This changes removes control from the hands of the (allocator) developer. It may be fine. It may be the beginning of a slippery slope. For the point at hand, is there any reason that the thread-local destructors could not be registered in an intrusive linked list instead? That is, each thread-local variable requiring a destructor would be accompanied by a thread-local: struct Node {
pointer: *mut u8,
destructor: unsafe extern "C" fn(*mut u8),
next: *mut Node,
} And destruction would simply consist of popping the first destructor of the linked-list and executing it in a loop. (This doesn't solve all problems identified in this PR, but it may solve the most salient one) |
Well, yes and no. I think technically yes, this is the first time specifically However, Note that this doesn't affect |
On some platforms we can only install a key (that is, a pointer) into thread-local storage, which requires boxing of the thread-local variable. We must allocate on these platforms. See here: rust/library/std/src/sys/thread_local/os.rs Line 101 in 8410440
We also require allocation for
Well, considering there are platforms which hard require allocation for thread-locals, and every platform requiring allocation for |
@RalfJung @matthieu-m Sorry, forgot to tag you earlier, awaiting your response. |
Not much for me to respond to, it's up to t-libs-api to make a call here. I don't care strongly either way myself, but this seemed like a potential issue not mentioned in the discussion so I am curious what t-libs-api thinks. The one point I do care strongly about is that this must be very clearly documented as part of this PR, that the Rust runtime will bypass the |
@RalfJung I've added a commit which adds a footnote to |
Thanks! However, I don't think I agree with delegating this to a footnote, that seems a bit too easy to miss -- though I admit I don't know how exactly footnotes even render, we rarely use them (which is another argument against using them here, IMO). |
@RalfJung It renders as such: ![]() |
Note that libc will already often use the system allocator behind our backs, such as for |
Fixes #115209.
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