RFR: 8186166: Generalize Atomic::cmpxchg with templates

Andrew Haley aph at redhat.com
Fri Aug 18 14:08:44 UTC 2017

On 18/08/17 14:21, Andrew Haley wrote:
> More seriously, IMO: accessing a stored pointer to X as though it had
> been stored as a void* is certainly undefined behaviour, by the rule
> you point out above.  It seems to me that you are worried about some
> things that are relatively minor, while ignoring things that are
> serious.
> If you followed the approach of never converting the type of the
> pointer to dest to some other point, *but instead converting the types
> of exchange_value and compare_value* everything would be fine.  This
> whole thing could be done with no UB at all.

Looking at your patch again, I think that I've got this wrong: you're
doing it the right way here:

struct Atomic::CmpxchgImpl<
  T*, D*, U*,
  typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value &&
                    IsSame<typename RemoveCV<D>::type,
                           typename RemoveCV<U>::type>::value>::type>
  D* operator()(T* exchange_value, D* volatile* dest, U* compare_value,
               cmpxchg_memory_order order) const {
    // Allow derived to base conversion, and adding cv-qualifiers.
    D* new_value = exchange_value;
    // Don't care what the CV qualifiers for compare_value are,
    // but we need to match D* when calling platform support.
    D* old_value = const_cast<D*>(compare_value);

My apologies.

Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

More information about the hotspot-dev mailing list