src/hotspot/share/runtime/atomic.hpp
author stefank
Mon, 25 Nov 2019 12:22:13 +0100
changeset 59247 56bf71d64d51
parent 53244 9807daeb47c4
child 59248 e92153ed8bdc
permissions -rw-r--r--
8234562: Move OrderAccess::release_store*/load_acquire to Atomic Reviewed-by: rehn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51330
diff changeset
     2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3795
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3795
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3795
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51330
diff changeset
    25
#ifndef SHARE_RUNTIME_ATOMIC_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51330
diff changeset
    26
#define SHARE_RUNTIME_ATOMIC_HPP
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
    29
#include "metaprogramming/conditional.hpp"
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
    30
#include "metaprogramming/enableIf.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
    31
#include "metaprogramming/isIntegral.hpp"
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
    32
#include "metaprogramming/isPointer.hpp"
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
    33
#include "metaprogramming/isSame.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
    34
#include "metaprogramming/primitiveConversions.hpp"
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
    35
#include "metaprogramming/removeCV.hpp"
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
    36
#include "metaprogramming/removePointer.hpp"
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    37
#include "runtime/orderAccess.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    38
#include "utilities/align.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
    39
#include "utilities/macros.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    40
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    41
enum atomic_memory_order {
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    42
  // The modes that align with C++11 are intended to
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    43
  // follow the same semantics.
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    44
  memory_order_relaxed = 0,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    45
  memory_order_acquire = 2,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    46
  memory_order_release = 3,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    47
  memory_order_acq_rel = 4,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
    48
  // Strong two-way memory barrier.
39404
d0ad5220e91c 8155949: Support relaxed semantics in cmpxchg
mdoerr
parents: 27691
diff changeset
    49
  memory_order_conservative = 8
d0ad5220e91c 8155949: Support relaxed semantics in cmpxchg
mdoerr
parents: 27691
diff changeset
    50
};
d0ad5220e91c 8155949: Support relaxed semantics in cmpxchg
mdoerr
parents: 27691
diff changeset
    51
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    52
enum ScopedFenceType {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    53
    X_ACQUIRE
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    54
  , RELEASE_X
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    55
  , RELEASE_X_FENCE
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    56
};
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    57
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
class Atomic : AllStatic {
47609
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47593
diff changeset
    59
public:
48468
7cc7de9bf4a4 8186903: Remove j-types from Atomic
coleenp
parents: 47634
diff changeset
    60
  // Atomic operations on int64 types are not available on all 32-bit
7cc7de9bf4a4 8186903: Remove j-types from Atomic
coleenp
parents: 47634
diff changeset
    61
  // platforms. If atomic ops on int64 are defined here they must only
15803
9328492899cc 8008314: Unimplemented() Atomic::load breaks the applications
jwilhelm
parents: 14626
diff changeset
    62
  // be used from code that verifies they are available at runtime and
9328492899cc 8008314: Unimplemented() Atomic::load breaks the applications
jwilhelm
parents: 14626
diff changeset
    63
  // can provide an alternative action if not - see supports_cx8() for
9328492899cc 8008314: Unimplemented() Atomic::load breaks the applications
jwilhelm
parents: 14626
diff changeset
    64
  // a means to test availability.
9328492899cc 8008314: Unimplemented() Atomic::load breaks the applications
jwilhelm
parents: 14626
diff changeset
    65
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    66
  // The memory operations that are mentioned with each of the atomic
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    67
  // function families come from src/share/vm/runtime/orderAccess.hpp,
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    68
  // e.g., <fence> is described in that file and is implemented by the
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    69
  // OrderAccess::fence() function. See that file for the gory details
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    70
  // on the Memory Access Ordering Model.
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    71
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    72
  // All of the atomic operations that imply a read-modify-write action
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    73
  // guarantee a two-way memory barrier across that operation. Historically
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    74
  // these semantics reflect the strength of atomic operations that are
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    75
  // provided on SPARC/X86. We assume that strength is necessary unless
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    76
  // we can prove that a weaker form is sufficiently safe.
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
    77
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // Atomically store to a location
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    79
  // The type T must be either a pointer type convertible to or equal
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    80
  // to D, an integral/enum type equal to D, or a type equal to D that
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    81
  // is primitive convertible using PrimitiveConversions.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    82
  template<typename T, typename D>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    83
  inline static void store(T store_value, volatile D* dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    84
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    85
  template <typename T, typename D>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    86
  inline static void release_store(volatile D* dest, T store_value);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    87
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    88
  template <typename T, typename D>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    89
  inline static void release_store_fence(volatile D* dest, T store_value);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    90
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    91
  // Atomically load from a location
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    92
  // The type T must be either a pointer type, an integral/enum type,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    93
  // or a type that is primitive convertible using PrimitiveConversions.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    94
  template<typename T>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
    95
  inline static T load(const volatile T* dest);
3594
0ce9158bc84c 6863420: os::javaTimeNanos() go backward on Solaris x86
kvn
parents: 670
diff changeset
    96
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    97
  template <typename T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    98
  inline static T load_acquire(const volatile T* dest);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
    99
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   100
  // Atomically add to a location. Returns updated value. add*() provide:
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   101
  // <fence> add-value-to-dest <membar StoreLoad|StoreStore>
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   102
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   103
  template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   104
  inline static D add(I add_value, D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   105
                      atomic_memory_order order = memory_order_conservative);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   106
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   107
  template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   108
  inline static D sub(I sub_value, D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   109
                      atomic_memory_order order = memory_order_conservative);
9990
c8683968c01b 6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents: 7397
diff changeset
   110
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   111
  // Atomically increment location. inc() provide:
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   112
  // <fence> increment-dest <membar StoreLoad|StoreStore>
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   113
  // The type D may be either a pointer type, or an integral
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   114
  // type. If it is a pointer type, then the increment is
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   115
  // scaled to the size of the type pointed to by the pointer.
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   116
  template<typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   117
  inline static void inc(D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   118
                         atomic_memory_order order = memory_order_conservative);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   120
  // Atomically decrement a location. dec() provide:
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   121
  // <fence> decrement-dest <membar StoreLoad|StoreStore>
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   122
  // The type D may be either a pointer type, or an integral
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   123
  // type. If it is a pointer type, then the decrement is
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   124
  // scaled to the size of the type pointed to by the pointer.
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   125
  template<typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   126
  inline static void dec(D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   127
                         atomic_memory_order order = memory_order_conservative);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   129
  // Performs atomic exchange of *dest with exchange_value. Returns old
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   130
  // prior value of *dest. xchg*() provide:
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   131
  // <fence> exchange-value-with-dest <membar StoreLoad|StoreStore>
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   132
  // The type T must be either a pointer type convertible to or equal
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   133
  // to D, an integral/enum type equal to D, or a type equal to D that
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   134
  // is primitive convertible using PrimitiveConversions.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   135
  template<typename T, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   136
  inline static D xchg(T exchange_value, volatile D* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   137
                       atomic_memory_order order = memory_order_conservative);
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   138
25625
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   139
  // Performs atomic compare of *dest and compare_value, and exchanges
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   140
  // *dest with exchange_value if the comparison succeeded. Returns prior
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   141
  // value of *dest. cmpxchg*() provide:
00dca966711e 8049104: resolve atomic.hpp wording issues from JDK-8047104 code review
dcubed
parents: 25069
diff changeset
   142
  // <fence> compare-and-exchange <membar StoreLoad|StoreStore>
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   143
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   144
  template<typename T, typename D, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   145
  inline static D cmpxchg(T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   146
                          D volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   147
                          U compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   148
                          atomic_memory_order order = memory_order_conservative);
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   149
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   150
  // Performs atomic compare of *dest and NULL, and replaces *dest
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   151
  // with exchange_value if the comparison succeeded.  Returns true if
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   152
  // the comparison succeeded and the exchange occurred.  This is
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   153
  // often used as part of lazy initialization, as a lock-free
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   154
  // alternative to the Double-Checked Locking Pattern.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   155
  template<typename T, typename D>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   156
  inline static bool replace_if_null(T* value, D* volatile* dest,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   157
                                     atomic_memory_order order = memory_order_conservative);
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   158
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   159
private:
49022
83fd70753121 8196889: VS2017 Unable to Instantiate OrderAccess::release_store with an Incomplete Class Within an Inlined Method
lfoltan
parents: 48468
diff changeset
   160
WINDOWS_ONLY(public:) // VS2017 warns (C2027) use of undefined type if IsPointerConvertible is declared private
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   161
  // Test whether From is implicitly convertible to To.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   162
  // From and To must be pointer types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   163
  // Note: Provides the limited subset of C++11 std::is_convertible
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   164
  // that is needed here.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   165
  template<typename From, typename To> struct IsPointerConvertible;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   166
47609
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47593
diff changeset
   167
protected:
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   168
  // Dispatch handler for store.  Provides type-based validity
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   169
  // checking and limited conversions around calls to the platform-
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   170
  // specific implementation layer provided by PlatformOp.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   171
  template<typename T, typename D, typename PlatformOp, typename Enable = void>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   172
  struct StoreImpl;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   173
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   174
  // Platform-specific implementation of store.  Support for sizes
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   175
  // of 1, 2, 4, and (if different) pointer size bytes are required.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   176
  // The class is a function object that must be default constructable,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   177
  // with these requirements:
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   178
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   179
  // either:
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   180
  // - dest is of type D*, an integral, enum or pointer type.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   181
  // - new_value are of type T, an integral, enum or pointer type D or
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   182
  //   pointer type convertible to D.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   183
  // or:
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   184
  // - T and D are the same and are primitive convertible using PrimitiveConversions
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   185
  // and either way:
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   186
  // - platform_store is an object of type PlatformStore<sizeof(T)>.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   187
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   188
  // Then
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   189
  //   platform_store(new_value, dest)
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   190
  // must be a valid expression.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   191
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   192
  // The default implementation is a volatile store. If a platform
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   193
  // requires more for e.g. 64 bit stores, a specialization is required
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   194
  template<size_t byte_size> struct PlatformStore;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   195
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   196
  // Dispatch handler for load.  Provides type-based validity
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   197
  // checking and limited conversions around calls to the platform-
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   198
  // specific implementation layer provided by PlatformOp.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   199
  template<typename T, typename PlatformOp, typename Enable = void>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   200
  struct LoadImpl;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   201
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   202
  // Platform-specific implementation of load. Support for sizes of
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   203
  // 1, 2, 4 bytes and (if different) pointer size bytes are required.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   204
  // The class is a function object that must be default
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   205
  // constructable, with these requirements:
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   206
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   207
  // - dest is of type T*, an integral, enum or pointer type, or
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   208
  //   T is convertible to a primitive type using PrimitiveConversions
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   209
  // - platform_load is an object of type PlatformLoad<sizeof(T)>.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   210
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   211
  // Then
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   212
  //   platform_load(src)
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   213
  // must be a valid expression, returning a result convertible to T.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   214
  //
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   215
  // The default implementation is a volatile load. If a platform
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   216
  // requires more for e.g. 64 bit loads, a specialization is required
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   217
  template<size_t byte_size> struct PlatformLoad;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   218
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   219
  // Give platforms a variation point to specialize.
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   220
  template<size_t byte_size, ScopedFenceType type> struct PlatformOrderedStore;
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   221
  template<size_t byte_size, ScopedFenceType type> struct PlatformOrderedLoad;
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   222
47609
a1f68e415b48 8188813: Generalize OrderAccess to use templates
eosterlund
parents: 47593
diff changeset
   223
private:
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   224
  // Dispatch handler for add.  Provides type-based validity checking
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   225
  // and limited conversions around calls to the platform-specific
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   226
  // implementation layer provided by PlatformAdd.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   227
  template<typename I, typename D, typename Enable = void>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   228
  struct AddImpl;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   229
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   230
  // Platform-specific implementation of add.  Support for sizes of 4
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   231
  // bytes and (if different) pointer size bytes are required.  The
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   232
  // class is a function object that must be default constructable,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   233
  // with these requirements:
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   234
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   235
  // - dest is of type D*, an integral or pointer type.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   236
  // - add_value is of type I, an integral type.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   237
  // - sizeof(I) == sizeof(D).
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   238
  // - if D is an integral type, I == D.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   239
  // - platform_add is an object of type PlatformAdd<sizeof(D)>.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   240
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   241
  // Then
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   242
  //   platform_add(add_value, dest)
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   243
  // must be a valid expression, returning a result convertible to D.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   244
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   245
  // No definition is provided; all platforms must explicitly define
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   246
  // this class and any needed specializations.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   247
  template<size_t byte_size> struct PlatformAdd;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   248
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   249
  // Helper base classes for defining PlatformAdd.  To use, define
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   250
  // PlatformAdd or a specialization that derives from one of these,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   251
  // and include in the PlatformAdd definition the support function
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   252
  // (described below) required by the base class.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   253
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   254
  // These classes implement the required function object protocol for
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   255
  // PlatformAdd, using a support function template provided by the
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   256
  // derived class.  Let add_value (of type I) and dest (of type D) be
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   257
  // the arguments the object is called with.  If D is a pointer type
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   258
  // P*, then let addend (of type I) be add_value * sizeof(P);
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   259
  // otherwise, addend is add_value.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   260
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   261
  // FetchAndAdd requires the derived class to provide
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   262
  //   fetch_and_add(addend, dest)
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   263
  // atomically adding addend to the value of dest, and returning the
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   264
  // old value.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   265
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   266
  // AddAndFetch requires the derived class to provide
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   267
  //   add_and_fetch(addend, dest)
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   268
  // atomically adding addend to the value of dest, and returning the
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   269
  // new value.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   270
  //
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   271
  // When D is a pointer type P*, both fetch_and_add and add_and_fetch
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   272
  // treat it as if it were a uintptr_t; they do not perform any
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   273
  // scaling of the addend, as that has already been done by the
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   274
  // caller.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   275
public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   276
  template<typename Derived> struct FetchAndAdd;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   277
  template<typename Derived> struct AddAndFetch;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   278
private:
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   279
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   280
  // Support for platforms that implement some variants of add using a
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   281
  // (typically out of line) non-template helper function.  The
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   282
  // generic arguments passed to PlatformAdd need to be translated to
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   283
  // the appropriate type for the helper function, the helper function
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   284
  // invoked on the translated arguments, and the result translated
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   285
  // back.  Type is the parameter / return type of the helper
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   286
  // function.  No scaling of add_value is performed when D is a pointer
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   287
  // type, so this function can be used to implement the support function
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   288
  // required by AddAndFetch.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   289
  template<typename Type, typename Fn, typename I, typename D>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   290
  static D add_using_helper(Fn fn, I add_value, D volatile* dest);
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   291
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   292
  // Dispatch handler for cmpxchg.  Provides type-based validity
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   293
  // checking and limited conversions around calls to the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   294
  // platform-specific implementation layer provided by
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   295
  // PlatformCmpxchg.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   296
  template<typename T, typename D, typename U, typename Enable = void>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   297
  struct CmpxchgImpl;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   298
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   299
  // Platform-specific implementation of cmpxchg.  Support for sizes
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   300
  // of 1, 4, and 8 are required.  The class is a function object that
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   301
  // must be default constructable, with these requirements:
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   302
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   303
  // - dest is of type T*.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   304
  // - exchange_value and compare_value are of type T.
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   305
  // - order is of type atomic_memory_order.
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   306
  // - platform_cmpxchg is an object of type PlatformCmpxchg<sizeof(T)>.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   307
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   308
  // Then
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   309
  //   platform_cmpxchg(exchange_value, dest, compare_value, order)
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   310
  // must be a valid expression, returning a result convertible to T.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   311
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   312
  // A default definition is provided, which declares a function template
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   313
  //   T operator()(T, T volatile*, T, atomic_memory_order) const
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   314
  //
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   315
  // For each required size, a platform must either provide an
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   316
  // appropriate definition of that function, or must entirely
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   317
  // specialize the class template for that size.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   318
  template<size_t byte_size> struct PlatformCmpxchg;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   319
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   320
  // Support for platforms that implement some variants of cmpxchg
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   321
  // using a (typically out of line) non-template helper function.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   322
  // The generic arguments passed to PlatformCmpxchg need to be
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   323
  // translated to the appropriate type for the helper function, the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   324
  // helper invoked on the translated arguments, and the result
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   325
  // translated back.  Type is the parameter / return type of the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   326
  // helper function.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   327
  template<typename Type, typename Fn, typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   328
  static T cmpxchg_using_helper(Fn fn,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   329
                                T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   330
                                T volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   331
                                T compare_value);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   332
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   333
  // Support platforms that do not provide Read-Modify-Write
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   334
  // byte-level atomic access. To use, derive PlatformCmpxchg<1> from
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   335
  // this class.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   336
public: // Temporary, can't be private: C++03 11.4/2. Fixed by C++11.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   337
  struct CmpxchgByteUsingInt;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   338
private:
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   339
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   340
  // Dispatch handler for xchg.  Provides type-based validity
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   341
  // checking and limited conversions around calls to the
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   342
  // platform-specific implementation layer provided by
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   343
  // PlatformXchg.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   344
  template<typename T, typename D, typename Enable = void>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   345
  struct XchgImpl;
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   346
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   347
  // Platform-specific implementation of xchg.  Support for sizes
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   348
  // of 4, and sizeof(intptr_t) are required.  The class is a function
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   349
  // object that must be default constructable, with these requirements:
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   350
  //
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   351
  // - dest is of type T*.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   352
  // - exchange_value is of type T.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   353
  // - platform_xchg is an object of type PlatformXchg<sizeof(T)>.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   354
  //
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   355
  // Then
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   356
  //   platform_xchg(exchange_value, dest)
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   357
  // must be a valid expression, returning a result convertible to T.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   358
  //
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   359
  // A default definition is provided, which declares a function template
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   360
  //   T operator()(T, T volatile*, T, atomic_memory_order) const
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   361
  //
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   362
  // For each required size, a platform must either provide an
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   363
  // appropriate definition of that function, or must entirely
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   364
  // specialize the class template for that size.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   365
  template<size_t byte_size> struct PlatformXchg;
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   366
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   367
  // Support for platforms that implement some variants of xchg
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   368
  // using a (typically out of line) non-template helper function.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   369
  // The generic arguments passed to PlatformXchg need to be
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   370
  // translated to the appropriate type for the helper function, the
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   371
  // helper invoked on the translated arguments, and the result
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   372
  // translated back.  Type is the parameter / return type of the
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   373
  // helper function.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   374
  template<typename Type, typename Fn, typename T>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   375
  static T xchg_using_helper(Fn fn,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   376
                             T exchange_value,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   377
                             T volatile* dest);
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   378
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   379
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   380
template<typename From, typename To>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   381
struct Atomic::IsPointerConvertible<From*, To*> : AllStatic {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   382
  // Determine whether From* is implicitly convertible to To*, using
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   383
  // the "sizeof trick".
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   384
  typedef char yes;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   385
  typedef char (&no)[2];
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   386
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   387
  static yes test(To*);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   388
  static no test(...);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   389
  static From* test_value;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   390
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   391
  static const bool value = (sizeof(yes) == sizeof(test(test_value)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   392
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   393
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   394
// Handle load for pointer, integral and enum types.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   395
template<typename T, typename PlatformOp>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   396
struct Atomic::LoadImpl<
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   397
  T,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   398
  PlatformOp,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   399
  typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value || IsPointer<T>::value>::type>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   400
{
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   401
  T operator()(T const volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   402
    // Forward to the platform handler for the size of T.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   403
    return PlatformOp()(dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   404
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   405
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   406
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   407
// Handle load for types that have a translator.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   408
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   409
// All the involved types must be identical.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   410
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   411
// This translates the original call into a call on the decayed
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   412
// arguments, and returns the recovered result of that translated
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   413
// call.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   414
template<typename T, typename PlatformOp>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   415
struct Atomic::LoadImpl<
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   416
  T,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   417
  PlatformOp,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   418
  typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   419
{
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   420
  T operator()(T const volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   421
    typedef PrimitiveConversions::Translate<T> Translator;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   422
    typedef typename Translator::Decayed Decayed;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   423
    STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   424
    Decayed result = PlatformOp()(reinterpret_cast<Decayed const volatile*>(dest));
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   425
    return Translator::recover(result);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   426
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   427
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   428
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   429
// Default implementation of atomic load if a specific platform
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   430
// does not provide a specialization for a certain size class.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   431
// For increased safety, the default implementation only allows
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   432
// load types that are pointer sized or smaller. If a platform still
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   433
// supports wide atomics, then it has to use specialization
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   434
// of Atomic::PlatformLoad for that wider size class.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   435
template<size_t byte_size>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   436
struct Atomic::PlatformLoad {
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   437
  template<typename T>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   438
  T operator()(T const volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   439
    STATIC_ASSERT(sizeof(T) <= sizeof(void*)); // wide atomics need specialization
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   440
    return *dest;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   441
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   442
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   443
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   444
// Handle store for integral and enum types.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   445
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   446
// All the involved types must be identical.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   447
template<typename T, typename PlatformOp>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   448
struct Atomic::StoreImpl<
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   449
  T, T,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   450
  PlatformOp,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   451
  typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   452
{
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   453
  void operator()(T new_value, T volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   454
    // Forward to the platform handler for the size of T.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   455
    PlatformOp()(new_value, dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   456
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   457
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   458
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   459
// Handle store for pointer types.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   460
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   461
// The new_value must be implicitly convertible to the
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   462
// destination's type; it must be type-correct to store the
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   463
// new_value in the destination.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   464
template<typename T, typename D, typename PlatformOp>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   465
struct Atomic::StoreImpl<
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   466
  T*, D*,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   467
  PlatformOp,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   468
  typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value>::type>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   469
{
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   470
  void operator()(T* new_value, D* volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   471
    // Allow derived to base conversion, and adding cv-qualifiers.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   472
    D* value = new_value;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   473
    PlatformOp()(value, dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   474
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   475
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   476
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   477
// Handle store for types that have a translator.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   478
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   479
// All the involved types must be identical.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   480
//
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   481
// This translates the original call into a call on the decayed
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   482
// arguments.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   483
template<typename T, typename PlatformOp>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   484
struct Atomic::StoreImpl<
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   485
  T, T,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   486
  PlatformOp,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   487
  typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   488
{
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   489
  void operator()(T new_value, T volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   490
    typedef PrimitiveConversions::Translate<T> Translator;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   491
    typedef typename Translator::Decayed Decayed;
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   492
    STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   493
    PlatformOp()(Translator::decay(new_value),
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   494
                 reinterpret_cast<Decayed volatile*>(dest));
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   495
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   496
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   497
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   498
// Default implementation of atomic store if a specific platform
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   499
// does not provide a specialization for a certain size class.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   500
// For increased safety, the default implementation only allows
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   501
// storing types that are pointer sized or smaller. If a platform still
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   502
// supports wide atomics, then it has to use specialization
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   503
// of Atomic::PlatformStore for that wider size class.
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   504
template<size_t byte_size>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   505
struct Atomic::PlatformStore {
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   506
  template<typename T>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   507
  void operator()(T new_value,
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   508
                  T volatile* dest) const {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   509
    STATIC_ASSERT(sizeof(T) <= sizeof(void*)); // wide atomics need specialization
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   510
    (void)const_cast<T&>(*dest = new_value);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   511
  }
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   512
};
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   513
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   514
// Define FetchAndAdd and AddAndFetch helper classes before including
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   515
// platform file, which may use these as base classes, requiring they
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   516
// be complete.
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   517
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   518
template<typename Derived>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   519
struct Atomic::FetchAndAdd {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   520
  template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   521
  D operator()(I add_value, D volatile* dest, atomic_memory_order order) const;
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   522
};
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   523
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   524
template<typename Derived>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   525
struct Atomic::AddAndFetch {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   526
  template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   527
  D operator()(I add_value, D volatile* dest, atomic_memory_order order) const;
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   528
};
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   529
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   530
template<typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   531
inline void Atomic::inc(D volatile* dest, atomic_memory_order order) {
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   532
  STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   533
  typedef typename Conditional<IsPointer<D>::value, ptrdiff_t, D>::type I;
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   534
  Atomic::add(I(1), dest, order);
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   535
}
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   536
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   537
template<typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   538
inline void Atomic::dec(D volatile* dest, atomic_memory_order order) {
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   539
  STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   540
  typedef typename Conditional<IsPointer<D>::value, ptrdiff_t, D>::type I;
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   541
  // Assumes two's complement integer representation.
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   542
  #pragma warning(suppress: 4146)
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   543
  Atomic::add(I(-1), dest, order);
47552
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   544
}
8a3599d60996 8186838: Generalize Atomic::inc/dec with templates
eosterlund
parents: 47216
diff changeset
   545
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   546
template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   547
inline D Atomic::sub(I sub_value, D volatile* dest, atomic_memory_order order) {
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   548
  STATIC_ASSERT(IsPointer<D>::value || IsIntegral<D>::value);
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   549
  STATIC_ASSERT(IsIntegral<I>::value);
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   550
  // If D is a pointer type, use [u]intptr_t as the addend type,
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   551
  // matching signedness of I.  Otherwise, use D as the addend type.
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   552
  typedef typename Conditional<IsSigned<I>::value, intptr_t, uintptr_t>::type PI;
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   553
  typedef typename Conditional<IsPointer<D>::value, PI, D>::type AddendType;
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   554
  // Only allow conversions that can't change the value.
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   555
  STATIC_ASSERT(IsSigned<I>::value == IsSigned<AddendType>::value);
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   556
  STATIC_ASSERT(sizeof(I) <= sizeof(AddendType));
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   557
  AddendType addend = sub_value;
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   558
  // Assumes two's complement integer representation.
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   559
  #pragma warning(suppress: 4146) // In case AddendType is not signed.
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   560
  return Atomic::add(-addend, dest, order);
47634
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   561
}
6a0c42c40cd1 8188220: Remove Atomic::*_ptr() uses and overloads from hotspot
coleenp
parents: 47609
diff changeset
   562
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   563
// Define the class before including platform file, which may specialize
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   564
// the operator definition.  No generic definition of specializations
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   565
// of the operator template are provided, nor are there any generic
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   566
// specializations of the class.  The platform file is responsible for
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   567
// providing those.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   568
template<size_t byte_size>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   569
struct Atomic::PlatformCmpxchg {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   570
  template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   571
  T operator()(T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   572
               T volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   573
               T compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   574
               atomic_memory_order order) const;
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   575
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   576
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   577
// Define the class before including platform file, which may use this
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   578
// as a base class, requiring it be complete.  The definition is later
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   579
// in this file, near the other definitions related to cmpxchg.
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   580
struct Atomic::CmpxchgByteUsingInt {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   581
  template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   582
  T operator()(T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   583
               T volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   584
               T compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   585
               atomic_memory_order order) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   587
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   588
// Define the class before including platform file, which may specialize
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   589
// the operator definition.  No generic definition of specializations
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   590
// of the operator template are provided, nor are there any generic
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   591
// specializations of the class.  The platform file is responsible for
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   592
// providing those.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   593
template<size_t byte_size>
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 49022
diff changeset
   594
struct Atomic::PlatformXchg {
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   595
  template<typename T>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   596
  T operator()(T exchange_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   597
               T volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   598
               atomic_memory_order order) const;
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   599
};
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   600
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   601
template <ScopedFenceType T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   602
class ScopedFenceGeneral: public StackObj {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   603
 public:
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   604
  void prefix() {}
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   605
  void postfix() {}
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   606
};
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   607
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   608
// The following methods can be specialized using simple template specialization
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   609
// in the platform specific files for optimization purposes. Otherwise the
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   610
// generalized variant is used.
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   611
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   612
template<> inline void ScopedFenceGeneral<X_ACQUIRE>::postfix()       { OrderAccess::acquire(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   613
template<> inline void ScopedFenceGeneral<RELEASE_X>::prefix()        { OrderAccess::release(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   614
template<> inline void ScopedFenceGeneral<RELEASE_X_FENCE>::prefix()  { OrderAccess::release(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   615
template<> inline void ScopedFenceGeneral<RELEASE_X_FENCE>::postfix() { OrderAccess::fence();   }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   616
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   617
template <ScopedFenceType T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   618
class ScopedFence : public ScopedFenceGeneral<T> {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   619
  void *const _field;
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   620
 public:
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   621
  ScopedFence(void *const field) : _field(field) { prefix(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   622
  ~ScopedFence() { postfix(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   623
  void prefix() { ScopedFenceGeneral<T>::prefix(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   624
  void postfix() { ScopedFenceGeneral<T>::postfix(); }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   625
};
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   626
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   627
// platform specific in-line definitions - must come before shared definitions
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   628
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   629
#include OS_CPU_HEADER(atomic)
18438
9ea6bbfe0b83 8009575: Reduce Symbol::_refcount from 4 bytes to 2 bytes
iklam
parents: 15803
diff changeset
   630
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   631
// shared in-line definitions
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   632
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   633
// size_t casts...
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   634
#if (SIZE_MAX != UINTPTR_MAX)
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   635
#error size_t is not WORD_SIZE, interesting platform, but missing implementation here
18438
9ea6bbfe0b83 8009575: Reduce Symbol::_refcount from 4 bytes to 2 bytes
iklam
parents: 15803
diff changeset
   636
#endif
9ea6bbfe0b83 8009575: Reduce Symbol::_refcount from 4 bytes to 2 bytes
iklam
parents: 15803
diff changeset
   637
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   638
template<typename T>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   639
inline T Atomic::load(const volatile T* dest) {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   640
  return LoadImpl<T, PlatformLoad<sizeof(T)> >()(dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   641
}
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   642
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   643
template<size_t byte_size, ScopedFenceType type>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   644
struct Atomic::PlatformOrderedLoad {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   645
  template <typename T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   646
  T operator()(const volatile T* p) const {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   647
    ScopedFence<type> f((void*)p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   648
    return Atomic::load(p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   649
  }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   650
};
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   651
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   652
template <typename T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   653
inline T Atomic::load_acquire(const volatile T* p) {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   654
  return LoadImpl<T, PlatformOrderedLoad<sizeof(T), X_ACQUIRE> >()(p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   655
}
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   656
47593
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   657
template<typename T, typename D>
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   658
inline void Atomic::store(T store_value, volatile D* dest) {
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   659
  StoreImpl<T, D, PlatformStore<sizeof(D)> >()(store_value, dest);
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   660
}
2d56326b98f0 8188224: Generalize Atomic::load/store to use templates
eosterlund
parents: 47578
diff changeset
   661
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   662
template<size_t byte_size, ScopedFenceType type>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   663
struct Atomic::PlatformOrderedStore {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   664
  template <typename T>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   665
  void operator()(T v, volatile T* p) const {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   666
    ScopedFence<type> f((void*)p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   667
    Atomic::store(v, p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   668
  }
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   669
};
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   670
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   671
template <typename T, typename D>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   672
inline void Atomic::release_store(volatile D* p, T v) {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   673
  StoreImpl<T, D, PlatformOrderedStore<sizeof(D), RELEASE_X> >()(v, p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   674
}
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   675
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   676
template <typename T, typename D>
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   677
inline void Atomic::release_store_fence(volatile D* p, T v) {
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   678
  StoreImpl<T, D, PlatformOrderedStore<sizeof(D), RELEASE_X_FENCE> >()(v, p);
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   679
}
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 53244
diff changeset
   680
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   681
template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   682
inline D Atomic::add(I add_value, D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   683
                     atomic_memory_order order) {
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   684
  return AddImpl<I, D>()(add_value, dest, order);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   685
}
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   686
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   687
template<typename I, typename D>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   688
struct Atomic::AddImpl<
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   689
  I, D,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   690
  typename EnableIf<IsIntegral<I>::value &&
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   691
                    IsIntegral<D>::value &&
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   692
                    (sizeof(I) <= sizeof(D)) &&
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   693
                    (IsSigned<I>::value == IsSigned<D>::value)>::type>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   694
{
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   695
  D operator()(I add_value, D volatile* dest, atomic_memory_order order) const {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   696
    D addend = add_value;
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   697
    return PlatformAdd<sizeof(D)>()(addend, dest, order);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   698
  }
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   699
};
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   700
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   701
template<typename I, typename P>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   702
struct Atomic::AddImpl<
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   703
  I, P*,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   704
  typename EnableIf<IsIntegral<I>::value && (sizeof(I) <= sizeof(P*))>::type>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   705
{
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   706
  P* operator()(I add_value, P* volatile* dest, atomic_memory_order order) const {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   707
    STATIC_ASSERT(sizeof(intptr_t) == sizeof(P*));
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   708
    STATIC_ASSERT(sizeof(uintptr_t) == sizeof(P*));
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   709
    typedef typename Conditional<IsSigned<I>::value,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   710
                                 intptr_t,
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   711
                                 uintptr_t>::type CI;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   712
    CI addend = add_value;
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   713
    return PlatformAdd<sizeof(P*)>()(addend, dest, order);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   714
  }
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   715
};
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   716
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   717
template<typename Derived>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   718
template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   719
inline D Atomic::FetchAndAdd<Derived>::operator()(I add_value, D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   720
                                                  atomic_memory_order order) const {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   721
  I addend = add_value;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   722
  // If D is a pointer type P*, scale by sizeof(P).
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   723
  if (IsPointer<D>::value) {
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   724
    addend *= sizeof(typename RemovePointer<D>::type);
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   725
  }
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   726
  D old = static_cast<const Derived*>(this)->fetch_and_add(addend, dest, order);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   727
  return old + add_value;
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   728
}
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   729
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   730
template<typename Derived>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   731
template<typename I, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   732
inline D Atomic::AddAndFetch<Derived>::operator()(I add_value, D volatile* dest,
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   733
                                                  atomic_memory_order order) const {
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   734
  // If D is a pointer type P*, scale by sizeof(P).
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   735
  if (IsPointer<D>::value) {
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   736
    add_value *= sizeof(typename RemovePointer<D>::type);
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   737
  }
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   738
  return static_cast<const Derived*>(this)->add_and_fetch(add_value, dest, order);
46993
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   739
}
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   740
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   741
template<typename Type, typename Fn, typename I, typename D>
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   742
inline D Atomic::add_using_helper(Fn fn, I add_value, D volatile* dest) {
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   743
  return PrimitiveConversions::cast<D>(
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   744
    fn(PrimitiveConversions::cast<Type>(add_value),
dd0f91c85ffc 8186476: Generalize Atomic::add with templates
eosterlund
parents: 46958
diff changeset
   745
       reinterpret_cast<Type volatile*>(dest)));
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   746
}
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   747
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   748
template<typename T, typename D, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   749
inline D Atomic::cmpxchg(T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   750
                         D volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   751
                         U compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   752
                         atomic_memory_order order) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   753
  return CmpxchgImpl<T, D, U>()(exchange_value, dest, compare_value, order);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   754
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   755
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   756
template<typename T, typename D>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   757
inline bool Atomic::replace_if_null(T* value, D* volatile* dest,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   758
                                    atomic_memory_order order) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   759
  // Presently using a trivial implementation in terms of cmpxchg.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   760
  // Consider adding platform support, to permit the use of compiler
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   761
  // intrinsics like gcc's __sync_bool_compare_and_swap.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   762
  D* expected_null = NULL;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   763
  return expected_null == cmpxchg(value, dest, expected_null, order);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   764
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   765
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   766
// Handle cmpxchg for integral and enum types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   767
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   768
// All the involved types must be identical.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   769
template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   770
struct Atomic::CmpxchgImpl<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   771
  T, T, T,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   772
  typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   773
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   774
  T operator()(T exchange_value, T volatile* dest, T compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   775
               atomic_memory_order order) const {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   776
    // Forward to the platform handler for the size of T.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   777
    return PlatformCmpxchg<sizeof(T)>()(exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   778
                                        dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   779
                                        compare_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   780
                                        order);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   781
  }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   782
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   783
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   784
// Handle cmpxchg for pointer types.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   785
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   786
// The destination's type and the compare_value type must be the same,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   787
// ignoring cv-qualifiers; we don't care about the cv-qualifiers of
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   788
// the compare_value.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   789
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   790
// The exchange_value must be implicitly convertible to the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   791
// destination's type; it must be type-correct to store the
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   792
// exchange_value in the destination.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   793
template<typename T, typename D, typename U>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   794
struct Atomic::CmpxchgImpl<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   795
  T*, D*, U*,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   796
  typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value &&
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   797
                    IsSame<typename RemoveCV<D>::type,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   798
                           typename RemoveCV<U>::type>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   799
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   800
  D* operator()(T* exchange_value, D* volatile* dest, U* compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   801
               atomic_memory_order order) const {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   802
    // Allow derived to base conversion, and adding cv-qualifiers.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   803
    D* new_value = exchange_value;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   804
    // Don't care what the CV qualifiers for compare_value are,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   805
    // but we need to match D* when calling platform support.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   806
    D* old_value = const_cast<D*>(compare_value);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   807
    return PlatformCmpxchg<sizeof(D*)>()(new_value, dest, old_value, order);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   808
  }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   809
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   810
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   811
// Handle cmpxchg for types that have a translator.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   812
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   813
// All the involved types must be identical.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   814
//
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   815
// This translates the original call into a call on the decayed
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   816
// arguments, and returns the recovered result of that translated
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   817
// call.
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   818
template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   819
struct Atomic::CmpxchgImpl<
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   820
  T, T, T,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   821
  typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   822
{
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   823
  T operator()(T exchange_value, T volatile* dest, T compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   824
               atomic_memory_order order) const {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   825
    typedef PrimitiveConversions::Translate<T> Translator;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   826
    typedef typename Translator::Decayed Decayed;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   827
    STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   828
    return Translator::recover(
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   829
      cmpxchg(Translator::decay(exchange_value),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   830
              reinterpret_cast<Decayed volatile*>(dest),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   831
              Translator::decay(compare_value),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   832
              order));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   833
  }
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   834
};
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   835
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   836
template<typename Type, typename Fn, typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   837
inline T Atomic::cmpxchg_using_helper(Fn fn,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   838
                                      T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   839
                                      T volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   840
                                      T compare_value) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   841
  STATIC_ASSERT(sizeof(Type) == sizeof(T));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   842
  return PrimitiveConversions::cast<T>(
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   843
    fn(PrimitiveConversions::cast<Type>(exchange_value),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   844
       reinterpret_cast<Type volatile*>(dest),
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   845
       PrimitiveConversions::cast<Type>(compare_value)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   846
}
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   847
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   848
template<typename T>
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   849
inline T Atomic::CmpxchgByteUsingInt::operator()(T exchange_value,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   850
                                                 T volatile* dest,
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   851
                                                 T compare_value,
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   852
                                                 atomic_memory_order order) const {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   853
  STATIC_ASSERT(sizeof(T) == sizeof(uint8_t));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   854
  uint8_t canon_exchange_value = exchange_value;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   855
  uint8_t canon_compare_value = compare_value;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   856
  volatile uint32_t* aligned_dest
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   857
    = reinterpret_cast<volatile uint32_t*>(align_down(dest, sizeof(uint32_t)));
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   858
  size_t offset = pointer_delta(dest, aligned_dest, 1);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   859
  uint32_t cur = *aligned_dest;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   860
  uint8_t* cur_as_bytes = reinterpret_cast<uint8_t*>(&cur);
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   861
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   862
  // current value may not be what we are looking for, so force it
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   863
  // to that value so the initial cmpxchg will fail if it is different
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   864
  cur_as_bytes[offset] = canon_compare_value;
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   865
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   866
  // always execute a real cmpxchg so that we get the required memory
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   867
  // barriers even on initial failure
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   868
  do {
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   869
    // value to swap in matches current value ...
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   870
    uint32_t new_value = cur;
48468
7cc7de9bf4a4 8186903: Remove j-types from Atomic
coleenp
parents: 47634
diff changeset
   871
    // ... except for the one byte we want to update
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   872
    reinterpret_cast<uint8_t*>(&new_value)[offset] = canon_exchange_value;
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   873
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   874
    uint32_t res = cmpxchg(new_value, aligned_dest, cur, order);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   875
    if (res == cur) break;      // success
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   876
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   877
    // at least one byte in the int changed value, so update
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   878
    // our view of the current int
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   879
    cur = res;
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   880
    // if our byte is still as cur we loop and try again
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   881
  } while (cur_as_bytes[offset] == canon_compare_value);
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   882
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46625
diff changeset
   883
  return PrimitiveConversions::cast<T>(cur_as_bytes[offset]);
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   884
}
40669
252f9d8272af 8157904: Atomic::cmpxchg for jbyte is missing a fence on initial failure
dholmes
parents: 40655
diff changeset
   885
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   886
// Handle xchg for integral and enum types.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   887
//
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   888
// All the involved types must be identical.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   889
template<typename T>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   890
struct Atomic::XchgImpl<
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   891
  T, T,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   892
  typename EnableIf<IsIntegral<T>::value || IsRegisteredEnum<T>::value>::type>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   893
{
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   894
  T operator()(T exchange_value, T volatile* dest, atomic_memory_order order) const {
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   895
    // Forward to the platform handler for the size of T.
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   896
    return PlatformXchg<sizeof(T)>()(exchange_value, dest, order);
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   897
  }
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   898
};
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   899
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   900
// Handle xchg for pointer types.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   901
//
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   902
// The exchange_value must be implicitly convertible to the
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   903
// destination's type; it must be type-correct to store the
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   904
// exchange_value in the destination.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   905
template<typename T, typename D>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   906
struct Atomic::XchgImpl<
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   907
  T*, D*,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   908
  typename EnableIf<Atomic::IsPointerConvertible<T*, D*>::value>::type>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   909
{
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   910
  D* operator()(T* exchange_value, D* volatile* dest, atomic_memory_order order) const {
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   911
    // Allow derived to base conversion, and adding cv-qualifiers.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   912
    D* new_value = exchange_value;
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   913
    return PlatformXchg<sizeof(D*)>()(new_value, dest, order);
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   914
  }
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   915
};
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   916
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   917
// Handle xchg for types that have a translator.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   918
//
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   919
// All the involved types must be identical.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   920
//
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   921
// This translates the original call into a call on the decayed
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   922
// arguments, and returns the recovered result of that translated
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   923
// call.
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   924
template<typename T>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   925
struct Atomic::XchgImpl<
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   926
  T, T,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   927
  typename EnableIf<PrimitiveConversions::Translate<T>::value>::type>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   928
{
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   929
  T operator()(T exchange_value, T volatile* dest, atomic_memory_order order) const {
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   930
    typedef PrimitiveConversions::Translate<T> Translator;
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   931
    typedef typename Translator::Decayed Decayed;
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   932
    STATIC_ASSERT(sizeof(T) == sizeof(Decayed));
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   933
    return Translator::recover(
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   934
      xchg(Translator::decay(exchange_value),
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   935
           reinterpret_cast<Decayed volatile*>(dest),
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   936
           order));
47578
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   937
  }
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   938
};
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   939
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   940
template<typename Type, typename Fn, typename T>
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   941
inline T Atomic::xchg_using_helper(Fn fn,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   942
                                   T exchange_value,
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   943
                                   T volatile* dest) {
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   944
  STATIC_ASSERT(sizeof(Type) == sizeof(T));
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   945
  return PrimitiveConversions::cast<T>(
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   946
    fn(PrimitiveConversions::cast<Type>(exchange_value),
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   947
       reinterpret_cast<Type volatile*>(dest)));
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   948
}
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   949
09c41c4913d9 8187977: Generalize Atomic::xchg to use templates
eosterlund
parents: 47552
diff changeset
   950
template<typename T, typename D>
50029
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   951
inline D Atomic::xchg(T exchange_value, volatile D* dest, atomic_memory_order order) {
ea0a16ba6ac0 8202080: Introduce ordering semantics for Atomic::add and other RMW atomics
mdoerr
parents: 49364
diff changeset
   952
  return XchgImpl<T, D>()(exchange_value, dest, order);
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   953
}
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39404
diff changeset
   954
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 51330
diff changeset
   955
#endif // SHARE_RUNTIME_ATOMIC_HPP