RFR: 8374692: ZGC: Convert zRemembered to use Atomic<T>

Axel Boldt-Christmas aboldtch at openjdk.org
Tue Jan 27 09:08:02 UTC 2026


On Fri, 16 Jan 2026 09:20:02 GMT, Stefan Karlsson <stefank at openjdk.org> wrote:

>> Convert usages of AtomicAccess to Atomic<T> in zRemembered.
>> 
>> Testing:
>>  * GHA
>>  * HotSpot GC tests with ZGC linux x64 and linux aarch64
>
> src/hotspot/share/gc/z/zRemembered.cpp line 419:
> 
>> 417:     }
>> 418: 
>> 419:     const BitMap::idx_t res = _claimed.compare_exchange(prev, page_index + 1, memory_order_relaxed);
> 
> If this were changed to `compare_set` then we could get rid of the `res` local.

And move `BitMap::idx_t prev = _claimed.load_relaxed();` inside the loop and make it `const`? 

I think that would be fine, in most of our CAS loops we use that pattern of reading the prev value twice, rather than use the value read by the CAS. 

It is already a bit strange that we reload the _claimed value when finding the next bit. Rather than just read it once. And also why are we using a CAS to set the terminal state. I wonder if we could write this loops as follows:

```C++
  for (;;) {
    const BitMap::idx_t prev = _claimed.load_relaxed();

    if (prev == _bm->size()) {
      // Terminal state
      return false;
    }

    const BitMap::idx_t page_index = _bm->find_first_set_bit(prev);
    if (page_index == _bm->size()) {
      // No more bit, set terminal state
      _claimed.store_relaxed(page_index);
      return false;
    }

    if (!_claimed.compare_set(prev, page_index + 1, memory_order_relaxed)) {
      // Interference, retry
      continue;
    }

    // Found bit - look around for page or forwarding to scan

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29269#discussion_r2730981068


More information about the hotspot-gc-dev mailing list