1 /*
   2  * Copyright (c) 1999, 2008, 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 // Implementation of class atomic
  26 
  27 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  28 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  29 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  30 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  31 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  32 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  33 
  34 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  35 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  36 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  37 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  38 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  39 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  40 
  41 inline void Atomic::inc    (volatile jint*     dest) { (void)add    (1, dest); }
  42 inline void Atomic::inc_ptr(volatile intptr_t* dest) { (void)add_ptr(1, dest); }
  43 inline void Atomic::inc_ptr(volatile void*     dest) { (void)add_ptr(1, dest); }
  44 
  45 inline void Atomic::dec    (volatile jint*     dest) { (void)add    (-1, dest); }
  46 inline void Atomic::dec_ptr(volatile intptr_t* dest) { (void)add_ptr(-1, dest); }
  47 inline void Atomic::dec_ptr(volatile void*     dest) { (void)add_ptr(-1, dest); }
  48 
  49 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  50   intptr_t rv;
  51   __asm__ volatile(
  52     "1: \n\t"
  53     " ld     [%2], %%o2\n\t"
  54     " add    %1, %%o2, %%o3\n\t"
  55     " cas    [%2], %%o2, %%o3\n\t"
  56     " cmp    %%o2, %%o3\n\t"
  57     " bne    1b\n\t"
  58     "  nop\n\t"
  59     " add    %1, %%o2, %0\n\t"
  60     : "=r" (rv)
  61     : "r" (add_value), "r" (dest)
  62     : "memory", "o2", "o3");
  63   return rv;
  64 }
  65 
  66 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  67   intptr_t rv;
  68 #ifdef _LP64
  69   __asm__ volatile(
  70     "1: \n\t"
  71     " ldx    [%2], %%o2\n\t"
  72     " add    %0, %%o2, %%o3\n\t"
  73     " casx   [%2], %%o2, %%o3\n\t"
  74     " cmp    %%o2, %%o3\n\t"
  75     " bne    %%xcc, 1b\n\t"
  76     "  nop\n\t"
  77     " add    %0, %%o2, %0\n\t"
  78     : "=r" (rv)
  79     : "r" (add_value), "r" (dest)
  80     : "memory", "o2", "o3");
  81 #else
  82   __asm__ volatile(
  83     "1: \n\t"
  84     " ld     [%2], %%o2\n\t"
  85     " add    %1, %%o2, %%o3\n\t"
  86     " cas    [%2], %%o2, %%o3\n\t"
  87     " cmp    %%o2, %%o3\n\t"
  88     " bne    1b\n\t"
  89     "  nop\n\t"
  90     " add    %1, %%o2, %0\n\t"
  91     : "=r" (rv)
  92     : "r" (add_value), "r" (dest)
  93     : "memory", "o2", "o3");
  94 #endif // _LP64
  95   return rv;
  96 }
  97 
  98 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
  99   return (void*)add_ptr((intptr_t)add_value, (volatile intptr_t*)dest);
 100 }
 101 
 102 
 103 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 104   intptr_t rv = exchange_value;
 105   __asm__ volatile(
 106     " swap   [%2],%1\n\t"
 107     : "=r" (rv)
 108     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 109     : "memory");
 110   return rv;
 111 }
 112 
 113 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 114   intptr_t rv = exchange_value;
 115 #ifdef _LP64
 116   __asm__ volatile(
 117     "1:\n\t"
 118     " mov    %1, %%o3\n\t"
 119     " ldx    [%2], %%o2\n\t"
 120     " casx   [%2], %%o2, %%o3\n\t"
 121     " cmp    %%o2, %%o3\n\t"
 122     " bne    %%xcc, 1b\n\t"
 123     "  nop\n\t"
 124     " mov    %%o2, %0\n\t"
 125     : "=r" (rv)
 126     : "r" (exchange_value), "r" (dest)
 127     : "memory", "o2", "o3");
 128 #else
 129   __asm__ volatile(
 130     "swap    [%2],%1\n\t"
 131     : "=r" (rv)
 132     : "0" (exchange_value) /* we use same register as for return value */, "r" (dest)
 133     : "memory");
 134 #endif // _LP64
 135   return rv;
 136 }
 137 
 138 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 139   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
 140 }
 141 
 142 
 143 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 144   jint rv;
 145   __asm__ volatile(
 146     " cas    [%2], %3, %0"
 147     : "=r" (rv)
 148     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 149     : "memory");
 150   return rv;
 151 }
 152 
 153 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 154 #ifdef _LP64
 155   jlong rv;
 156   __asm__ volatile(
 157     " casx   [%2], %3, %0"
 158     : "=r" (rv)
 159     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 160     : "memory");
 161   return rv;
 162 #else
 163   assert(VM_Version::v9_instructions_work(), "cas only supported on v9");
 164   volatile jlong_accessor evl, cvl, rv;
 165   evl.long_value = exchange_value;
 166   cvl.long_value = compare_value;
 167 
 168   __asm__ volatile(
 169     " sllx   %2, 32, %2\n\t"
 170     " srl    %3, 0,  %3\n\t"
 171     " or     %2, %3, %2\n\t"
 172     " sllx   %5, 32, %5\n\t"
 173     " srl    %6, 0,  %6\n\t"
 174     " or     %5, %6, %5\n\t"
 175     " casx   [%4], %5, %2\n\t"
 176     " srl    %2, 0, %1\n\t"
 177     " srlx   %2, 32, %0\n\t"
 178     : "=r" (rv.words[0]), "=r" (rv.words[1])
 179     : "r"  (evl.words[0]), "r" (evl.words[1]), "r" (dest), "r" (cvl.words[0]), "r" (cvl.words[1])
 180     : "memory");
 181 
 182   return rv.long_value;
 183 #endif
 184 }
 185 
 186 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 187   intptr_t rv;
 188 #ifdef _LP64
 189   __asm__ volatile(
 190     " casx    [%2], %3, %0"
 191     : "=r" (rv)
 192     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 193     : "memory");
 194 #else
 195   __asm__ volatile(
 196     " cas     [%2], %3, %0"
 197     : "=r" (rv)
 198     : "0" (exchange_value), "r" (dest), "r" (compare_value)
 199     : "memory");
 200 #endif // _LP64
 201   return rv;
 202 }
 203 
 204 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 205   return (void*)cmpxchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest, (intptr_t)compare_value);
 206 }