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 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 107   intptr_t rv = exchange_value;
 108   __asm__ volatile(
 109     " swap   [%2],%1\n\t"
 110     : "=r" (rv)
 111     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 112     : "memory");
 113   return rv;
 114 }
 115 
 116 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 117   intptr_t rv = exchange_value;
 118   __asm__ volatile(
 119     "1:\n\t"
 120     " mov    %1, %%o3\n\t"
 121     " ldx    [%2], %%o2\n\t"
 122     " casx   [%2], %%o2, %%o3\n\t"
 123     " cmp    %%o2, %%o3\n\t"
 124     " bne    %%xcc, 1b\n\t"
 125     "  nop\n\t"
 126     " mov    %%o2, %0\n\t"
 127     : "=r" (rv)
 128     : "r" (exchange_value), "r" (dest)
 129     : "memory", "o2", "o3");
 130   return rv;
 131 }
 132 
 133 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 134   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 135 }
 136 
 137 // No direct support for cmpxchg of bytes; emulate using int.
 138 template<>
 139 struct Atomic::PlatformCmpxchg<1> : Atomic::CmpxchgByteUsingInt {};
 140 
 141 template<>
 142 template<typename T>
 143 inline T Atomic::PlatformCmpxchg<4>::operator()(T exchange_value,
 144                                                 T volatile* dest,
 145                                                 T compare_value,
 146                                                 cmpxchg_memory_order order) const {
 147   STATIC_ASSERT(4 == sizeof(T));
 148   T rv;
 149   __asm__ volatile(
 150     " cas    [%2], %3, %0"
 151     : "=r" (rv)
 152     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 153     : "memory");
 154   return rv;
 155 }
 156 
 157 template<>
 158 template<typename T>
 159 inline T Atomic::PlatformCmpxchg<8>::operator()(T exchange_value,
 160                                                 T volatile* dest,
 161                                                 T compare_value,
 162                                                 cmpxchg_memory_order order) const {
 163   STATIC_ASSERT(8 == sizeof(T));
 164   T rv;
 165   __asm__ volatile(
 166     " casx   [%2], %3, %0"
 167     : "=r" (rv)
 168     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 169     : "memory");
 170   return rv;
 171 }
 172 
 173 #endif // OS_CPU_LINUX_SPARC_VM_ATOMIC_LINUX_SPARC_INLINE_HPP