< prev index next >

src/share/vm/runtime/atomic.hpp

Print this page
rev 13452 : imported patch Atomic_cmpxchg
rev 13453 : imported patch Atomic_add
rev 13454 : [mq]: Atomic_add_v2


   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/enableIf.hpp"
  30 #include "metaprogramming/isIntegral.hpp"

  31 #include "metaprogramming/isSame.hpp"
  32 #include "metaprogramming/primitiveConversions.hpp"
  33 #include "metaprogramming/removeCV.hpp"

  34 #include "utilities/align.hpp"
  35 #include "utilities/macros.hpp"
  36 
  37 enum cmpxchg_memory_order {
  38   memory_order_relaxed,
  39   // Use value which doesn't interfere with C++2011. We need to be more conservative.
  40   memory_order_conservative = 8
  41 };
  42 
  43 class Atomic : AllStatic {
  44  public:
  45   // Atomic operations on jlong types are not available on all 32-bit
  46   // platforms. If atomic ops on jlongs are defined here they must only
  47   // be used from code that verifies they are available at runtime and
  48   // can provide an alternative action if not - see supports_cx8() for
  49   // a means to test availability.
  50 
  51   // The memory operations that are mentioned with each of the atomic
  52   // function families come from src/share/vm/runtime/orderAccess.hpp,
  53   // e.g., <fence> is described in that file and is implemented by the


  65   inline static void store    (jshort   store_value, jshort*   dest);
  66   inline static void store    (jint     store_value, jint*     dest);
  67   // See comment above about using jlong atomics on 32-bit platforms
  68   inline static void store    (jlong    store_value, jlong*    dest);
  69   inline static void store_ptr(intptr_t store_value, intptr_t* dest);
  70   inline static void store_ptr(void*    store_value, void*     dest);
  71 
  72   inline static void store    (jbyte    store_value, volatile jbyte*    dest);
  73   inline static void store    (jshort   store_value, volatile jshort*   dest);
  74   inline static void store    (jint     store_value, volatile jint*     dest);
  75   // See comment above about using jlong atomics on 32-bit platforms
  76   inline static void store    (jlong    store_value, volatile jlong*    dest);
  77   inline static void store_ptr(intptr_t store_value, volatile intptr_t* dest);
  78   inline static void store_ptr(void*    store_value, volatile void*     dest);
  79 
  80   // See comment above about using jlong atomics on 32-bit platforms
  81   inline static jlong load(const volatile jlong* src);
  82 
  83   // Atomically add to a location. Returns updated value. add*() provide:
  84   // <fence> add-value-to-dest <membar StoreLoad|StoreStore>
  85   inline static jshort   add    (jshort   add_value, volatile jshort*   dest);
  86   inline static jint     add    (jint     add_value, volatile jint*     dest);
  87   inline static size_t   add    (size_t   add_value, volatile size_t*   dest);
  88   inline static intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest);
  89   inline static void*    add_ptr(intptr_t add_value, volatile void*     dest);






  90 
  91   // Atomically increment location. inc*() provide:
  92   // <fence> increment-dest <membar StoreLoad|StoreStore>
  93   inline static void inc    (volatile jint*     dest);
  94   inline static void inc    (volatile jshort*   dest);
  95   inline static void inc    (volatile size_t*   dest);
  96   inline static void inc_ptr(volatile intptr_t* dest);
  97   inline static void inc_ptr(volatile void*     dest);
  98 
  99   // Atomically decrement a location. dec*() provide:
 100   // <fence> decrement-dest <membar StoreLoad|StoreStore>
 101   inline static void dec    (volatile jint*     dest);
 102   inline static void dec    (volatile jshort*   dest);
 103   inline static void dec    (volatile size_t*   dest);
 104   inline static void dec_ptr(volatile intptr_t* dest);
 105   inline static void dec_ptr(volatile void*     dest);
 106 
 107   // Performs atomic exchange of *dest with exchange_value. Returns old
 108   // prior value of *dest. xchg*() provide:
 109   // <fence> exchange-value-with-dest <membar StoreLoad|StoreStore>


 139     return cmpxchg(exchange_value, dest, compare_value, order);
 140   }
 141 
 142   inline static void* cmpxchg_ptr(void* exchange_value,
 143                                   volatile void* dest,
 144                                   void* compare_value,
 145                                   cmpxchg_memory_order order = memory_order_conservative) {
 146     return cmpxchg(exchange_value,
 147                    reinterpret_cast<void* volatile*>(dest),
 148                    compare_value,
 149                    order);
 150   }
 151 
 152 private:
 153   // Test whether From is implicitly convertible to To.
 154   // From and To must be pointer types.
 155   // Note: Provides the limited subset of C++11 std::is_convertible
 156   // that is needed here.
 157   template<typename From, typename To> struct IsPointerConvertible;
 158 





































































 159   // Dispatch handler for cmpxchg.  Provides type-based validity
 160   // checking and limited conversions around calls to the
 161   // platform-specific implementation layer provided by
 162   // PlatformCmpxchg.
 163   template<typename T, typename D, typename U, typename Enable = void>
 164   struct CmpxchgImpl;
 165 
 166   // Platform-specific implementation of cmpxchg.  Support for sizes
 167   // of 1, 4, and 8 are required.  The class is a function object that
 168   // must be default constructable, with these requirements:
 169   //
 170   // - dest is of type T*.
 171   // - exchange_value and compare_value are of type T.
 172   // - order is of type cmpxchg_memory_order.
 173   // - platform_cmpxchg is an object of type PlatformCmpxchg<sizeof(T)>.
 174   //
 175   // Then
 176   //   platform_cmpxchg(exchange_value, dest, compare_value, order)
 177   // must be a valid expression, returning a result convertible to T.
 178   //


 202   // this class.
 203 public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
 204   struct CmpxchgByteUsingInt;
 205 private:
 206 };
 207 
 208 template<typename From, typename To>
 209 struct Atomic::IsPointerConvertible<From*, To*> : AllStatic {
 210   // Determine whether From* is implicitly convertible to To*, using
 211   // the "sizeof trick".
 212   typedef char yes;
 213   typedef char (&no)[2];
 214 
 215   static yes test(To*);
 216   static no test(...);
 217   static From* test_value;
 218 
 219   static const bool value = (sizeof(yes) == sizeof(test(test_value)));
 220 };
 221 






























 222 // Define the class before including platform file, which may specialize
 223 // the operator definition.  No generic definition of specializations
 224 // of the operator template are provided, nor are there any generic
 225 // specializations of the class.  The platform file is responsible for
 226 // providing those.
 227 template<size_t byte_size>
 228 struct Atomic::PlatformCmpxchg VALUE_OBJ_CLASS_SPEC {
 229   template<typename T>
 230   T operator()(T exchange_value,
 231                T volatile* dest,
 232                T compare_value,
 233                cmpxchg_memory_order order) const;
 234 };
 235 
 236 // Define the class before including platform file, which may use this
 237 // as a base class, requiring it be complete.  The definition is later
 238 // in this file, near the other definitions related to cmpxchg.
 239 struct Atomic::CmpxchgByteUsingInt VALUE_OBJ_CLASS_SPEC {
 240   template<typename T>
 241   T operator()(T exchange_value,
 242                T volatile* dest,
 243                T compare_value,
 244                cmpxchg_memory_order order) const;
 245 };
 246 
 247 // platform specific in-line definitions - must come before shared definitions
 248 
 249 #include OS_CPU_HEADER(atomic)
 250 
 251 // shared in-line definitions
 252 
 253 // size_t casts...
 254 #if (SIZE_MAX != UINTPTR_MAX)
 255 #error size_t is not WORD_SIZE, interesting platform, but missing implementation here
 256 #endif
 257 
 258 inline size_t Atomic::add(size_t add_value, volatile size_t* dest) {
 259   return (size_t) add_ptr((intptr_t) add_value, (volatile intptr_t*) dest);










































































 260 }
 261 
 262 inline void Atomic::inc(volatile size_t* dest) {
 263   inc_ptr((volatile intptr_t*) dest);
 264 }
 265 
 266 inline void Atomic::dec(volatile size_t* dest) {
 267   dec_ptr((volatile intptr_t*) dest);
 268 }
 269 
 270 template<typename T, typename D, typename U>
 271 inline D Atomic::cmpxchg(T exchange_value,
 272                          D volatile* dest,
 273                          U compare_value,
 274                          cmpxchg_memory_order order) {
 275   return CmpxchgImpl<T, D, U>()(exchange_value, dest, compare_value, order);
 276 }
 277 
 278 template<typename T, typename D>
 279 inline bool Atomic::replace_if_null(T* value, D* volatile* dest,


 396     // ... except for the one jbyte we want to update
 397     reinterpret_cast<uint8_t*>(&new_value)[offset] = canon_exchange_value;
 398 
 399     uint32_t res = cmpxchg(new_value, aligned_dest, cur, order);
 400     if (res == cur) break;      // success
 401 
 402     // at least one byte in the int changed value, so update
 403     // our view of the current int
 404     cur = res;
 405     // if our byte is still as cur we loop and try again
 406   } while (cur_as_bytes[offset] == canon_compare_value);
 407 
 408   return PrimitiveConversions::cast<T>(cur_as_bytes[offset]);
 409 }
 410 
 411 inline unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {
 412   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
 413   return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);
 414 }
 415 
 416 inline jshort Atomic::add(jshort add_value, volatile jshort* dest) {
 417   // Most platforms do not support atomic add on a 2-byte value. However,
 418   // if the value occupies the most significant 16 bits of an aligned 32-bit
 419   // word, then we can do this with an atomic add of (add_value << 16)
 420   // to the 32-bit word.
 421   //
 422   // The least significant parts of this 32-bit word will never be affected, even
 423   // in case of overflow/underflow.
 424   //
 425   // Use the ATOMIC_SHORT_PAIR macro (see macros.hpp) to get the desired alignment.
 426 #ifdef VM_LITTLE_ENDIAN
 427   assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
 428   jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest-1));
 429 #else
 430   assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
 431   jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest));
 432 #endif
 433   return (jshort)(new_value >> 16); // preserves sign
 434 }
 435 
 436 inline void Atomic::inc(volatile jshort* dest) {
 437   (void)add(1, dest);
 438 }
 439 
 440 inline void Atomic::dec(volatile jshort* dest) {
 441   (void)add(-1, dest);
 442 }
 443 
 444 #endif // SHARE_VM_RUNTIME_ATOMIC_HPP


   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/conditional.hpp"
  30 #include "metaprogramming/enableIf.hpp"
  31 #include "metaprogramming/isIntegral.hpp"
  32 #include "metaprogramming/isPointer.hpp"
  33 #include "metaprogramming/isSame.hpp"
  34 #include "metaprogramming/primitiveConversions.hpp"
  35 #include "metaprogramming/removeCV.hpp"
  36 #include "metaprogramming/removePointer.hpp"
  37 #include "utilities/align.hpp"
  38 #include "utilities/macros.hpp"
  39 
  40 enum cmpxchg_memory_order {
  41   memory_order_relaxed,
  42   // Use value which doesn't interfere with C++2011. We need to be more conservative.
  43   memory_order_conservative = 8
  44 };
  45 
  46 class Atomic : AllStatic {
  47  public:
  48   // Atomic operations on jlong types are not available on all 32-bit
  49   // platforms. If atomic ops on jlongs are defined here they must only
  50   // be used from code that verifies they are available at runtime and
  51   // can provide an alternative action if not - see supports_cx8() for
  52   // a means to test availability.
  53 
  54   // The memory operations that are mentioned with each of the atomic
  55   // function families come from src/share/vm/runtime/orderAccess.hpp,
  56   // e.g., <fence> is described in that file and is implemented by the


  68   inline static void store    (jshort   store_value, jshort*   dest);
  69   inline static void store    (jint     store_value, jint*     dest);
  70   // See comment above about using jlong atomics on 32-bit platforms
  71   inline static void store    (jlong    store_value, jlong*    dest);
  72   inline static void store_ptr(intptr_t store_value, intptr_t* dest);
  73   inline static void store_ptr(void*    store_value, void*     dest);
  74 
  75   inline static void store    (jbyte    store_value, volatile jbyte*    dest);
  76   inline static void store    (jshort   store_value, volatile jshort*   dest);
  77   inline static void store    (jint     store_value, volatile jint*     dest);
  78   // See comment above about using jlong atomics on 32-bit platforms
  79   inline static void store    (jlong    store_value, volatile jlong*    dest);
  80   inline static void store_ptr(intptr_t store_value, volatile intptr_t* dest);
  81   inline static void store_ptr(void*    store_value, volatile void*     dest);
  82 
  83   // See comment above about using jlong atomics on 32-bit platforms
  84   inline static jlong load(const volatile jlong* src);
  85 
  86   // Atomically add to a location. Returns updated value. add*() provide:
  87   // <fence> add-value-to-dest <membar StoreLoad|StoreStore>
  88 
  89   template<typename I, typename D>
  90   inline static D add(I add_value, D volatile* dest);
  91 
  92   inline static intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  93     return add(add_value, dest);
  94   }
  95 
  96   inline static void* add_ptr(intptr_t add_value, volatile void* dest) {
  97     return add(add_value, reinterpret_cast<char* volatile*>(dest));
  98   }
  99 
 100   // Atomically increment location. inc*() provide:
 101   // <fence> increment-dest <membar StoreLoad|StoreStore>
 102   inline static void inc    (volatile jint*     dest);
 103   inline static void inc    (volatile jshort*   dest);
 104   inline static void inc    (volatile size_t*   dest);
 105   inline static void inc_ptr(volatile intptr_t* dest);
 106   inline static void inc_ptr(volatile void*     dest);
 107 
 108   // Atomically decrement a location. dec*() provide:
 109   // <fence> decrement-dest <membar StoreLoad|StoreStore>
 110   inline static void dec    (volatile jint*     dest);
 111   inline static void dec    (volatile jshort*   dest);
 112   inline static void dec    (volatile size_t*   dest);
 113   inline static void dec_ptr(volatile intptr_t* dest);
 114   inline static void dec_ptr(volatile void*     dest);
 115 
 116   // Performs atomic exchange of *dest with exchange_value. Returns old
 117   // prior value of *dest. xchg*() provide:
 118   // <fence> exchange-value-with-dest <membar StoreLoad|StoreStore>


 148     return cmpxchg(exchange_value, dest, compare_value, order);
 149   }
 150 
 151   inline static void* cmpxchg_ptr(void* exchange_value,
 152                                   volatile void* dest,
 153                                   void* compare_value,
 154                                   cmpxchg_memory_order order = memory_order_conservative) {
 155     return cmpxchg(exchange_value,
 156                    reinterpret_cast<void* volatile*>(dest),
 157                    compare_value,
 158                    order);
 159   }
 160 
 161 private:
 162   // Test whether From is implicitly convertible to To.
 163   // From and To must be pointer types.
 164   // Note: Provides the limited subset of C++11 std::is_convertible
 165   // that is needed here.
 166   template<typename From, typename To> struct IsPointerConvertible;
 167 
 168   // Dispatch handler for add.  Provides type-based validity checking
 169   // and limited conversions around calls to the platform-specific
 170   // implementation layer provided by PlatformAdd.
 171   template<typename I, typename D, typename Enable = void>
 172   struct AddImpl;
 173 
 174   // Platform-specific implementation of add.  Support for sizes of 4
 175   // bytes and (if different) pointer size bytes are required.  The
 176   // class is a function object that must be default constructable,
 177   // with these requirements:
 178   //
 179   // - dest is of type D*, an integral or pointer type.
 180   // - add_value is of type I, an integral type.
 181   // - sizeof(I) == sizeof(D).
 182   // - if D is an integral type, I == D.
 183   // - platform_add is an object of type PlatformAdd<sizeof(D)>.
 184   //
 185   // Then
 186   //   platform_add(add_value, dest)
 187   // must be a valid expression, returning a result convertible to D.
 188   //
 189   // No definition is provided; all platforms must explicitly define
 190   // this class and any needed specializations.
 191   template<size_t byte_size> struct PlatformAdd;
 192 
 193   // Helper base classes for defining PlatformAdd.  To use, define
 194   // PlatformAdd or a specialization that derives from one of these,
 195   // and include in the PlatformAdd definition the support function
 196   // (described below) required by the base class.
 197   //
 198   // These classes implement the required function object protocol for
 199   // PlatformAdd, using a support function template provided by the
 200   // derived class.  Let add_value (of type I) and dest (of type D) be
 201   // the arguments the object is called with.  If D is a pointer type
 202   // P*, then let addend (of type I) be add_value * sizeof(P);
 203   // otherwise, addend is add_value.
 204   //
 205   // FetchAndAdd requires the derived class to provide
 206   //   fetch_and_add(addend, dest)
 207   // atomically adding addend to the value of dest, and returning the
 208   // old value.
 209   //
 210   // AddAndFetch requires the derived class to provide
 211   //   add_and_fetch(addend, dest)
 212   // atomically adding addend to the value of dest, and returning the
 213   // new value.
 214   //
 215   // When D is a pointer type P*, both fetch_and_add and add_and_fetch
 216   // treat it as if it were a uintptr_t; they do not perform any
 217   // scaling of the addend, as that has already been done by the
 218   // caller.
 219 public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
 220   template<typename Derived> struct FetchAndAdd;
 221   template<typename Derived> struct AddAndFetch;
 222   struct AddShortUsingInt;
 223 private:
 224 
 225   // Support for platforms that implement some variants of add using a
 226   // (typically out of line) non-template helper function.  The
 227   // generic arguments passed to PlatformAdd need to be translated to
 228   // the appropriate type for the helper function, the helper function
 229   // invoked on the translated arguments, and the result translated
 230   // back.  Type is the parameter / return type of the helper
 231   // function.  No scaling of add_value is performed when D is a pointer
 232   // type, so this function can be used to implement the support function
 233   // required by AddAndFetch.
 234   template<typename Type, typename Fn, typename I, typename D>
 235   static D add_using_helper(Fn fn, I add_value, D volatile* dest);
 236 
 237   // Dispatch handler for cmpxchg.  Provides type-based validity
 238   // checking and limited conversions around calls to the
 239   // platform-specific implementation layer provided by
 240   // PlatformCmpxchg.
 241   template<typename T, typename D, typename U, typename Enable = void>
 242   struct CmpxchgImpl;
 243 
 244   // Platform-specific implementation of cmpxchg.  Support for sizes
 245   // of 1, 4, and 8 are required.  The class is a function object that
 246   // must be default constructable, with these requirements:
 247   //
 248   // - dest is of type T*.
 249   // - exchange_value and compare_value are of type T.
 250   // - order is of type cmpxchg_memory_order.
 251   // - platform_cmpxchg is an object of type PlatformCmpxchg<sizeof(T)>.
 252   //
 253   // Then
 254   //   platform_cmpxchg(exchange_value, dest, compare_value, order)
 255   // must be a valid expression, returning a result convertible to T.
 256   //


 280   // this class.
 281 public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
 282   struct CmpxchgByteUsingInt;
 283 private:
 284 };
 285 
 286 template<typename From, typename To>
 287 struct Atomic::IsPointerConvertible<From*, To*> : AllStatic {
 288   // Determine whether From* is implicitly convertible to To*, using
 289   // the "sizeof trick".
 290   typedef char yes;
 291   typedef char (&no)[2];
 292 
 293   static yes test(To*);
 294   static no test(...);
 295   static From* test_value;
 296 
 297   static const bool value = (sizeof(yes) == sizeof(test(test_value)));
 298 };
 299 
 300 // Define FetchAndAdd and AddAndFetch helper classes before including
 301 // platform file, which may use these as base classes, requiring they
 302 // be complete.
 303 
 304 template<typename Derived>
 305 struct Atomic::FetchAndAdd VALUE_OBJ_CLASS_SPEC {
 306   template<typename I, typename D>
 307   D operator()(I add_value, D volatile* dest) const;
 308 };
 309 
 310 template<typename Derived>
 311 struct Atomic::AddAndFetch VALUE_OBJ_CLASS_SPEC {
 312   template<typename I, typename D>
 313   D operator()(I add_value, D volatile* dest) const;
 314 };
 315 
 316 // Most platforms do not support atomic add on a 2-byte value. However,
 317 // if the value occupies the most significant 16 bits of an aligned 32-bit
 318 // word, then we can do this with an atomic add of (add_value << 16)
 319 // to the 32-bit word.
 320 //
 321 // The least significant parts of this 32-bit word will never be affected, even
 322 // in case of overflow/underflow.
 323 //
 324 // Use the ATOMIC_SHORT_PAIR macro (see macros.hpp) to get the desired alignment.
 325 struct Atomic::AddShortUsingInt VALUE_OBJ_CLASS_SPEC {
 326   template<typename T>
 327   T operator()(T add_value, T volatile* dest) const;
 328 };
 329 
 330 // Define the class before including platform file, which may specialize
 331 // the operator definition.  No generic definition of specializations
 332 // of the operator template are provided, nor are there any generic
 333 // specializations of the class.  The platform file is responsible for
 334 // providing those.
 335 template<size_t byte_size>
 336 struct Atomic::PlatformCmpxchg VALUE_OBJ_CLASS_SPEC {
 337   template<typename T>
 338   T operator()(T exchange_value,
 339                T volatile* dest,
 340                T compare_value,
 341                cmpxchg_memory_order order) const;
 342 };
 343 
 344 // Define the class before including platform file, which may use this
 345 // as a base class, requiring it be complete.  The definition is later
 346 // in this file, near the other definitions related to cmpxchg.
 347 struct Atomic::CmpxchgByteUsingInt VALUE_OBJ_CLASS_SPEC {
 348   template<typename T>
 349   T operator()(T exchange_value,
 350                T volatile* dest,
 351                T compare_value,
 352                cmpxchg_memory_order order) const;
 353 };
 354 
 355 // platform specific in-line definitions - must come before shared definitions
 356 
 357 #include OS_CPU_HEADER(atomic)
 358 
 359 // shared in-line definitions
 360 
 361 // size_t casts...
 362 #if (SIZE_MAX != UINTPTR_MAX)
 363 #error size_t is not WORD_SIZE, interesting platform, but missing implementation here
 364 #endif
 365 
 366 template<typename I, typename D>
 367 inline D Atomic::add(I add_value, D volatile* dest) {
 368   return AddImpl<I, D>()(add_value, dest);
 369 }
 370 
 371 template<typename I, typename D>
 372 struct Atomic::AddImpl<
 373   I, D,
 374   typename EnableIf<IsIntegral<I>::value &&
 375                     IsIntegral<D>::value &&
 376                     (sizeof(I) <= sizeof(D)) &&
 377                     (IsSigned<I>::value == IsSigned<D>::value)>::type>
 378   VALUE_OBJ_CLASS_SPEC
 379 {
 380   D operator()(I add_value, D volatile* dest) const {
 381     D addend = add_value;
 382     return PlatformAdd<sizeof(D)>()(addend, dest);
 383   }
 384 };
 385 
 386 template<typename I, typename P>
 387 struct Atomic::AddImpl<
 388   I, P*,
 389   typename EnableIf<IsIntegral<I>::value && (sizeof(I) <= sizeof(P*))>::type>
 390   VALUE_OBJ_CLASS_SPEC
 391 {
 392   P* operator()(I add_value, P* volatile* dest) const {
 393     STATIC_ASSERT(sizeof(intptr_t) == sizeof(P*));
 394     STATIC_ASSERT(sizeof(uintptr_t) == sizeof(P*));
 395     typedef typename Conditional<IsSigned<I>::value,
 396                                  intptr_t,
 397                                  uintptr_t>::type CI;
 398     CI addend = add_value;
 399     return PlatformAdd<sizeof(P*)>()(addend, dest);
 400   }
 401 };
 402 
 403 template<typename T>
 404 T Atomic::AddShortUsingInt::operator()(T add_value, T volatile* dest) const {
 405 #ifdef VM_LITTLE_ENDIAN
 406   assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
 407   uint32_t new_value = Atomic::add(uint32_t(add_value) << 16, (volatile uint32_t*)(dest-1));
 408 #else
 409   assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
 410   uint32_t new_value = Atomic::add(uint32_t(add_value) << 16, (volatile uint32_t*)(dest));
 411 #endif
 412   return T(new_value >> 16); // preserves sign
 413 };
 414 
 415 template<typename Derived>
 416 template<typename I, typename D>
 417 inline D Atomic::FetchAndAdd<Derived>::operator()(I add_value, D volatile* dest) const {
 418   I addend = add_value;
 419   // If D is a pointer type P*, scale by sizeof(P).
 420   if (IsPointer<D>::value) {
 421     addend *= sizeof(typename RemovePointer<D>::type);
 422   }
 423   D old = static_cast<const Derived*>(this)->fetch_and_add(addend, dest);
 424   return old + add_value;
 425 }
 426 
 427 template<typename Derived>
 428 template<typename I, typename D>
 429 inline D Atomic::AddAndFetch<Derived>::operator()(I add_value, D volatile* dest) const {
 430   // If D is a pointer type P*, scale by sizeof(P).
 431   if (IsPointer<D>::value) {
 432     add_value *= sizeof(typename RemovePointer<D>::type);
 433   }
 434   return static_cast<const Derived*>(this)->add_and_fetch(add_value, dest);
 435 }
 436 
 437 template<typename Type, typename Fn, typename I, typename D>
 438 inline D Atomic::add_using_helper(Fn fn, I add_value, D volatile* dest) {
 439   return PrimitiveConversions::cast<D>(
 440     fn(PrimitiveConversions::cast<Type>(add_value),
 441        reinterpret_cast<Type volatile*>(dest)));
 442 }
 443 
 444 inline void Atomic::inc(volatile size_t* dest) {
 445   inc_ptr((volatile intptr_t*) dest);
 446 }
 447 
 448 inline void Atomic::dec(volatile size_t* dest) {
 449   dec_ptr((volatile intptr_t*) dest);
 450 }
 451 
 452 template<typename T, typename D, typename U>
 453 inline D Atomic::cmpxchg(T exchange_value,
 454                          D volatile* dest,
 455                          U compare_value,
 456                          cmpxchg_memory_order order) {
 457   return CmpxchgImpl<T, D, U>()(exchange_value, dest, compare_value, order);
 458 }
 459 
 460 template<typename T, typename D>
 461 inline bool Atomic::replace_if_null(T* value, D* volatile* dest,


 578     // ... except for the one jbyte we want to update
 579     reinterpret_cast<uint8_t*>(&new_value)[offset] = canon_exchange_value;
 580 
 581     uint32_t res = cmpxchg(new_value, aligned_dest, cur, order);
 582     if (res == cur) break;      // success
 583 
 584     // at least one byte in the int changed value, so update
 585     // our view of the current int
 586     cur = res;
 587     // if our byte is still as cur we loop and try again
 588   } while (cur_as_bytes[offset] == canon_compare_value);
 589 
 590   return PrimitiveConversions::cast<T>(cur_as_bytes[offset]);
 591 }
 592 
 593 inline unsigned Atomic::xchg(unsigned int exchange_value, volatile unsigned int* dest) {
 594   assert(sizeof(unsigned int) == sizeof(jint), "more work to do");
 595   return (unsigned int)Atomic::xchg((jint)exchange_value, (volatile jint*)dest);
 596 }
 597 




















 598 inline void Atomic::inc(volatile jshort* dest) {
 599   (void)add(jshort(1), dest);
 600 }
 601 
 602 inline void Atomic::dec(volatile jshort* dest) {
 603   (void)add(jshort(-1), dest);
 604 }
 605 
 606 #endif // SHARE_VM_RUNTIME_ATOMIC_HPP
< prev index next >