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 STATIC_ASSERT(IsIntegral<I>::value); 524 // If D is a pointer type, use [u]intptr_t as the addend type, 525 // matching signedness of I. Otherwise, use D as the addend type. 526 typedef typename Conditional<IsSigned<I>::value, intptr_t, uintptr_t>::type PI; 527 typedef typename Conditional<IsPointer<D>::value, PI, D>::type AddendType; 528 // Only allow conversions that can't change the value. 529 STATIC_ASSERT(IsSigned<I>::value == IsSigned<AddendType>::value); 530 STATIC_ASSERT(sizeof(I) <= sizeof(AddendType)); 531 AddendType addend = sub_value; 532 // Assumes two's complement integer representation. 533 #pragma warning(suppress: 4146) // In case AddendType is not signed. 534 return Atomic::add(-addend, dest); 535 } 536 537 // Define the class before including platform file, which may specialize 538 // the operator definition. No generic definition of specializations 539 // of the operator template are provided, nor are there any generic 540 // specializations of the class. The platform file is responsible for 541 // providing those. 542 template<size_t byte_size> 543 struct Atomic::PlatformCmpxchg VALUE_OBJ_CLASS_SPEC { 544 template<typename T> 545 T operator()(T exchange_value, 546 T volatile* dest, 547 T compare_value, 548 cmpxchg_memory_order order) const; 549 }; 550 551 // Define the class before including platform file, which may use this 552 // as a base class, requiring it be complete. The definition is later 553 // in this file, near the other definitions related to cmpxchg. 554 struct Atomic::CmpxchgByteUsingInt VALUE_OBJ_CLASS_SPEC { 555 template<typename T> 556 T operator()(T exchange_value, 557 T volatile* dest, 558 T compare_value, 559 cmpxchg_memory_order order) const; 560 }; 561 562 // Define the class before including platform file, which may specialize 563 // the operator definition. No generic definition of specializations 564 // of the operator template are provided, nor are there any generic 565 // specializations of the class. The platform file is responsible for 566 // providing those. 567 template<size_t byte_size> 568 struct Atomic::PlatformXchg VALUE_OBJ_CLASS_SPEC { 569 template<typename T> 570 T operator()(T exchange_value, 571 T volatile* dest) const; 572 }; 573 574 // platform specific in-line definitions - must come before shared definitions 575 576 #include OS_CPU_HEADER(atomic) 577 578 // shared in-line definitions 579 580 // size_t casts... 581 #if (SIZE_MAX != UINTPTR_MAX) 582 #error size_t is not WORD_SIZE, interesting platform, but missing implementation here 583 #endif 584 585 template<typename T> 586 inline T Atomic::load(const volatile T* dest) { 587 return LoadImpl<T, PlatformLoad<sizeof(T)> >()(dest); 588 } 589 590 template<typename T, typename D> 591 inline void Atomic::store(T store_value, volatile D* dest) { 592 StoreImpl<T, D, PlatformStore<sizeof(D)> >()(store_value, dest); 593 } 594 595 template<typename I, typename D> 596 inline D Atomic::add(I add_value, D volatile* dest) { 597 return AddImpl<I, D>()(add_value, dest); 598 } 599 600 template<typename I, typename D> 601 struct Atomic::AddImpl< 602 I, D, 603 typename EnableIf<IsIntegral<I>::value && 604 IsIntegral<D>::value && 605 (sizeof(I) <= sizeof(D)) && 606 (IsSigned<I>::value == IsSigned<D>::value)>::type> 607 VALUE_OBJ_CLASS_SPEC 608 { 609 D operator()(I add_value, D volatile* dest) const { 610 D addend = add_value; 611 return PlatformAdd<sizeof(D)>()(addend, dest); 612 } 613 }; 614 615 template<typename I, typename P> 616 struct Atomic::AddImpl< 617 I, P*, 618 typename EnableIf<IsIntegral<I>::value && (sizeof(I) <= sizeof(P*))>::type> 619 VALUE_OBJ_CLASS_SPEC 620 { 621 P* operator()(I add_value, P* volatile* dest) const { 622 STATIC_ASSERT(sizeof(intptr_t) == sizeof(P*)); 623 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(P*)); 624 typedef typename Conditional<IsSigned<I>::value, 625 intptr_t, 626 uintptr_t>::type CI; 627 CI addend = add_value; 628 return PlatformAdd<sizeof(P*)>()(addend, dest); 629 } 630 }; 631 632 // Most platforms do not support atomic add on a 2-byte value. However, 633 // if the value occupies the most significant 16 bits of an aligned 32-bit 634 // word, then we can do this with an atomic add of (add_value << 16) 635 // to the 32-bit word. 636 // 637 // The least significant parts of this 32-bit word will never be affected, even 638 // in case of overflow/underflow. 639 // 640 // Use the ATOMIC_SHORT_PAIR macro (see macros.hpp) to get the desired alignment. 641 template<> 642 struct Atomic::AddImpl<jshort, jshort> VALUE_OBJ_CLASS_SPEC { 643 jshort operator()(jshort add_value, jshort volatile* dest) const { 644 #ifdef VM_LITTLE_ENDIAN 645 assert((intx(dest) & 0x03) == 0x02, "wrong alignment"); 646 jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest-1)); 647 #else 648 assert((intx(dest) & 0x03) == 0x00, "wrong alignment"); 649 jint new_value = Atomic::add(add_value << 16, (volatile jint*)(dest)); 650 #endif 651 return (jshort)(new_value >> 16); // preserves sign 652 } 653 }; 654 655 template<typename Derived> 656 template<typename I, typename D> 657 inline D Atomic::FetchAndAdd<Derived>::operator()(I add_value, D volatile* dest) const { 658 I addend = add_value; 659 // If D is a pointer type P*, scale by sizeof(P). 660 if (IsPointer<D>::value) { 661 addend *= sizeof(typename RemovePointer<D>::type); 662 } 663 D old = static_cast<const Derived*>(this)->fetch_and_add(addend, dest); 664 return old + add_value; 665 } 666 667 template<typename Derived> 668 template<typename I, typename D> 669 inline D Atomic::AddAndFetch<Derived>::operator()(I add_value, D volatile* dest) const { 670 // If D is a pointer type P*, scale by sizeof(P). 671 if (IsPointer<D>::value) { 672 add_value *= sizeof(typename RemovePointer<D>::type); 673 } 674 return static_cast<const Derived*>(this)->add_and_fetch(add_value, dest); 675 } 676 677 template<typename Type, typename Fn, typename I, typename D> 678 inline D Atomic::add_using_helper(Fn fn, I add_value, D volatile* dest) { 679 return PrimitiveConversions::cast<D>( 680 fn(PrimitiveConversions::cast<Type>(add_value), 681 reinterpret_cast<Type volatile*>(dest))); 682 } 683 684 template<typename T, typename D, typename U> 685 inline D Atomic::cmpxchg(T exchange_value, 686 D volatile* dest, 687 U compare_value, 688 cmpxchg_memory_order order) { 689 return CmpxchgImpl<T, D, U>()(exchange_value, dest, compare_value, order); 690 } 691 692 template<typename T, typename D> 693 inline bool Atomic::replace_if_null(T* value, D* volatile* dest, 694 cmpxchg_memory_order order) { 695 // Presently using a trivial implementation in terms of cmpxchg. 696 // Consider adding platform support, to permit the use of compiler 697 // intrinsics like gcc's __sync_bool_compare_and_swap. 698 D* expected_null = NULL; 699 return expected_null == cmpxchg(value, dest, expected_null, order); 700 } 701 702 // Handle cmpxchg for integral and enum types. 703 // 704 // All the involved types must be identical. 705 template<typename T> 706 struct Atomic::CmpxchgImpl< 707 T, T, T, 708 typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type> 709 VALUE_OBJ_CLASS_SPEC 710 { 711 T operator()(T exchange_value, T volatile* dest, T compare_value, 712 cmpxchg_memory_order order) const { 713 // Forward to the platform handler for the size of T. 714 return PlatformCmpxchg<sizeof(T)>()(exchange_value, 715 dest, 716 compare_value, 717 order); 718 } 719 }; 720 721 // Handle cmpxchg for pointer types. 722 // 723 // The destination's type and the compare_value type must be the same, 724 // ignoring cv-qualifiers; we don't care about the cv-qualifiers of 725 // the compare_value. 726 // 727 // The exchange_value must be implicitly convertible to the 728 // destination's type; it must be type-correct to store the 729 // exchange_value in the destination. 730 template<typename T, typename D, typename U> 731 struct Atomic::CmpxchgImpl< 732 T*, D*, U*, 733 typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value && 734 IsSame<typename RemoveCV<D>::type, 735 typename RemoveCV<U>::type>::value>::type> 736 VALUE_OBJ_CLASS_SPEC 737 { 738 D* operator()(T* exchange_value, D* volatile* dest, U* compare_value, 739 cmpxchg_memory_order order) const { 740 // Allow derived to base conversion, and adding cv-qualifiers. 741 D* new_value = exchange_value; 742 // Don't care what the CV qualifiers for compare_value are, 743 // but we need to match D* when calling platform support. 744 D* old_value = const_cast<D*>(compare_value); 745 return PlatformCmpxchg<sizeof(D*)>()(new_value, dest, old_value, order); 746 } 747 }; 748 749 // Handle cmpxchg for types that have a translator. 750 // 751 // All the involved types must be identical. 752 // 753 // This translates the original call into a call on the decayed 754 // arguments, and returns the recovered result of that translated 755 // call. 756 template<typename T> 757 struct Atomic::CmpxchgImpl< 758 T, T, T, 759 typename EnableIf<PrimitiveConversions::Translate<T>::value>::type> 760 VALUE_OBJ_CLASS_SPEC 761 { 762 T operator()(T exchange_value, T volatile* dest, T compare_value, 763 cmpxchg_memory_order order) const { 764 typedef PrimitiveConversions::Translate<T> Translator; 765 typedef typename Translator::Decayed Decayed; 766 STATIC_ASSERT(sizeof(T) == sizeof(Decayed)); 767 return Translator::recover( 768 cmpxchg(Translator::decay(exchange_value), 769 reinterpret_cast<Decayed volatile*>(dest), 770 Translator::decay(compare_value), 771 order)); 772 } 773 }; 774 775 template<typename Type, typename Fn, typename T> 776 inline T Atomic::cmpxchg_using_helper(Fn fn, 777 T exchange_value, 778 T volatile* dest, 779 T compare_value) { 780 STATIC_ASSERT(sizeof(Type) == sizeof(T)); 781 return PrimitiveConversions::cast<T>( 782 fn(PrimitiveConversions::cast<Type>(exchange_value), 783 reinterpret_cast<Type volatile*>(dest), 784 PrimitiveConversions::cast<Type>(compare_value))); 785 } 786 787 template<typename T> 788 inline T Atomic::CmpxchgByteUsingInt::operator()(T exchange_value, 789 T volatile* dest, 790 T compare_value, 791 cmpxchg_memory_order order) const { 792 STATIC_ASSERT(sizeof(T) == sizeof(uint8_t)); 793 uint8_t canon_exchange_value = exchange_value; 794 uint8_t canon_compare_value = compare_value; 795 volatile uint32_t* aligned_dest 796 = reinterpret_cast<volatile uint32_t*>(align_down(dest, sizeof(uint32_t))); 797 size_t offset = pointer_delta(dest, aligned_dest, 1); 798 uint32_t cur = *aligned_dest; 799 uint8_t* cur_as_bytes = reinterpret_cast<uint8_t*>(&cur); 800 801 // current value may not be what we are looking for, so force it 802 // to that value so the initial cmpxchg will fail if it is different 803 cur_as_bytes[offset] = canon_compare_value; 804 805 // always execute a real cmpxchg so that we get the required memory 806 // barriers even on initial failure 807 do { 808 // value to swap in matches current value ... 809 uint32_t new_value = cur; 810 // ... except for the one jbyte we want to update 811 reinterpret_cast<uint8_t*>(&new_value)[offset] = canon_exchange_value; 812 813 uint32_t res = cmpxchg(new_value, aligned_dest, cur, order); 814 if (res == cur) break; // success 815 816 // at least one byte in the int changed value, so update 817 // our view of the current int 818 cur = res; 819 // if our byte is still as cur we loop and try again 820 } while (cur_as_bytes[offset] == canon_compare_value); 821 822 return PrimitiveConversions::cast<T>(cur_as_bytes[offset]); 823 } 824 825 // Handle xchg for integral and enum types. 826 // 827 // All the involved types must be identical. 828 template<typename T> 829 struct Atomic::XchgImpl< 830 T, T, 831 typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type> 832 VALUE_OBJ_CLASS_SPEC 833 { 834 T operator()(T exchange_value, T volatile* dest) const { 835 // Forward to the platform handler for the size of T. 836 return PlatformXchg<sizeof(T)>()(exchange_value, dest); 837 } 838 }; 839 840 // Handle xchg for pointer types. 841 // 842 // The exchange_value must be implicitly convertible to the 843 // destination's type; it must be type-correct to store the 844 // exchange_value in the destination. 845 template<typename T, typename D> 846 struct Atomic::XchgImpl< 847 T*, D*, 848 typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value>::type> 849 VALUE_OBJ_CLASS_SPEC 850 { 851 D* operator()(T* exchange_value, D* volatile* dest) const { 852 // Allow derived to base conversion, and adding cv-qualifiers. 853 D* new_value = exchange_value; 854 return PlatformXchg<sizeof(D*)>()(new_value, dest); 855 } 856 }; 857 858 // Handle xchg for types that have a translator. 859 // 860 // All the involved types must be identical. 861 // 862 // This translates the original call into a call on the decayed 863 // arguments, and returns the recovered result of that translated 864 // call. 865 template<typename T> 866 struct Atomic::XchgImpl< 867 T, T, 868 typename EnableIf<PrimitiveConversions::Translate<T>::value>::type> 869 VALUE_OBJ_CLASS_SPEC 870 { 871 T operator()(T exchange_value, T volatile* dest) const { 872 typedef PrimitiveConversions::Translate<T> Translator; 873 typedef typename Translator::Decayed Decayed; 874 STATIC_ASSERT(sizeof(T) == sizeof(Decayed)); 875 return Translator::recover( 876 xchg(Translator::decay(exchange_value), 877 reinterpret_cast<Decayed volatile*>(dest))); 878 } 879 }; 880 881 template<typename Type, typename Fn, typename T> 882 inline T Atomic::xchg_using_helper(Fn fn, 883 T exchange_value, 884 T volatile* dest) { 885 STATIC_ASSERT(sizeof(Type) == sizeof(T)); 886 return PrimitiveConversions::cast<T>( 887 fn(PrimitiveConversions::cast<Type>(exchange_value), 888 reinterpret_cast<Type volatile*>(dest))); 889 } 890 891 template<typename T, typename D> 892 inline D Atomic::xchg(T exchange_value, volatile D* dest) { 893 return XchgImpl<T, D>()(exchange_value, dest); 894 } 895 896 #endif // SHARE_VM_RUNTIME_ATOMIC_HPP