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, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // The following alternative implementations are needed because
  26 // Windows 95 doesn't support (some of) the corresponding Windows NT
  27 // calls. Furthermore, these versions allow inlining in the caller.
  28 // (More precisely: The documentation for InterlockedExchange says
  29 // it is supported for Windows 95. However, when single-stepping
  30 // through the assembly code we cannot step into the routine and
  31 // when looking at the routine address we see only garbage code.
  32 // Better safe then sorry!). Was bug 7/31/98 (gri).
  33 //
  34 // Performance note: On uniprocessors, the 'lock' prefixes are not
  35 // necessary (and expensive). We should generate separate cases if
  36 // this becomes a performance problem.
  37 
  38 #pragma warning(disable: 4035) // Disables warnings reporting missing return statement
  39 
  40 inline void Atomic::store    (jbyte    store_value, jbyte*    dest) { *dest = store_value; }
  41 inline void Atomic::store    (jshort   store_value, jshort*   dest) { *dest = store_value; }
  42 inline void Atomic::store    (jint     store_value, jint*     dest) { *dest = store_value; }
  43 
  44 inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; }
  45 inline void Atomic::store_ptr(void*    store_value, void*     dest) { *(void**)dest = store_value; }
  46 
  47 inline void Atomic::store    (jbyte    store_value, volatile jbyte*    dest) { *dest = store_value; }
  48 inline void Atomic::store    (jshort   store_value, volatile jshort*   dest) { *dest = store_value; }
  49 inline void Atomic::store    (jint     store_value, volatile jint*     dest) { *dest = store_value; }
  50 
  51 
  52 inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; }
  53 inline void Atomic::store_ptr(void*    store_value, volatile void*     dest) { *(void* volatile *)dest = store_value; }
  54 
  55 // Adding a lock prefix to an instruction on MP machine
  56 // VC++ doesn't like the lock prefix to be on a single line
  57 // so we can't insert a label after the lock prefix.
  58 // By emitting a lock prefix, we can define a label after it.
  59 #define LOCK_IF_MP(mp) __asm cmp mp, 0  \
  60                        __asm je L0      \
  61                        __asm _emit 0xF0 \
  62                        __asm L0:
  63 
  64 #ifdef AMD64
  65 inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
  66 inline void Atomic::store    (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
  67 
  68 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
  69   return (jint)(*os::atomic_add_func)(add_value, dest);
  70 }
  71 
  72 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
  73   return (intptr_t)(*os::atomic_add_ptr_func)(add_value, dest);
  74 }
  75 
  76 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
  77   return (void*)(*os::atomic_add_ptr_func)(add_value, (volatile intptr_t*)dest);
  78 }
  79 
  80 inline void Atomic::inc    (volatile jint*     dest) {
  81   (void)add    (1, dest);
  82 }
  83 
  84 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
  85   (void)add_ptr(1, dest);
  86 }
  87 
  88 inline void Atomic::inc_ptr(volatile void*     dest) {
  89   (void)add_ptr(1, dest);
  90 }
  91 
  92 inline void Atomic::dec    (volatile jint*     dest) {
  93   (void)add    (-1, dest);
  94 }
  95 
  96 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
  97   (void)add_ptr(-1, dest);
  98 }
  99 
 100 inline void Atomic::dec_ptr(volatile void*     dest) {
 101   (void)add_ptr(-1, dest);
 102 }
 103 
 104 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 105   return (jint)(*os::atomic_xchg_func)(exchange_value, dest);
 106 }
 107 
 108 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 109   return (intptr_t)(os::atomic_xchg_ptr_func)(exchange_value, dest);
 110 }
 111 
 112 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 113   return (void *)(os::atomic_xchg_ptr_func)((intptr_t)exchange_value, (volatile intptr_t*)dest);
 114 }
 115 
 116 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 117   return (*os::atomic_cmpxchg_func)(exchange_value, dest, compare_value);
 118 }
 119 
 120 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 121   return (*os::atomic_cmpxchg_long_func)(exchange_value, dest, compare_value);
 122 }
 123 
 124 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 125   return (intptr_t)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 126 }
 127 
 128 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 129   return (void*)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value);
 130 }
 131 
 132 #else // !AMD64
 133 
 134 //inline void Atomic::store    (jlong    store_value, jlong*    dest) { *dest = store_value; }
 135 //inline void Atomic::store  (jlong    store_value, volatile jlong*    dest) { *dest = store_value; }
 136 inline jint     Atomic::add    (jint     add_value, volatile jint*     dest) {
 137   int mp = os::is_MP();
 138   __asm {
 139     mov edx, dest;
 140     mov eax, add_value;
 141     mov ecx, eax;
 142     LOCK_IF_MP(mp)
 143     xadd dword ptr [edx], eax;
 144     add eax, ecx;
 145   }
 146 }
 147 
 148 inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) {
 149   return (intptr_t)add((jint)add_value, (volatile jint*)dest);
 150 }
 151 
 152 inline void*    Atomic::add_ptr(intptr_t add_value, volatile void*     dest) {
 153   return (void*)add((jint)add_value, (volatile jint*)dest);
 154 }
 155 
 156 inline void Atomic::inc    (volatile jint*     dest) {
 157   // alternative for InterlockedIncrement
 158   int mp = os::is_MP();
 159   __asm {
 160     mov edx, dest;
 161     LOCK_IF_MP(mp)
 162     add dword ptr [edx], 1;
 163   }
 164 }
 165 
 166 inline void Atomic::inc_ptr(volatile intptr_t* dest) {
 167   inc((volatile jint*)dest);
 168 }
 169 
 170 inline void Atomic::inc_ptr(volatile void*     dest) {
 171   inc((volatile jint*)dest);
 172 }
 173 
 174 inline void Atomic::dec    (volatile jint*     dest) {
 175   // alternative for InterlockedDecrement
 176   int mp = os::is_MP();
 177   __asm {
 178     mov edx, dest;
 179     LOCK_IF_MP(mp)
 180     sub dword ptr [edx], 1;
 181   }
 182 }
 183 
 184 inline void Atomic::dec_ptr(volatile intptr_t* dest) {
 185   dec((volatile jint*)dest);
 186 }
 187 
 188 inline void Atomic::dec_ptr(volatile void*     dest) {
 189   dec((volatile jint*)dest);
 190 }
 191 
 192 inline jint     Atomic::xchg    (jint     exchange_value, volatile jint*     dest) {
 193   // alternative for InterlockedExchange
 194   __asm {
 195     mov eax, exchange_value;
 196     mov ecx, dest;
 197     xchg eax, dword ptr [ecx];
 198   }
 199 }
 200 
 201 inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) {
 202   return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest);
 203 }
 204 
 205 inline void*    Atomic::xchg_ptr(void*    exchange_value, volatile void*     dest) {
 206   return (void*)xchg((jint)exchange_value, (volatile jint*)dest);
 207 }
 208 
 209 inline jint     Atomic::cmpxchg    (jint     exchange_value, volatile jint*     dest, jint     compare_value) {
 210   // alternative for InterlockedCompareExchange
 211   int mp = os::is_MP();
 212   __asm {
 213     mov edx, dest
 214     mov ecx, exchange_value
 215     mov eax, compare_value
 216     LOCK_IF_MP(mp)
 217     cmpxchg dword ptr [edx], ecx
 218   }
 219 }
 220 
 221 inline jlong    Atomic::cmpxchg    (jlong    exchange_value, volatile jlong*    dest, jlong    compare_value) {
 222   int mp = os::is_MP();
 223   jint ex_lo  = (jint)exchange_value;
 224   jint ex_hi  = *( ((jint*)&exchange_value) + 1 );
 225   jint cmp_lo = (jint)compare_value;
 226   jint cmp_hi = *( ((jint*)&compare_value) + 1 );
 227   __asm {
 228     push ebx
 229     push edi
 230     mov eax, cmp_lo
 231     mov edx, cmp_hi
 232     mov edi, dest
 233     mov ebx, ex_lo
 234     mov ecx, ex_hi
 235     LOCK_IF_MP(mp)
 236     cmpxchg8b qword ptr [edi]
 237     pop edi
 238     pop ebx
 239   }
 240 }
 241 
 242 inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) {
 243   return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 244 }
 245 
 246 inline void*    Atomic::cmpxchg_ptr(void*    exchange_value, volatile void*     dest, void*    compare_value) {
 247   return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value);
 248 }
 249 #endif // AMD64
 250 
 251 #pragma warning(default: 4035) // Enables warnings reporting missing return statement