JDK-8303277 : Add Atomic bit operations
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 21
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2023-02-28
  • Updated: 2023-02-28
  • Resolved: 2023-02-28
Related Reports
Duplicate :  
Description
Atomically setting/clearing/flipping bits in bytes/words is sometimes needed and currently performed in an ad-hoc manner by the code that needs it. We could add something like (per [~jrose] suggestion):

template<typename D, typename T>
inline bool
Atomic::change_bits(D volatile* dest,
                    T dest_mask,  // mask of bits which may be changed (0=>none)
                    int effect) { // 1=>set, 0=>clear, -1=>toggle
  for (;;) {
    u1 d0 = *dest;
    u1 d1 = d0 ^ (dest_mask & (effect < 0 ? (T)-1 : effect ? d0 : ~d0));
    if (d0 == d1)  return false;
    d1 = Atomic::cmpxchg(dest, d0, d1);
    if (d0 == d1)  return true;
  }
}

or split it out into bit_set, bit_clear, bit_toggle.

Code that could potentially use this is in:

java_lang_String::test_and_set_flag
ClassLoaderData::clear_claim
ClassLoaderData::try_claim
set_defined_by_cds_in_class_path
BitMap::par_set_bit/BitMap::par_clear_bit/BitMap::par_put_range_within_word
AccessFlags::atomic_set[clear]_bits
Comments
JDK-8293117 already exists.
28-02-2023