1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_ATOMIC_HPP
  26 #define SHARE_VM_RUNTIME_ATOMIC_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "metaprogramming/integerTypes.hpp"
  30 #include "metaprogramming/isIntegral.hpp"
  31 #include "metaprogramming/isPointer.hpp"
  32 #include "utilities/align.hpp"
  33 #include "utilities/debug.hpp"
  34 #include "utilities/macros.hpp"
  35 
  36 enum cmpxchg_memory_order {
  37   memory_order_relaxed,
  38   // Use value which doesn't interfere with C++2011. We need to be more conservative.
  39   memory_order_conservative = 8
  40 };
  41 
  42 class Atomic : AllStatic {
  43   template<typename T> class Never: public FalseType {};
  44 
  45   template <typename T>
  46   inline static void specialized_store(T store_value, volatile T* dest) {
  47     STATIC_ASSERT(sizeof(T) <= size_t(BytesPerWord)); // Does the machine support atomic wide accesses?
  48     (void)const_cast<T&>(*dest = store_value);
  49   }
  50 
  51   template <typename T>
  52   inline static T specialized_load(const volatile T* dest) {
  53     STATIC_ASSERT(sizeof(T) <= size_t(BytesPerWord)); // Does the machine support atomic wide accesses?
  54     return *dest;
  55   }
  56 
  57   template <typename T>
  58   inline static T specialized_add(T add_value, volatile T* dest) {
  59     STATIC_ASSERT(Never<T>::value);
  60     return add_value;
  61   }
  62 
  63   template <typename T>
  64   inline static void specialized_inc(volatile T* dest) {
  65     add(1, dest);
  66   }
  67 
  68   template <typename T>
  69   inline static void specialized_dec(volatile T* dest) {
  70     add(-1, dest);
  71   }
  72 
  73   template <typename T>
  74   inline static T specialized_xchg(T exchange_value, volatile T* dest) {
  75     STATIC_ASSERT(Never<T>::value);
  76     return exchange_value;
  77   }
  78 
  79   template <typename T>
  80   inline static T specialized_cmpxchg(T exchange_value, volatile T* dest, T compare_value, cmpxchg_memory_order order) {
  81     STATIC_ASSERT(Never<T>::value);
  82     return exchange_value;
  83   }
  84 
  85  public:
  86   // Atomic operations on 64-bit types are not available on all 32-bit
  87   // platforms. If atomic ops on 64-bit types are defined here they must only
  88   // be used from code that verifies they are available at runtime and
  89   // can provide an alternative action if not - see supports_cx8() for
  90   // a means to test availability.
  91 
  92   // The memory operations that are mentioned with each of the atomic
  93   // function families come from src/share/vm/runtime/orderAccess.hpp,
  94   // e.g., <fence> is described in that file and is implemented by the
  95   // OrderAccess::fence() function. See that file for the gory details
  96   // on the Memory Access Ordering Model.
  97 
  98   // All of the atomic operations that imply a read-modify-write action
  99   // guarantee a two-way memory barrier across that operation. Historically
 100   // these semantics reflect the strength of atomic operations that are
 101   // provided on SPARC/X86. We assume that strength is necessary unless
 102   // we can prove that a weaker form is sufficiently safe.
 103 
 104   // Atomically store to a location
 105   // See comment above about using 64-bit atomics on 32-bit platforms
 106   template <typename T, typename U>
 107   inline static void store(T store_value, volatile U* dest);
 108 
 109   // The store_ptr() member functions are deprecated. Use store() instead.
 110   static void store_ptr(intptr_t store_value, volatile intptr_t* dest) {
 111     store(store_value, dest);
 112   }
 113 
 114   static void store_ptr(void*    store_value, volatile void*     dest) {
 115     store((intptr_t)store_value, (volatile intptr_t*)dest);
 116   }
 117 
 118   // Atomically load from a location
 119   // See comment above about using 64-bit atomics on 32-bit platforms
 120   template <typename T>
 121   inline static T load(volatile T* src);
 122 
 123   // Atomically add to a location. Returns updated value. add*() provide:
 124   // <fence> add-value-to-dest <membar StoreLoad|StoreStore>
 125   // add(I1 v, I* d)
 126   // add(I1 v, P* d)
 127   // where I, I1 are integral types, P is a pointer type.
 128   // Functional behavior is modelled on *dest += add_value.
 129   template <typename T, typename U>
 130   inline static U add(T add_value, volatile U* dst);
 131 
 132   template <typename T, typename U>
 133   inline static U* add(T add_value, U* volatile* dst);
 134 
 135   // The add_ptr() member functions are deprecated. Use add() instead.
 136   static intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 137     return add(add_value, dest);
 138   }
 139 
 140   static void*    add_ptr(intptr_t add_value, volatile void*     dest) {
 141     return (void*)add(add_value, (volatile intptr_t*)dest);
 142   }
 143 
 144   // Atomically increment location. inc*() provide:
 145   // <fence> increment-dest <membar StoreLoad|StoreStore>
 146   // Functional behavior is modelled on *dest++
 147   template <typename T>
 148   inline static void inc(volatile T* dest);
 149 
 150   template <typename T>
 151   inline static void inc(T* volatile* dest);
 152 
 153   // The inc_ptr member functions are deprecated. Use inc() instead.
 154   static void inc_ptr(volatile intptr_t* dest) {
 155     inc(dest);
 156   }
 157 
 158   static void inc_ptr(volatile void*     dest) {
 159     inc((volatile intptr_t*)dest);
 160   }
 161 
 162   // Atomically decrement a location. dec*() provide:
 163   // <fence> decrement-dest <membar StoreLoad|StoreStore>
 164   // Functional behavior is modelled on *dest--
 165   template <typename T>
 166   inline static void dec(volatile T* dest);
 167 
 168   template <typename T>
 169   inline static void dec(T* volatile* dest);
 170 
 171   // The dec_ptr member functions are deprecated. Use dec() instead.
 172   static void dec_ptr(volatile intptr_t* dest) {
 173     dec(dest);
 174   }
 175 
 176   static void dec_ptr(volatile void*     dest) {
 177     dec((volatile intptr_t*)dest);
 178   }
 179 
 180   // Performs atomic exchange of *dest with exchange_value. Returns old
 181   // prior value of *dest. xchg*() provide:
 182   // <fence> exchange-value-with-dest <membar StoreLoad|StoreStore>
 183   template <typename T, typename U>
 184   inline static U xchg(T exchange_value, volatile U* dest);
 185 
 186   // The xchg_ptr() member functions are deprecated. Use xchg() instead.
 187   static intptr_t xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 188     return xchg(exchange_value, dest);
 189   }
 190 
 191   static void*    xchg_ptr(void*    exchange_value, volatile void*     dest) {
 192     return (void*)xchg((intptr_t)exchange_value, (volatile intptr_t*)dest);
 193   }
 194 
 195   // Performs atomic compare of *dest and compare_value, and exchanges
 196   // *dest with exchange_value if the comparison succeeded. Returns prior
 197   // value of *dest. cmpxchg*() provide:
 198   // <fence> compare-and-exchange <membar StoreLoad|StoreStore>
 199   // See comment above about using 64-bit atomics on 32-bit platforms
 200   template <typename T, typename U, typename V>
 201   inline static U cmpxchg(T exchange_value, volatile U* dest, V compare_value, cmpxchg_memory_order order = memory_order_conservative);
 202 
 203   // The cmpxchg_ptr member functions are deprecated. Use cmpxchg() instead.
 204   inline static intptr_t cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t*  dest,
 205                                      intptr_t compare_value, cmpxchg_memory_order order = memory_order_conservative) {
 206     return cmpxchg(exchange_value, dest, compare_value, order);
 207 
 208   }
 209 
 210   inline static void*    cmpxchg_ptr(void*    exchange_value, volatile void*      dest,
 211                                      void*    compare_value, cmpxchg_memory_order order = memory_order_conservative) {
 212     return (void*)cmpxchg((intptr_t)exchange_value, (volatile intptr_t*)dest, (intptr_t)compare_value, order);
 213   }
 214 };
 215 
 216 // internal implementation
 217 
 218 template <typename T, typename U>
 219 inline void Atomic::store(T store_value, volatile U* dest) {
 220   typedef typename IntegerTypes::Signed<U>::type Raw;
 221   U store_value_cast = store_value;
 222   specialized_store(IntegerTypes::cast_to_signed(store_value_cast), reinterpret_cast<volatile Raw*>(dest));
 223 }
 224 
 225 template <typename T>
 226 inline T Atomic::load(volatile T* src) {
 227   typedef typename IntegerTypes::Signed<T>::type Raw;
 228   return IntegerTypes::cast<T>(specialized_load(reinterpret_cast<const volatile Raw*>(src)));
 229 }
 230 
 231 template <typename T, typename U>
 232 inline U Atomic::add(T add_value, volatile U* dst) {
 233   STATIC_ASSERT(IsIntegral<T>::value);
 234   STATIC_ASSERT(IsIntegral<U>::value);
 235   typedef typename IntegerTypes::Signed<U>::type Raw;
 236   // Allow -Wconversion or the like to complain about unsafe conversions.
 237   U value = add_value;
 238   Raw raw_value = IntegerTypes::cast_to_signed(value);
 239   Raw result = specialized_add(raw_value, reinterpret_cast<volatile Raw*>(dst));
 240   return IntegerTypes::cast<U>(result);
 241 }
 242 
 243 template <typename T, typename U>
 244 inline U* Atomic::add(T add_value, U* volatile* dst) {
 245   STATIC_ASSERT(IsIntegral<T>::value);
 246   typedef typename IntegerTypes::Signed<U*>::type Raw;
 247   ptrdiff_t value = add_value;
 248   Raw raw_value = IntegerTypes::cast_to_signed(value * sizeof(U));
 249   Raw result = specialized_add(raw_value, reinterpret_cast<volatile Raw*>(dst));
 250   return IntegerTypes::cast<U*>(result);
 251 }
 252 
 253 template <typename T>
 254 inline void Atomic::inc(volatile T* src) {
 255   STATIC_ASSERT(IsIntegral<T>::value);
 256   typedef typename IntegerTypes::Signed<T>::type Raw;
 257   specialized_inc(reinterpret_cast<volatile Raw*>(src));
 258 }
 259 
 260 template <typename T>
 261 inline void Atomic::inc(T* volatile* src) {
 262   if (sizeof(T) != 1) {
 263     add(1, src);
 264   } else {
 265     typedef typename IntegerTypes::Signed<T*>::type Raw;
 266     specialized_inc(reinterpret_cast<volatile Raw*>(src));
 267   }
 268 }
 269 
 270 template <typename T>
 271 inline void Atomic::dec(volatile T* src) {
 272   STATIC_ASSERT(IsIntegral<T>::value);
 273   typedef typename IntegerTypes::Signed<T>::type Raw;
 274   specialized_dec(reinterpret_cast<volatile Raw*>(src));
 275 }
 276 
 277 template <typename T>
 278 inline void Atomic::dec(T* volatile* src) {
 279   if (sizeof(T) != 1) {
 280     add(-1, src);
 281   } else {
 282     typedef typename IntegerTypes::Signed<T*>::type Raw;
 283     specialized_dec(reinterpret_cast<volatile Raw*>(src));
 284   }
 285 }
 286 
 287 template <typename T, typename U>
 288 inline U Atomic::xchg(T exchange_value, volatile U* dest) {
 289   typedef typename IntegerTypes::Signed<U>::type Raw;
 290   U exchange_value_cast = exchange_value;
 291   Raw result = specialized_xchg(IntegerTypes::cast_to_signed(exchange_value_cast),
 292                                 reinterpret_cast<volatile Raw*>(dest));
 293   return IntegerTypes::cast<U>(result);
 294 }
 295 
 296 template <typename T, typename U, typename V>
 297 inline U Atomic::cmpxchg(T exchange_value, volatile U* dest, V compare_value, cmpxchg_memory_order order) {
 298   typedef typename IntegerTypes::Signed<U>::type Raw;
 299   U exchange_value_cast = exchange_value;
 300   U compare_value_cast = compare_value;
 301   Raw result = specialized_cmpxchg(IntegerTypes::cast_to_signed(exchange_value_cast),
 302                                    reinterpret_cast<volatile Raw*>(dest),
 303                                    IntegerTypes::cast_to_signed(compare_value_cast), order);
 304   return IntegerTypes::cast<U>(result);
 305 }
 306 
 307 // platform specific in-line definitions - must come before shared definitions
 308 
 309 #include OS_CPU_HEADER(atomic)
 310 
 311 // shared in-line definitions
 312 
 313 #ifndef VM_HAS_SPECIALIZED_CMPXCHG_BYTE
 314 /*
 315  * This is the default implementation of byte-sized cmpxchg. It emulates 8-bit-sized cmpxchg
 316  * in terms of 32-bit-sized cmpxchg. Platforms may override this by defining their own inline definition
 317  * as well as defining VM_HAS_SPECIALIZED_CMPXCHG_BYTE. This will cause the platform specific
 318  * implementation to be used instead.
 319  */
 320 template <>
 321 inline int8_t Atomic::specialized_cmpxchg<int8_t>(int8_t exchange_value, volatile int8_t* dest,
 322                                                   int8_t compare_value, cmpxchg_memory_order order) {
 323   volatile int32_t* dest_int =
 324       reinterpret_cast<volatile int32_t*>(align_down(dest, sizeof(int32_t)));
 325   size_t offset = pointer_delta(dest, dest_int, 1);
 326   int32_t cur = *dest_int;
 327   int8_t* cur_as_bytes = reinterpret_cast<int8_t*>(&cur);
 328 
 329   // current value may not be what we are looking for, so force it
 330   // to that value so the initial cmpxchg will fail if it is different
 331   cur_as_bytes[offset] = compare_value;
 332 
 333   // always execute a real cmpxchg so that we get the required memory
 334   // barriers even on initial failure
 335   do {
 336     // value to swap in matches current value ...
 337     int32_t new_value = cur;
 338     // ... except for the one byte we want to update
 339     reinterpret_cast<int8_t*>(&new_value)[offset] = exchange_value;
 340 
 341     int32_t res = cmpxchg(new_value, dest_int, cur, order);
 342     if (res == cur) break; // success
 343 
 344     // at least one byte in the int changed value, so update
 345     // our view of the current int
 346     cur = res;
 347     // if our byte is still as cur we loop and try again
 348   } while (cur_as_bytes[offset] == compare_value);
 349 
 350   return cur_as_bytes[offset];
 351 }
 352 
 353 #endif // VM_HAS_SPECIALIZED_CMPXCHG_BYTE
 354 
 355 template <>
 356 inline int16_t Atomic::specialized_add<int16_t>(int16_t add_value, volatile int16_t* dest) {
 357   // Most platforms do not support atomic add on a 2-byte value. However,
 358   // if the value occupies the most significant 16 bits of an aligned 32-bit
 359   // word, then we can do this with an atomic add of (add_value << 16)
 360   // to the 32-bit word.
 361   //
 362   // The least significant parts of this 32-bit word will never be affected, even
 363   // in case of overflow/underflow.
 364   //
 365   // Use the ATOMIC_SHORT_PAIR macro (see macros.hpp) to get the desired alignment.
 366 #ifdef VM_LITTLE_ENDIAN
 367   assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
 368   int32_t new_value = Atomic::add(int32_t(add_value) << 16, (volatile int32_t*)(dest-1));
 369 #else
 370   assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
 371   int32_t new_value = Atomic::add(int32_t(add_value) << 16, (volatile int32_t*)(dest));
 372 #endif
 373   return (int16_t)(new_value >> 16); // preserves sign
 374 }
 375 
 376 template <>
 377 inline void Atomic::specialized_inc<int16_t>(volatile int16_t* dest) {
 378   (void)add(int16_t(1), dest);
 379 }
 380 
 381 template <>
 382 inline void Atomic::specialized_dec<int16_t>(volatile int16_t* dest) {
 383   (void)add(int16_t(-1), dest);
 384 }
 385 
 386 #endif // SHARE_VM_RUNTIME_ATOMIC_HPP