-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
atomic.cpp: Spinlock powering shared_ptr atomics can lead to priority inversion #370
Comments
I believe you've filed a duplicate of #86. If so, which one do you want to keep? |
Also, how can this possibly be fixed without breaking ABI, given that existing object files could be calling |
This bug is not "make shared_ptr stop using a common spinlock", it is "make common lock shared_ptr uses less bad". If we didn't have to worry about XP I would wholesale replace it with an SRWLOCK. |
Ah, changing what the separately compiled functions do. I agree that that would be ABI-safe. Thanks! |
I think it can be done in scope of #593 as follows:
But need to link atomic satellite. Is linking atomic satellite acceptable here? |
@AlexGuteniev: I think it would be better to attempt to detect and use SRWLOCK here since that would engage on more OSes. |
(But if you still disagree, I can revert this part) |
Resolves microsoft#370 , resolves microsoft#680
Fixes #370. Co-authored-by: Stephan T. Lavavej <[email protected]>
Describe the bug
shared_ptr's atomic functions (e.g.
std::atomic_store
) are powered by an external lock present in our separately compiled machinery,_Lock_shared_ptr_spin_lock
and_Unlock_shared_ptr_spin_lock
. This was written to use a plain spin lock with no mitigation to go to sleep if spinning is taking too long, nor is there any mitigation for memory bandwidth consumption.STL/stl/src/atomic.cpp
Lines 13 to 34 in aa0a7a3
In single threaded scenarios this is particularly bad when a low priority thread currently holds the spinlock, and a high priority thread spins "effectively forever".
In an ABI breaking release, it would be nice to reuse the low order bit of the reference count control block pointer in the shared_ptr; but even without an ABI breaking release we could do better by replacing the spinlock entirely with something like
SRWLOCK
on Vista and later, implementing exponential backoff, or relying on C++20std::atomic
waiting features once we have those implemented.Also tracked by Developer Community as DevCom-716238 and Microsoft-internal VSO-975564.
The text was updated successfully, but these errors were encountered: