Use memory_order_acquire for spinlock instead of acq_rel#169
Open
HFTrader wants to merge 1 commit intoefficient:masterfrom
Open
Use memory_order_acquire for spinlock instead of acq_rel#169HFTrader wants to merge 1 commit intoefficient:masterfrom
HFTrader wants to merge 1 commit intoefficient:masterfrom
Conversation
The standard spinlock pattern is acquire on lock, release on unlock. The release semantics on the lock path (from acq_rel) are unnecessary — when acquiring a lock, you need to ensure subsequent operations stay after the acquisition (acquire), but there is nothing to release at that point. This restores the original behavior from the initial commit (e57758e, Jan 2014), which correctly used memory_order_acquire. The change to acq_rel was introduced in 8530b8a (Dec 2016). On x86 this is a no-op (test_and_set compiles to XCHG which is a full barrier regardless). On ARM and POWER, acq_rel generates an extra barrier instruction that acquire does not, making this a measurable improvement on those architectures.
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.
Summary
memory_order_acquireforlock()andtry_lock()instead ofmemory_order_acq_relacq_relwas introduced in 8530b8a (Dec 2016) with the rationale "since test-and-set is both a read and a write operation, it should [use acq_rel]" — however the "write" in test-and-set is the lock flag itself, which does not need release semantics during acquisitionImpact
test_and_setcompiles toXCHGwhich is a full barrier regardless of the memory order specifiedacq_relgenerates both an acquire and release barrier, whileacquiregenerates only the acquire barrier. This is a measurable improvement on weakly-ordered architecturesContext
This aligns with the three open PRs improving the spinlock (#147, #149, #158) and the discussion in issue #146.
Test plan
unlock()method