Skip to content

Conversation

@shipilev
Copy link
Member

@shipilev shipilev commented Jan 9, 2026

Following up on JDK-8367013 improvement, there is an opportunity to rewrite some of our low-level cas(oldv, newv) == oldv patterns to more straight-forward compare-and-set helper method. This is useful when you do not actually care about the result that used to be in memory or that is currently in memory, you only care if CAS succeeded or not.

Java atomics already have this duality; arguably due to historical timeline of having compare-and-set before introducing compare-and-exchange.

Found this when converting Epsilon to Atomic (JDK-8374876), where this method would simplify the code a bit. I looked around current uses of compare_exchange and rewrote them to compare_set to show what simplifications are possible.

Additional testing:

  • Linux x86_64 server fastdebug, all

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8374878: Add Atomic<T>::compare_set (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/29135/head:pull/29135
$ git checkout pull/29135

Update a local copy of the PR:
$ git checkout pull/29135
$ git pull https://git.openjdk.org/jdk.git pull/29135/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 29135

View PR using the GUI difftool:
$ git pr show -t 29135

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/29135.diff

Using Webrev

Link to Webrev Comment

@shipilev
Copy link
Member Author

shipilev commented Jan 9, 2026

@kimbarrett, tell me what you think. I can polish and test it further if this is sensible.

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 9, 2026

👋 Welcome back shade! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jan 9, 2026

@shipilev This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8374878: Add Atomic<T>::compare_set

Reviewed-by: kbarrett, stefank

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 8 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the hotspot hotspot-dev@openjdk.org label Jan 9, 2026
@openjdk
Copy link

openjdk bot commented Jan 9, 2026

@shipilev The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 9, 2026
@mlbridge
Copy link

mlbridge bot commented Jan 9, 2026

Webrevs

Copy link
Member

@stefank stefank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but let's at least wait for Kim's review.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 9, 2026
@shipilev
Copy link
Member Author

shipilev commented Jan 9, 2026

/reviewers 2

@openjdk
Copy link

openjdk bot commented Jan 9, 2026

@shipilev
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jan 9, 2026
Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this addition. One optional suggestion.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 9, 2026
@shipilev
Copy link
Member Author

shipilev commented Jan 9, 2026

First run of Linux x86_64 server fastdebug, all yields no failures. I polished the patch a little, and now running the second iteration. If it is still clean, and I have no other comments, I'll integrate.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jan 9, 2026
Copy link

@kimbarrett kimbarrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Note that the recent updates invalidated prior approvals. So here's a re-approval.


// Note that using "bottom" here might fail, since a pop_local might
// have decremented it.
assert_not_underflow(localBot, newAge.top());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-existing] This assertion seems weird. It is checking newAge.top() after
already using been installed. Maybe it is misplaced? Or maybe it is checking
the wrong thing? Needs to be investigated. I've filed
https://bugs.openjdk.org/browse/JDK-8374915

@shipilev
Copy link
Member Author

/reviewers 1

@openjdk
Copy link

openjdk bot commented Jan 11, 2026

@shipilev
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 1 (with at least 1 Reviewer).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 11, 2026
@shipilev
Copy link
Member Author

Testing is still clean. I am integrating now, thanks.

/integrate

@openjdk
Copy link

openjdk bot commented Jan 11, 2026

Going to push as commit 3368948.
Since your change was applied there have been 8 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jan 11, 2026
@openjdk openjdk bot closed this Jan 11, 2026
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jan 11, 2026
@openjdk openjdk bot removed the rfr Pull request is ready for review label Jan 11, 2026
@openjdk
Copy link

openjdk bot commented Jan 11, 2026

@shipilev Pushed as commit 3368948.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Comment on lines +490 to +492
return _value.compare_set(decay(compare_value),
decay(new_value),
order);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is subtly different from what we usually did with AtomicAccess / Atomic compare_exchange on translated types. We used to check equality with the bool operator==(const T&, constT&) rather than bool operator==(const PrimitiveConversions::Translate<T>::Decayed&, const PrimitiveConversions::Translate<T>::Decayed&).

For the PrimitiveConversions::Translate we currently have I do not think there is an issues as they are memcmp equivalent in both cases. But we may introduce a PrimitiveConversion in the future where this is not the case and this function returns false when using recover and bool operator==(const T&, constT&) would have returned true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants