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