1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
  26 #define OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP
  27 
  28 // Implementation of class atomic
  29 
  30 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  31 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  32 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  33 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  34 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  35 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  36 
  37 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  38 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  39 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  40 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  41 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  42 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  43 
  44 inline void Atomic::inc    (volatile jint*     dest) { (void)add    (1, dest); }
  45 inline void Atomic::inc_ptr(volatile intptr_t* dest) { (void)add_ptr(1, dest); }
  46 inline void Atomic::inc_ptr(volatile void*     dest) { (void)add_ptr(1, dest); }
  47 
  48 inline void Atomic::dec    (volatile jint*     dest) { (void)add    (-1, dest); }
  49 inline void Atomic::dec_ptr(volatile intptr_t* dest) { (void)add_ptr(-1, dest); }
  50 inline void Atomic::dec_ptr(volatile void*     dest) { (void)add_ptr(-1, dest); }
  51 
  52 inline jlong Atomic::load(const volatile jlong* src) { return *src; }
  53 
  54 template<size_t byte_size>
  55 struct Atomic::PlatformAdd
  56   : Atomic::AddAndFetch<Atomic::PlatformAdd<byte_size> >
  57 {
  58   template<typename I, typename D>
  59   D add_and_fetch(I add_value, D volatile* dest) const;
  60 };
  61 
  62 template<>
  63 template<typename I, typename D>
  64 inline D Atomic::PlatformAdd<4>::add_and_fetch(I add_value, D volatile* dest) const {
  65   STATIC_CAST(4 == sizeof(I));
  66   STATIC_CAST(4 == sizeof(D));
  67 
  68   D rv;
  69   __asm__ volatile(
  70     "1: \n\t"
  71     " ld     [%2], %%o2\n\t"
  72     " add    %1, %%o2, %%o3\n\t"
  73     " cas    [%2], %%o2, %%o3\n\t"
  74     " cmp    %%o2, %%o3\n\t"
  75     " bne    1b\n\t"
  76     "  nop\n\t"
  77     " add    %1, %%o2, %0\n\t"
  78     : "=r" (rv)
  79     : "r" (add_value), "r" (dest)
  80     : "memory", "o2", "o3");
  81   return rv;
  82 }
  83 
  84 template<typename I, typename D>
  85 inline D Atomic::PlatformAdd<8>::add_and_fetch(I add_value, D volatile* dest) const {
  86   STATIC_CAST(8 == sizeof(I));
  87   STATIC_CAST(8 == sizeof(D));
  88 
  89   D rv;
  90   __asm__ volatile(
  91     "1: \n\t"
  92     " ldx    [%2], %%o2\n\t"
  93     " add    %1, %%o2, %%o3\n\t"
  94     " casx   [%2], %%o2, %%o3\n\t"
  95     " cmp    %%o2, %%o3\n\t"
  96     " bne    %%xcc, 1b\n\t"
  97     "  nop\n\t"
  98     " add    %1, %%o2, %0\n\t"
  99     : "=r" (rv)
 100     : "r" (add_value), "r" (dest)
 101     : "memory", "o2", "o3");
 102   return rv;
 103 }
 104 
 105 
 106 template<>
 107 struct Atomic::PlatformAdd<2>: Atomic::AddShortUsingInt {};
 108 
 109 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 110   intptr_t rv = exchange_value;
 111   __asm__ volatile(
 112     " swap   [%2],%1\n\t"
 113     : "=r" (rv)
 114     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 115     : "memory");
 116   return rv;
 117 }
 118 
 119 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 120   intptr_t rv = exchange_value;
 121   __asm__ volatile(
 122     "1:\n\t"
 123     " mov    %1, %%o3\n\t"
 124     " ldx    [%2], %%o2\n\t"
 125     " casx   [%2], %%o2, %%o3\n\t"
 126     " cmp    %%o2, %%o3\n\t"
 127     " bne    %%xcc, 1b\n\t"
 128     "  nop\n\t"
 129     " mov    %%o2, %0\n\t"
 130     : "=r" (rv)
 131     : "r" (exchange_value), "r" (dest)
 132     : "memory", "o2", "o3");
 133   return rv;
 134 }
 135 
 136 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 137   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 138 }
 139 
 140 // No direct support for cmpxchg of bytes; emulate using int.
 141 template<>
 142 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
 143 
 144 template<>
 145 template<typename T>
 146 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 147                                                 T volatile* dest,
 148                                                 T compare_value,
 149                                                 cmpxchg_memory_order order) const {
 150   STATIC_ASSERT(4 == sizeof(T));
 151   T rv;
 152   __asm__ volatile(
 153     " cas    [%2], %3, %0"
 154     : "=r" (rv)
 155     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 156     : "memory");
 157   return rv;
 158 }
 159 
 160 template<>
 161 template<typename T>
 162 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 163                                                 T volatile* dest,
 164                                                 T compare_value,
 165                                                 cmpxchg_memory_order order) const {
 166   STATIC_ASSERT(8 == sizeof(T));
 167   T rv;
 168   __asm__ volatile(
 169     " casx   [%2], %3, %0"
 170     : "=r" (rv)
 171     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 172     : "memory");
 173   return rv;
 174 }
 175 
 176 #endif // OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP