1 /*
   2  * Copyright (c) 1999, 2005, 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,
  20  * CA 94065 USA or visit www.oracle.com if you need additional information or
  21  * have any 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_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  31 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  32 
  33 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  34 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  35 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  36 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  37 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  38 
  39 
  40 // Adding a lock prefix to an instruction on MP machine
  41 #define LOCK_IF_MP(mp) "cmp $0, " #mp "; je 1f; lock; 1: "
  42 
  43 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  44   jint addend = add_value;
  45   int mp = os::is_MP();
  46   __asm__ volatile (  LOCK_IF_MP(%3) "xaddl %0,(%2)"
  47                     : "=r" (addend)
  48                     : "0" (addend), "r" (dest), "r" (mp)
  49                     : "cc", "memory");
  50   return addend + add_value;
  51 }
  52 
  53 inline void Atomic::inc    (volatile jint*     dest) {
  54   int mp = os::is_MP();
  55   __asm__ volatile (LOCK_IF_MP(%1) "addl $1,(%0)" :
  56                     : "r" (dest), "r" (mp) : "cc", "memory");
  57 }
  58 
  59 inline void Atomic::inc_ptr(volatile void*     dest) {
  60   inc_ptr((volatile intptr_t*)dest);
  61 }
  62 
  63 inline void Atomic::dec    (volatile jint*     dest) {
  64   int mp = os::is_MP();
  65   __asm__ volatile (LOCK_IF_MP(%1) "subl $1,(%0)" :
  66                     : "r" (dest), "r" (mp) : "cc", "memory");
  67 }
  68 
  69 inline void Atomic::dec_ptr(volatile void*     dest) {
  70   dec_ptr((volatile intptr_t*)dest);
  71 }
  72 
  73 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
  74   __asm__ volatile (  "xchgl (%2),%0"
  75                     : "=r" (exchange_value)
  76                     : "0" (exchange_value), "r" (dest)
  77                     : "memory");
  78   return exchange_value;
  79 }
  80 
  81 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
  82   return (void*)xchg_ptr((intptr_t)exchange_value, (volatile intptr_t*)dest);
  83 }
  84 
  85 
  86 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
  87   int mp = os::is_MP();
  88   __asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)"
  89                     : "=a" (exchange_value)
  90                     : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
  91                     : "cc", "memory");
  92   return exchange_value;
  93 }
  94 
  95 extern "C" {
  96   // defined in linux_x86.s
  97   jlong _Atomic_cmpxchg_long(jlong, volatile jlong*, jlong, bool);
  98 }
  99 
 100 #ifdef AMD64
 101 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
 102 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
 103 
 104 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 105   intptr_t addend = add_value;
 106   bool mp = os::is_MP();
 107   __asm__ __volatile__ (LOCK_IF_MP(%3) "xaddq %0,(%2)"
 108                         : "=r" (addend)
 109                         : "0" (addend), "r" (dest), "r" (mp)
 110                         : "cc", "memory");
 111   return addend + add_value;
 112 }
 113 
 114 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 115   return (void*)add_ptr(add_value, (volatile intptr_t*)dest);
 116 }
 117 
 118 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
 119   bool mp = os::is_MP();
 120   __asm__ __volatile__ (LOCK_IF_MP(%1) "addq $1,(%0)"
 121                         :
 122                         : "r" (dest), "r" (mp)
 123                         : "cc", "memory");
 124 }
 125 
 126 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
 127   bool mp = os::is_MP();
 128   __asm__ __volatile__ (LOCK_IF_MP(%1) "subq $1,(%0)"
 129                         :
 130                         : "r" (dest), "r" (mp)
 131                         : "cc", "memory");
 132 }
 133 
 134 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 135   __asm__ __volatile__ ("xchgq (%2),%0"
 136                         : "=r" (exchange_value)
 137                         : "0" (exchange_value), "r" (dest)
 138                         : "memory");
 139   return exchange_value;
 140 }
 141 
 142 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 143   bool mp = os::is_MP();
 144   __asm__ __volatile__ (LOCK_IF_MP(%4) "cmpxchgq %1,(%3)"
 145                         : "=a" (exchange_value)
 146                         : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp)
 147                         : "cc", "memory");
 148   return exchange_value;
 149 }
 150 
 151 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 152   return (intptr_t)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 153 }
 154 
 155 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 156   return (void*)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 157 }
 158 
 159 #else
 160 //inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
 161 //inline void Atomic::store  (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
 162 
 163 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 164   return (intptr_t)Atomic::add((jint)add_value, (volatile jint*)dest);
 165 }
 166 
 167 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 168   return (void*)Atomic::add((jint)add_value, (volatile jint*)dest);
 169 }
 170 
 171 
 172 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
 173   inc((volatile jint*)dest);
 174 }
 175 
 176 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
 177   dec((volatile jint*)dest);
 178 }
 179 
 180 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 181   return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
 182 }
 183 
 184 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 185   return _Atomic_cmpxchg_long(exchange_value, dest, compare_value, os::is_MP());
 186 }
 187 
 188 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 189   return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 190 }
 191 
 192 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 193   return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 194 }
 195 #endif // AMD64