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/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
  57   // OrderAccess::fence() function. See that file for the gory details
  58   // on the Memory Access Ordering Model.
  59 
  60   // All of the atomic operations that imply a read-modify-write action
  61   // guarantee a two-way memory barrier across that operation. Historically
  62   // these semantics reflect the strength of atomic operations that are
  63   // provided on SPARC/X86. We assume that strength is necessary unless
  64   // we can prove that a weaker form is sufficiently safe.
  65 
  66   // Atomically store to a location
  67   // The type T must be either a pointer type convertible to or equal
  68   // to D, an integral/enum type equal to D, or a type equal to D that
  69   // is primitive convertible using PrimitiveConversions.
  70   template<typename T, typename D>
  71   inline static void store(T store_value, volatile D* dest);
  72 
  73   // Atomically load from a location
  74   // The type T must be either a pointer type, an integral/enum type,
  75   // or a type that is primitive convertible using PrimitiveConversions.
  76   template<typename T>
  77   inline static T load(const volatile T* dest);
  78 
  79   // Atomically add to a location. Returns updated value. add*() provide:
  80   // <fence> add-value-to-dest <membar StoreLoad|StoreStore>
  81 
  82   template<typename I, typename D>
  83   inline static D add(I add_value, D volatile* dest);
  84 
  85   template<typename I, typename D>
  86   inline static D sub(I sub_value, D volatile* dest);
  87 
  88   // Atomically increment location. inc() provide:
  89   // <fence> increment-dest <membar StoreLoad|StoreStore>
  90   // The type D may be either a pointer type, or an integral
  91   // type. If it is a pointer type, then the increment is
  92   // scaled to the size of the type pointed to by the pointer.
  93   template<typename D>
  94   inline static void inc(D volatile* dest);
  95 
  96   // Atomically decrement a location. dec() provide:
  97   // <fence> decrement-dest <membar StoreLoad|StoreStore>
  98   // The type D may be either a pointer type, or an integral
  99   // type. If it is a pointer type, then the decrement is
 100   // scaled to the size of the type pointed to by the pointer.
 101   template<typename D>
 102   inline static void dec(D volatile* dest);
 103 
 104   // Performs atomic exchange of *dest with exchange_value. Returns old
 105   // prior value of *dest. xchg*() provide:
 106   // <fence> exchange-value-with-dest <membar StoreLoad|StoreStore>
 107   // The type T must be either a pointer type convertible to or equal
 108   // to D, an integral/enum type equal to D, or a type equal to D that
 109   // is primitive convertible using PrimitiveConversions.
 110   template<typename T, typename D>
 111   inline static D xchg(T exchange_value, volatile D* dest);
 112 
 113   // Performs atomic compare of *dest and compare_value, and exchanges
 114   // *dest with exchange_value if the comparison succeeded. Returns prior
 115   // value of *dest. cmpxchg*() provide:
 116   // <fence> compare-and-exchange <membar StoreLoad|StoreStore>
 117 
 118   template<typename T, typename D, typename U>
 119   inline static D cmpxchg(T exchange_value,
 120                           D volatile* dest,
 121                           U compare_value,
 122                           cmpxchg_memory_order order = memory_order_conservative);
 123 
 124   // Performs atomic compare of *dest and NULL, and replaces *dest
 125   // with exchange_value if the comparison succeeded.  Returns true if
 126   // the comparison succeeded and the exchange occurred.  This is
 127   // often used as part of lazy initialization, as a lock-free
 128   // alternative to the Double-Checked Locking Pattern.
 129   template<typename T, typename D>
 130   inline static bool replace_if_null(T* value, D* volatile* dest,
 131                                      cmpxchg_memory_order order = memory_order_conservative);
 132 
 133 private:
 134   // Test whether From is implicitly convertible to To.
 135   // From and To must be pointer types.
 136   // Note: Provides the limited subset of C++11 std::is_convertible
 137   // that is needed here.
 138   template<typename From, typename To> struct IsPointerConvertible;
 139 
 140 protected:
 141   // Dispatch handler for store.  Provides type-based validity
 142   // checking and limited conversions around calls to the platform-
 143   // specific implementation layer provided by PlatformOp.
 144   template<typename T, typename D, typename PlatformOp, typename Enable = void>
 145   struct StoreImpl;
 146 
 147   // Platform-specific implementation of store.  Support for sizes
 148   // of 1, 2, 4, and (if different) pointer size bytes are required.
 149   // The class is a function object that must be default constructable,
 150   // with these requirements:
 151   //
 152   // either:
 153   // - dest is of type D*, an integral, enum or pointer type.
 154   // - new_value are of type T, an integral, enum or pointer type D or
 155   //   pointer type convertible to D.
 156   // or:
 157   // - T and D are the same and are primitive convertible using PrimitiveConversions
 158   // and either way:
 159   // - platform_store is an object of type PlatformStore<sizeof(T)>.
 160   //
 161   // Then
 162   //   platform_store(new_value, dest)
 163   // must be a valid expression.
 164   //
 165   // The default implementation is a volatile store. If a platform
 166   // requires more for e.g. 64 bit stores, a specialization is required
 167   template<size_t byte_size> struct PlatformStore;
 168 
 169   // Dispatch handler for load.  Provides type-based validity
 170   // checking and limited conversions around calls to the platform-
 171   // specific implementation layer provided by PlatformOp.
 172   template<typename T, typename PlatformOp, typename Enable = void>
 173   struct LoadImpl;
 174 
 175   // Platform-specific implementation of load. Support for sizes of
 176   // 1, 2, 4 bytes and (if different) pointer size bytes are required.
 177   // The class is a function object that must be default
 178   // constructable, with these requirements:
 179   //
 180   // - dest is of type T*, an integral, enum or pointer type, or
 181   //   T is convertible to a primitive type using PrimitiveConversions
 182   // - platform_load is an object of type PlatformLoad<sizeof(T)>.
 183   //
 184   // Then
 185   //   platform_load(src)
 186   // must be a valid expression, returning a result convertible to T.
 187   //
 188   // The default implementation is a volatile load. If a platform
 189   // requires more for e.g. 64 bit loads, a specialization is required
 190   template<size_t byte_size> struct PlatformLoad;
 191 
 192 private:
 193   // Dispatch handler for add.  Provides type-based validity checking
 194   // and limited conversions around calls to the platform-specific
 195   // implementation layer provided by PlatformAdd.
 196   template<typename I, typename D, typename Enable = void>
 197   struct AddImpl;
 198 
 199   // Platform-specific implementation of add.  Support for sizes of 4
 200   // bytes and (if different) pointer size bytes are required.  The
 201   // class is a function object that must be default constructable,
 202   // with these requirements:
 203   //
 204   // - dest is of type D*, an integral or pointer type.
 205   // - add_value is of type I, an integral type.
 206   // - sizeof(I) == sizeof(D).
 207   // - if D is an integral type, I == D.
 208   // - platform_add is an object of type PlatformAdd<sizeof(D)>.
 209   //
 210   // Then
 211   //   platform_add(add_value, dest)
 212   // must be a valid expression, returning a result convertible to D.
 213   //
 214   // No definition is provided; all platforms must explicitly define
 215   // this class and any needed specializations.
 216   template<size_t byte_size> struct PlatformAdd;
 217 
 218   // Helper base classes for defining PlatformAdd.  To use, define
 219   // PlatformAdd or a specialization that derives from one of these,
 220   // and include in the PlatformAdd definition the support function
 221   // (described below) required by the base class.
 222   //
 223   // These classes implement the required function object protocol for
 224   // PlatformAdd, using a support function template provided by the
 225   // derived class.  Let add_value (of type I) and dest (of type D) be
 226   // the arguments the object is called with.  If D is a pointer type
 227   // P*, then let addend (of type I) be add_value * sizeof(P);
 228   // otherwise, addend is add_value.
 229   //
 230   // FetchAndAdd requires the derived class to provide
 231   //   fetch_and_add(addend, dest)
 232   // atomically adding addend to the value of dest, and returning the
 233   // old value.
 234   //
 235   // AddAndFetch requires the derived class to provide
 236   //   add_and_fetch(addend, dest)
 237   // atomically adding addend to the value of dest, and returning the
 238   // new value.
 239   //
 240   // When D is a pointer type P*, both fetch_and_add and add_and_fetch
 241   // treat it as if it were a uintptr_t; they do not perform any
 242   // scaling of the addend, as that has already been done by the
 243   // caller.
 244 public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
 245   template<typename Derived> struct FetchAndAdd;
 246   template<typename Derived> struct AddAndFetch;
 247 private:
 248 
 249   // Support for platforms that implement some variants of add using a
 250   // (typically out of line) non-template helper function.  The
 251   // generic arguments passed to PlatformAdd need to be translated to
 252   // the appropriate type for the helper function, the helper function
 253   // invoked on the translated arguments, and the result translated
 254   // back.  Type is the parameter / return type of the helper
 255   // function.  No scaling of add_value is performed when D is a pointer
 256   // type, so this function can be used to implement the support function
 257   // required by AddAndFetch.
 258   template<typename Type, typename Fn, typename I, typename D>
 259   static D add_using_helper(Fn fn, I add_value, D volatile* dest);
 260 
 261   // Dispatch handler for cmpxchg.  Provides type-based validity
 262   // checking and limited conversions around calls to the
 263   // platform-specific implementation layer provided by
 264   // PlatformCmpxchg.
 265   template<typename T, typename D, typename U, typename Enable = void>
 266   struct CmpxchgImpl;
 267 
 268   // Platform-specific implementation of cmpxchg.  Support for sizes
 269   // of 1, 4, and 8 are required.  The class is a function object that
 270   // must be default constructable, with these requirements:
 271   //
 272   // - dest is of type T*.
 273   // - exchange_value and compare_value are of type T.
 274   // - order is of type cmpxchg_memory_order.
 275   // - platform_cmpxchg is an object of type PlatformCmpxchg<sizeof(T)>.
 276   //
 277   // Then
 278   //   platform_cmpxchg(exchange_value, dest, compare_value, order)
 279   // must be a valid expression, returning a result convertible to T.
 280   //
 281   // A default definition is provided, which declares a function template
 282   //   T operator()(T, T volatile*, T, cmpxchg_memory_order) const
 283   //
 284   // For each required size, a platform must either provide an
 285   // appropriate definition of that function, or must entirely
 286   // specialize the class template for that size.
 287   template<size_t byte_size> struct PlatformCmpxchg;
 288 
 289   // Support for platforms that implement some variants of cmpxchg
 290   // using a (typically out of line) non-template helper function.
 291   // The generic arguments passed to PlatformCmpxchg need to be
 292   // translated to the appropriate type for the helper function, the
 293   // helper invoked on the translated arguments, and the result
 294   // translated back.  Type is the parameter / return type of the
 295   // helper function.
 296   template<typename Type, typename Fn, typename T>
 297   static T cmpxchg_using_helper(Fn fn,
 298                                 T exchange_value,
 299                                 T volatile* dest,
 300                                 T compare_value);
 301 
 302   // Support platforms that do not provide Read-Modify-Write
 303   // byte-level atomic access. To use, derive PlatformCmpxchg<1> from
 304   // this class.
 305 public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
 306   struct CmpxchgByteUsingInt;
 307 private:
 308 
 309   // Dispatch handler for xchg.  Provides type-based validity
 310   // checking and limited conversions around calls to the
 311   // platform-specific implementation layer provided by
 312   // PlatformXchg.
 313   template<typename T, typename D, typename Enable = void>
 314   struct XchgImpl;
 315 
 316   // Platform-specific implementation of xchg.  Support for sizes
 317   // of 4, and sizeof(intptr_t) are required.  The class is a function
 318   // object that must be default constructable, with these requirements:
 319   //
 320   // - dest is of type T*.
 321   // - exchange_value is of type T.
 322   // - platform_xchg is an object of type PlatformXchg<sizeof(T)>.
 323   //
 324   // Then
 325   //   platform_xchg(exchange_value, dest)
 326   // must be a valid expression, returning a result convertible to T.
 327   //
 328   // A default definition is provided, which declares a function template
 329   //   T operator()(T, T volatile*, T, cmpxchg_memory_order) const
 330   //
 331   // For each required size, a platform must either provide an
 332   // appropriate definition of that function, or must entirely
 333   // specialize the class template for that size.
 334   template<size_t byte_size> struct PlatformXchg;
 335 
 336   // Support for platforms that implement some variants of xchg
 337   // using a (typically out of line) non-template helper function.
 338   // The generic arguments passed to PlatformXchg need to be
 339   // translated to the appropriate type for the helper function, the
 340   // helper invoked on the translated arguments, and the result
 341   // translated back.  Type is the parameter / return type of the
 342   // helper function.
 343   template<typename Type, typename Fn, typename T>
 344   static T xchg_using_helper(Fn fn,
 345                              T exchange_value,
 346                              T volatile* dest);
 347 };
 348 
 349 template<typename From, typename To>
 350 struct Atomic::IsPointerConvertible<From*, To*> : AllStatic {
 351   // Determine whether From* is implicitly convertible to To*, using
 352   // the "sizeof trick".
 353   typedef char yes;
 354   typedef char (&no)[2];
 355 
 356   static yes test(To*);
 357   static no test(...);
 358   static From* test_value;
 359 
 360   static const bool value = (sizeof(yes) == sizeof(test(test_value)));
 361 };
 362 
 363 // Handle load for pointer, integral and enum types.
 364 template<typename T, typename PlatformOp>
 365 struct Atomic::LoadImpl<
 366   T,
 367   PlatformOp,
 368   typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value || IsPointer<T>::value>::type>
 369   VALUE_OBJ_CLASS_SPEC
 370 {
 371   T operator()(T const volatile* dest) const {
 372     // Forward to the platform handler for the size of T.
 373     return PlatformOp()(dest);
 374   }
 375 };
 376 
 377 // Handle load for types that have a translator.
 378 //
 379 // All the involved types must be identical.
 380 //
 381 // This translates the original call into a call on the decayed
 382 // arguments, and returns the recovered result of that translated
 383 // call.
 384 template<typename T, typename PlatformOp>
 385 struct Atomic::LoadImpl<
 386   T,
 387   PlatformOp,
 388   typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
 389   VALUE_OBJ_CLASS_SPEC
 390 {
 391   T operator()(T const volatile* dest) const {
 392     typedef PrimitiveConversions::Translate<T> Translator;
 393     typedef typename Translator::Decayed Decayed;
 394     STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
 395     Decayed result = PlatformOp()(reinterpret_cast<Decayed const volatile*>(dest));
 396     return Translator::recover(result);
 397   }
 398 };
 399 
 400 // Default implementation of atomic load if a specific platform
 401 // does not provide a specialization for a certain size class.
 402 // For increased safety, the default implementation only allows
 403 // load types that are pointer sized or smaller. If a platform still
 404 // supports wide atomics, then it has to use specialization
 405 // of Atomic::PlatformLoad for that wider size class.
 406 template<size_t byte_size>
 407 struct Atomic::PlatformLoad VALUE_OBJ_CLASS_SPEC {
 408   template<typename T>
 409   T operator()(T const volatile* dest) const {
 410     STATIC_ASSERT(sizeof(T) <= sizeof(void*)); // wide atomics need specialization
 411     return *dest;
 412   }
 413 };
 414 
 415 // Handle store for integral and enum types.
 416 //
 417 // All the involved types must be identical.
 418 template<typename T, typename PlatformOp>
 419 struct Atomic::StoreImpl<
 420   T, T,
 421   PlatformOp,
 422   typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
 423   VALUE_OBJ_CLASS_SPEC
 424 {
 425   void operator()(T new_value, T volatile* dest) const {
 426     // Forward to the platform handler for the size of T.
 427     PlatformOp()(new_value, dest);
 428   }
 429 };
 430 
 431 // Handle store for pointer types.
 432 //
 433 // The new_value must be implicitly convertible to the
 434 // destination's type; it must be type-correct to store the
 435 // new_value in the destination.
 436 template<typename T, typename D, typename PlatformOp>
 437 struct Atomic::StoreImpl<
 438   T*, D*,
 439   PlatformOp,
 440   typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value>::type>
 441   VALUE_OBJ_CLASS_SPEC
 442 {
 443   void operator()(T* new_value, D* volatile* dest) const {
 444     // Allow derived to base conversion, and adding cv-qualifiers.
 445     D* value = new_value;
 446     PlatformOp()(value, dest);
 447   }
 448 };
 449 
 450 // Handle store for types that have a translator.
 451 //
 452 // All the involved types must be identical.
 453 //
 454 // This translates the original call into a call on the decayed
 455 // arguments.
 456 template<typename T, typename PlatformOp>
 457 struct Atomic::StoreImpl<
 458   T, T,
 459   PlatformOp,
 460   typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
 461   VALUE_OBJ_CLASS_SPEC
 462 {
 463   void operator()(T new_value, T volatile* dest) const {
 464     typedef PrimitiveConversions::Translate<T> Translator;
 465     typedef typename Translator::Decayed Decayed;
 466     STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
 467     PlatformOp()(Translator::decay(new_value),
 468                  reinterpret_cast<Decayed volatile*>(dest));
 469   }
 470 };
 471 
 472 // Default implementation of atomic store if a specific platform
 473 // does not provide a specialization for a certain size class.
 474 // For increased safety, the default implementation only allows
 475 // storing types that are pointer sized or smaller. If a platform still
 476 // supports wide atomics, then it has to use specialization
 477 // of Atomic::PlatformStore for that wider size class.
 478 template<size_t byte_size>
 479 struct Atomic::PlatformStore VALUE_OBJ_CLASS_SPEC {
 480   template<typename T>
 481   void operator()(T new_value,
 482                   T volatile* dest) const {
 483     STATIC_ASSERT(sizeof(T) <= sizeof(void*)); // wide atomics need specialization
 484     (void)const_cast<T&>(*dest = new_value);
 485   }
 486 };
 487 
 488 // Define FetchAndAdd and AddAndFetch helper classes before including
 489 // platform file, which may use these as base classes, requiring they
 490 // be complete.
 491 
 492 template<typename Derived>
 493 struct Atomic::FetchAndAdd VALUE_OBJ_CLASS_SPEC {
 494   template<typename I, typename D>
 495   D operator()(I add_value, D volatile* dest) const;
 496 };
 497 
 498 template<typename Derived>
 499 struct Atomic::AddAndFetch VALUE_OBJ_CLASS_SPEC {
 500   template<typename I, typename D>
 501   D operator()(I add_value, D volatile* dest) const;
 502 };
 503 
 504 template<typename D>
 505 inline void Atomic::inc(D volatile* dest) {
 506   STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
 507   typedef typename Conditional<IsPointer<D>::value, ptrdiff_t, D>::type I;
 508   Atomic::add(I(1), dest);
 509 }
 510 
 511 template<typename D>
 512 inline void Atomic::dec(D volatile* dest) {
 513   STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
 514   typedef typename Conditional<IsPointer<D>::value, ptrdiff_t, D>::type I;
 515   // Assumes two's complement integer representation.
 516   #pragma warning(suppress: 4146)
 517   Atomic::add(I(-1), dest);
 518 }
 519 
 520 template<typename I, typename D>
 521 inline D Atomic::sub(I sub_value, D volatile* dest) {
 522   STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
 523   // Assumes two's complement integer representation.
 524   #pragma warning(suppress: 4146)
 525   return Atomic::add(-sub_value, dest);
 526 }
 527 
 528 // Define the class before including platform file, which may specialize
 529 // the operator definition.  No generic definition of specializations
 530 // of the operator template are provided, nor are there any generic
 531 // specializations of the class.  The platform file is responsible for
 532 // providing those.
 533 template<size_t byte_size>
 534 struct Atomic::PlatformCmpxchg VALUE_OBJ_CLASS_SPEC {
 535   template<typename T>
 536   T operator()(T exchange_value,
 537                T volatile* dest,
 538                T compare_value,
 539                cmpxchg_memory_order order) const;
 540 };
 541 
 542 // Define the class before including platform file, which may use this
 543 // as a base class, requiring it be complete.  The definition is later
 544 // in this file, near the other definitions related to cmpxchg.
 545 struct Atomic::CmpxchgByteUsingInt VALUE_OBJ_CLASS_SPEC {
 546   template<typename T>
 547   T operator()(T exchange_value,
 548                T volatile* dest,
 549                T compare_value,
 550                cmpxchg_memory_order order) const;
 551 };
 552 
 553 // Define the class before including platform file, which may specialize
 554 // the operator definition.  No generic definition of specializations
 555 // of the operator template are provided, nor are there any generic
 556 // specializations of the class.  The platform file is responsible for
 557 // providing those.
 558 template<size_t byte_size>
 559 struct Atomic::PlatformXchg VALUE_OBJ_CLASS_SPEC {
 560   template<typename T>
 561   T operator()(T exchange_value,
 562                T volatile* dest) const;
 563 };
 564 
 565 // platform specific in-line definitions - must come before shared definitions
 566 
 567 #include OS_CPU_HEADER(atomic)
 568 
 569 // shared in-line definitions
 570 
 571 // size_t casts...
 572 #if (SIZE_MAX != UINTPTR_MAX)
 573 #error size_t is not WORD_SIZE, interesting platform, but missing implementation here
 574 #endif
 575 
 576 template<typename T>
 577 inline T Atomic::load(const volatile T* dest) {
 578   return LoadImpl<T, PlatformLoad<sizeof(T)> >()(dest);
 579 }
 580 
 581 template<typename T, typename D>
 582 inline void Atomic::store(T store_value, volatile D* dest) {
 583   StoreImpl<T, D, PlatformStore<sizeof(D)> >()(store_value, dest);
 584 }
 585 
 586 template<typename I, typename D>
 587 inline D Atomic::add(I add_value, D volatile* dest) {
 588   return AddImpl<I, D>()(add_value, dest);
 589 }
 590 
 591 template<typename I, typename D>
 592 struct Atomic::AddImpl<
 593   I, D,
 594   typename EnableIf<IsIntegral<I>::value &&
 595                     IsIntegral<D>::value &&
 596                     (sizeof(I) <= sizeof(D)) &&
 597                     (IsSigned<I>::value == IsSigned<D>::value)>::type>
 598   VALUE_OBJ_CLASS_SPEC
 599 {
 600   D operator()(I add_value, D volatile* dest) const {
 601     D addend = add_value;
 602     return PlatformAdd<sizeof(D)>()(addend, dest);
 603   }
 604 };
 605 
 606 template<typename I, typename P>
 607 struct Atomic::AddImpl<
 608   I, P*,
 609   typename EnableIf<IsIntegral<I>::value && (sizeof(I) <= sizeof(P*))>::type>
 610   VALUE_OBJ_CLASS_SPEC
 611 {
 612   P* operator()(I add_value, P* volatile* dest) const {
 613     STATIC_ASSERT(sizeof(intptr_t) == sizeof(P*));
 614     STATIC_ASSERT(sizeof(uintptr_t) == sizeof(P*));
 615     typedef typename Conditional<IsSigned<I>::value,
 616                                  intptr_t,
 617                                  uintptr_t>::type CI;
 618     CI addend = add_value;
 619     return PlatformAdd<sizeof(P*)>()(addend, dest);
 620   }
 621 };
 622 
 623 // Most platforms do not support atomic add on a 2-byte value. However,
 624 // if the value occupies the most significant 16 bits of an aligned 32-bit
 625 // word, then we can do this with an atomic add of (add_value << 16)
 626 // to the 32-bit word.
 627 //
 628 // The least significant parts of this 32-bit word will never be affected, even
 629 // in case of overflow/underflow.
 630 //
 631 // Use the ATOMIC_SHORT_PAIR macro (see macros.hpp) to get the desired alignment.
 632 template<>
 633 struct Atomic::AddImpl<jshort, jshort> VALUE_OBJ_CLASS_SPEC {
 634   jshort operator()(jshort add_value, jshort volatile* dest) const {
 635 #ifdef VM_LITTLE_ENDIAN
 636     assert((intx(dest) & 0x03) == 0x02, "wrong alignment");
 637     jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest-1));
 638 #else
 639     assert((intx(dest) & 0x03) == 0x00, "wrong alignment");
 640     jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest));
 641 #endif
 642     return (jshort)(new_value >> 16); // preserves sign
 643   }
 644 };
 645 
 646 template<typename Derived>
 647 template<typename I, typename D>
 648 inline D Atomic::FetchAndAdd<Derived>::operator()(I add_value, D volatile* dest) const {
 649   I addend = add_value;
 650   // If D is a pointer type P*, scale by sizeof(P).
 651   if (IsPointer<D>::value) {
 652     addend *= sizeof(typename RemovePointer<D>::type);
 653   }
 654   D old = static_cast<const Derived*>(this)->fetch_and_add(addend, dest);
 655   return old + add_value;
 656 }
 657 
 658 template<typename Derived>
 659 template<typename I, typename D>
 660 inline D Atomic::AddAndFetch<Derived>::operator()(I add_value, D volatile* dest) const {
 661   // If D is a pointer type P*, scale by sizeof(P).
 662   if (IsPointer<D>::value) {
 663     add_value *= sizeof(typename RemovePointer<D>::type);
 664   }
 665   return static_cast<const Derived*>(this)->add_and_fetch(add_value, dest);
 666 }
 667 
 668 template<typename Type, typename Fn, typename I, typename D>
 669 inline D Atomic::add_using_helper(Fn fn, I add_value, D volatile* dest) {
 670   return PrimitiveConversions::cast<D>(
 671     fn(PrimitiveConversions::cast<Type>(add_value),
 672        reinterpret_cast<Type volatile*>(dest)));
 673 }
 674 
 675 template<typename T, typename D, typename U>
 676 inline D Atomic::cmpxchg(T exchange_value,
 677                          D volatile* dest,
 678                          U compare_value,
 679                          cmpxchg_memory_order order) {
 680   return CmpxchgImpl<T, D, U>()(exchange_value, dest, compare_value, order);
 681 }
 682 
 683 template<typename T, typename D>
 684 inline bool Atomic::replace_if_null(T* value, D* volatile* dest,
 685                                     cmpxchg_memory_order order) {
 686   // Presently using a trivial implementation in terms of cmpxchg.
 687   // Consider adding platform support, to permit the use of compiler
 688   // intrinsics like gcc's __sync_bool_compare_and_swap.
 689   D* expected_null = NULL;
 690   return expected_null == cmpxchg(value, dest, expected_null, order);
 691 }
 692 
 693 // Handle cmpxchg for integral and enum types.
 694 //
 695 // All the involved types must be identical.
 696 template<typename T>
 697 struct Atomic::CmpxchgImpl<
 698   T, T, T,
 699   typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
 700   VALUE_OBJ_CLASS_SPEC
 701 {
 702   T operator()(T exchange_value, T volatile* dest, T compare_value,
 703                cmpxchg_memory_order order) const {
 704     // Forward to the platform handler for the size of T.
 705     return PlatformCmpxchg<sizeof(T)>()(exchange_value,
 706                                         dest,
 707                                         compare_value,
 708                                         order);
 709   }
 710 };
 711 
 712 // Handle cmpxchg for pointer types.
 713 //
 714 // The destination's type and the compare_value type must be the same,
 715 // ignoring cv-qualifiers; we don't care about the cv-qualifiers of
 716 // the compare_value.
 717 //
 718 // The exchange_value must be implicitly convertible to the
 719 // destination's type; it must be type-correct to store the
 720 // exchange_value in the destination.
 721 template<typename T, typename D, typename U>
 722 struct Atomic::CmpxchgImpl<
 723   T*, D*, U*,
 724   typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value &&
 725                     IsSame<typename RemoveCV<D>::type,
 726                            typename RemoveCV<U>::type>::value>::type>
 727   VALUE_OBJ_CLASS_SPEC
 728 {
 729   D* operator()(T* exchange_value, D* volatile* dest, U* compare_value,
 730                cmpxchg_memory_order order) const {
 731     // Allow derived to base conversion, and adding cv-qualifiers.
 732     D* new_value = exchange_value;
 733     // Don't care what the CV qualifiers for compare_value are,
 734     // but we need to match D* when calling platform support.
 735     D* old_value = const_cast<D*>(compare_value);
 736     return PlatformCmpxchg<sizeof(D*)>()(new_value, dest, old_value, order);
 737   }
 738 };
 739 
 740 // Handle cmpxchg for types that have a translator.
 741 //
 742 // All the involved types must be identical.
 743 //
 744 // This translates the original call into a call on the decayed
 745 // arguments, and returns the recovered result of that translated
 746 // call.
 747 template<typename T>
 748 struct Atomic::CmpxchgImpl<
 749   T, T, T,
 750   typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
 751   VALUE_OBJ_CLASS_SPEC
 752 {
 753   T operator()(T exchange_value, T volatile* dest, T compare_value,
 754                cmpxchg_memory_order order) const {
 755     typedef PrimitiveConversions::Translate<T> Translator;
 756     typedef typename Translator::Decayed Decayed;
 757     STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
 758     return Translator::recover(
 759       cmpxchg(Translator::decay(exchange_value),
 760               reinterpret_cast<Decayed volatile*>(dest),
 761               Translator::decay(compare_value),
 762               order));
 763   }
 764 };
 765 
 766 template<typename Type, typename Fn, typename T>
 767 inline T Atomic::cmpxchg_using_helper(Fn fn,
 768                                       T exchange_value,
 769                                       T volatile* dest,
 770                                       T compare_value) {
 771   STATIC_ASSERT(sizeof(Type) == sizeof(T));
 772   return PrimitiveConversions::cast<T>(
 773     fn(PrimitiveConversions::cast<Type>(exchange_value),
 774        reinterpret_cast<Type volatile*>(dest),
 775        PrimitiveConversions::cast<Type>(compare_value)));
 776 }
 777 
 778 template<typename T>
 779 inline T Atomic::CmpxchgByteUsingInt::operator()(T exchange_value,
 780                                                  T volatile* dest,
 781                                                  T compare_value,
 782                                                  cmpxchg_memory_order order) const {
 783   STATIC_ASSERT(sizeof(T) == sizeof(uint8_t));
 784   uint8_t canon_exchange_value = exchange_value;
 785   uint8_t canon_compare_value = compare_value;
 786   volatile uint32_t* aligned_dest
 787     = reinterpret_cast<volatile uint32_t*>(align_down(dest, sizeof(uint32_t)));
 788   size_t offset = pointer_delta(dest, aligned_dest, 1);
 789   uint32_t cur = *aligned_dest;
 790   uint8_t* cur_as_bytes = reinterpret_cast<uint8_t*>(&cur);
 791 
 792   // current value may not be what we are looking for, so force it
 793   // to that value so the initial cmpxchg will fail if it is different
 794   cur_as_bytes[offset] = canon_compare_value;
 795 
 796   // always execute a real cmpxchg so that we get the required memory
 797   // barriers even on initial failure
 798   do {
 799     // value to swap in matches current value ...
 800     uint32_t new_value = cur;
 801     // ... except for the one jbyte we want to update
 802     reinterpret_cast<uint8_t*>(&new_value)[offset] = canon_exchange_value;
 803 
 804     uint32_t res = cmpxchg(new_value, aligned_dest, cur, order);
 805     if (res == cur) break;      // success
 806 
 807     // at least one byte in the int changed value, so update
 808     // our view of the current int
 809     cur = res;
 810     // if our byte is still as cur we loop and try again
 811   } while (cur_as_bytes[offset] == canon_compare_value);
 812 
 813   return PrimitiveConversions::cast<T>(cur_as_bytes[offset]);
 814 }
 815 
 816 // Handle xchg for integral and enum types.
 817 //
 818 // All the involved types must be identical.
 819 template<typename T>
 820 struct Atomic::XchgImpl<
 821   T, T,
 822   typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
 823   VALUE_OBJ_CLASS_SPEC
 824 {
 825   T operator()(T exchange_value, T volatile* dest) const {
 826     // Forward to the platform handler for the size of T.
 827     return PlatformXchg<sizeof(T)>()(exchange_value, dest);
 828   }
 829 };
 830 
 831 // Handle xchg for pointer types.
 832 //
 833 // The exchange_value must be implicitly convertible to the
 834 // destination's type; it must be type-correct to store the
 835 // exchange_value in the destination.
 836 template<typename T, typename D>
 837 struct Atomic::XchgImpl<
 838   T*, D*,
 839   typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value>::type>
 840   VALUE_OBJ_CLASS_SPEC
 841 {
 842   D* operator()(T* exchange_value, D* volatile* dest) const {
 843     // Allow derived to base conversion, and adding cv-qualifiers.
 844     D* new_value = exchange_value;
 845     return PlatformXchg<sizeof(D*)>()(new_value, dest);
 846   }
 847 };
 848 
 849 // Handle xchg for types that have a translator.
 850 //
 851 // All the involved types must be identical.
 852 //
 853 // This translates the original call into a call on the decayed
 854 // arguments, and returns the recovered result of that translated
 855 // call.
 856 template<typename T>
 857 struct Atomic::XchgImpl<
 858   T, T,
 859   typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
 860   VALUE_OBJ_CLASS_SPEC
 861 {
 862   T operator()(T exchange_value, T volatile* dest) const {
 863     typedef PrimitiveConversions::Translate<T> Translator;
 864     typedef typename Translator::Decayed Decayed;
 865     STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
 866     return Translator::recover(
 867       xchg(Translator::decay(exchange_value),
 868            reinterpret_cast<Decayed volatile*>(dest)));
 869   }
 870 };
 871 
 872 template<typename Type, typename Fn, typename T>
 873 inline T Atomic::xchg_using_helper(Fn fn,
 874                                    T exchange_value,
 875                                    T volatile* dest) {
 876   STATIC_ASSERT(sizeof(Type) == sizeof(T));
 877   return PrimitiveConversions::cast<T>(
 878     fn(PrimitiveConversions::cast<Type>(exchange_value),
 879        reinterpret_cast<Type volatile*>(dest)));
 880 }
 881 
 882 template<typename T, typename D>
 883 inline D Atomic::xchg(T exchange_value, volatile D* dest) {
 884   return XchgImpl<T, D>()(exchange_value, dest);
 885 }
 886 
 887 #endif // SHARE_VM_RUNTIME_ATOMIC_HPP