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