You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've tried rearranging this in the C++; however it seems quite resistant to ordering change; perhaps PGO is incorrectly enforcing the order here?
Aside: I assume it needs to be a cmpxchg rather than just a volatile write because it may have changed from a lock to a lock+hashcode or perhaps signaling? Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling, so it could just be a volatile write to unlock a non-recursive non-signaled lock? (as JIT_MonExit_Portable is quite expensive in the code I'm looking at as 3rd highest cost method)
Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling
Do you mean having a first class lock type (e.g. System.Threading.Lock) that is a simple lock without the syncblock baggage? I think that would be useful.
The first image seems to indicate that the bad speculation is mostly caused by machine clears and not branch misprediction. I'm not sure what exactly would be causing those, perhaps just the cmpxchg or false sharing with other data? In .NET 6 too I don't see the ideal code ordering. Was this happening on a benchmark?
Aside: I assume it needs to be a cmpxchg rather than just a volatile write because it may have changed from a lock to a lock+hashcode or perhaps signaling? Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling, so it could just be a volatile write to unlock a non-recursive non-signaled lock?
The fast paths for exiting AwareLock and LowLevelLock also currently use CAS to determine if a waiter needs to be signaled. It may be possible to use a volatile write with another full memory barrier instead.
Object locking is done entirely in managed code now and does not need special helpers for unframed/framed code paths. It is highly unlikely that the same pattern/issue is still present after all those changes.
Activity
jkotas commentedon Apr 10, 2020
Do you mean having a first class lock type (e.g.
System.Threading.Lock) that is a simple lock without the syncblock baggage? I think that would be useful.benaadams commentedon Apr 10, 2020
Exactly. Have raised issue #34812
kouvel commentedon Sep 8, 2022
The first image seems to indicate that the bad speculation is mostly caused by machine clears and not branch misprediction. I'm not sure what exactly would be causing those, perhaps just the
cmpxchgor false sharing with other data? In .NET 6 too I don't see the ideal code ordering. Was this happening on a benchmark?The fast paths for exiting
AwareLockandLowLevelLockalso currently use CAS to determine if a waiter needs to be signaled. It may be possible to use a volatile write with another full memory barrier instead.VSadov commentedon Jan 7, 2026
As of #118371 the entire AwareLock deal is gone.
Object locking is done entirely in managed code now and does not need special helpers for unframed/framed code paths. It is highly unlikely that the same pattern/issue is still present after all those changes.