1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os_cpu/windows_x86/vm/atomic_windows_x86.inline.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,287 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_INLINE_HPP 1.29 +#define OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_INLINE_HPP 1.30 + 1.31 +#include "runtime/atomic.hpp" 1.32 +#include "runtime/os.hpp" 1.33 +#include "vm_version_x86.hpp" 1.34 + 1.35 +// The following alternative implementations are needed because 1.36 +// Windows 95 doesn't support (some of) the corresponding Windows NT 1.37 +// calls. Furthermore, these versions allow inlining in the caller. 1.38 +// (More precisely: The documentation for InterlockedExchange says 1.39 +// it is supported for Windows 95. However, when single-stepping 1.40 +// through the assembly code we cannot step into the routine and 1.41 +// when looking at the routine address we see only garbage code. 1.42 +// Better safe then sorry!). Was bug 7/31/98 (gri). 1.43 +// 1.44 +// Performance note: On uniprocessors, the 'lock' prefixes are not 1.45 +// necessary (and expensive). We should generate separate cases if 1.46 +// this becomes a performance problem. 1.47 + 1.48 +#pragma warning(disable: 4035) // Disables warnings reporting missing return statement 1.49 + 1.50 +inline void Atomic::store (jbyte store_value, jbyte* dest) { *dest = store_value; } 1.51 +inline void Atomic::store (jshort store_value, jshort* dest) { *dest = store_value; } 1.52 +inline void Atomic::store (jint store_value, jint* dest) { *dest = store_value; } 1.53 + 1.54 +inline void Atomic::store_ptr(intptr_t store_value, intptr_t* dest) { *dest = store_value; } 1.55 +inline void Atomic::store_ptr(void* store_value, void* dest) { *(void**)dest = store_value; } 1.56 + 1.57 +inline void Atomic::store (jbyte store_value, volatile jbyte* dest) { *dest = store_value; } 1.58 +inline void Atomic::store (jshort store_value, volatile jshort* dest) { *dest = store_value; } 1.59 +inline void Atomic::store (jint store_value, volatile jint* dest) { *dest = store_value; } 1.60 + 1.61 + 1.62 +inline void Atomic::store_ptr(intptr_t store_value, volatile intptr_t* dest) { *dest = store_value; } 1.63 +inline void Atomic::store_ptr(void* store_value, volatile void* dest) { *(void* volatile *)dest = store_value; } 1.64 + 1.65 +// Adding a lock prefix to an instruction on MP machine 1.66 +// VC++ doesn't like the lock prefix to be on a single line 1.67 +// so we can't insert a label after the lock prefix. 1.68 +// By emitting a lock prefix, we can define a label after it. 1.69 +#define LOCK_IF_MP(mp) __asm cmp mp, 0 \ 1.70 + __asm je L0 \ 1.71 + __asm _emit 0xF0 \ 1.72 + __asm L0: 1.73 + 1.74 +#ifdef AMD64 1.75 +inline void Atomic::store (jlong store_value, jlong* dest) { *dest = store_value; } 1.76 +inline void Atomic::store (jlong store_value, volatile jlong* dest) { *dest = store_value; } 1.77 + 1.78 +inline jint Atomic::add (jint add_value, volatile jint* dest) { 1.79 + return (jint)(*os::atomic_add_func)(add_value, dest); 1.80 +} 1.81 + 1.82 +inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) { 1.83 + return (intptr_t)(*os::atomic_add_ptr_func)(add_value, dest); 1.84 +} 1.85 + 1.86 +inline void* Atomic::add_ptr(intptr_t add_value, volatile void* dest) { 1.87 + return (void*)(*os::atomic_add_ptr_func)(add_value, (volatile intptr_t*)dest); 1.88 +} 1.89 + 1.90 +inline void Atomic::inc (volatile jint* dest) { 1.91 + (void)add (1, dest); 1.92 +} 1.93 + 1.94 +inline void Atomic::inc_ptr(volatile intptr_t* dest) { 1.95 + (void)add_ptr(1, dest); 1.96 +} 1.97 + 1.98 +inline void Atomic::inc_ptr(volatile void* dest) { 1.99 + (void)add_ptr(1, dest); 1.100 +} 1.101 + 1.102 +inline void Atomic::dec (volatile jint* dest) { 1.103 + (void)add (-1, dest); 1.104 +} 1.105 + 1.106 +inline void Atomic::dec_ptr(volatile intptr_t* dest) { 1.107 + (void)add_ptr(-1, dest); 1.108 +} 1.109 + 1.110 +inline void Atomic::dec_ptr(volatile void* dest) { 1.111 + (void)add_ptr(-1, dest); 1.112 +} 1.113 + 1.114 +inline jint Atomic::xchg (jint exchange_value, volatile jint* dest) { 1.115 + return (jint)(*os::atomic_xchg_func)(exchange_value, dest); 1.116 +} 1.117 + 1.118 +inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) { 1.119 + return (intptr_t)(os::atomic_xchg_ptr_func)(exchange_value, dest); 1.120 +} 1.121 + 1.122 +inline void* Atomic::xchg_ptr(void* exchange_value, volatile void* dest) { 1.123 + return (void *)(os::atomic_xchg_ptr_func)((intptr_t)exchange_value, (volatile intptr_t*)dest); 1.124 +} 1.125 + 1.126 +inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { 1.127 + return (*os::atomic_cmpxchg_func)(exchange_value, dest, compare_value); 1.128 +} 1.129 + 1.130 +inline jlong Atomic::cmpxchg (jlong exchange_value, volatile jlong* dest, jlong compare_value) { 1.131 + return (*os::atomic_cmpxchg_long_func)(exchange_value, dest, compare_value); 1.132 +} 1.133 + 1.134 +inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) { 1.135 + return (intptr_t)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value); 1.136 +} 1.137 + 1.138 +inline void* Atomic::cmpxchg_ptr(void* exchange_value, volatile void* dest, void* compare_value) { 1.139 + return (void*)cmpxchg((jlong)exchange_value, (volatile jlong*)dest, (jlong)compare_value); 1.140 +} 1.141 + 1.142 +inline jlong Atomic::load(volatile jlong* src) { return *src; } 1.143 + 1.144 +#else // !AMD64 1.145 + 1.146 +inline jint Atomic::add (jint add_value, volatile jint* dest) { 1.147 + int mp = os::is_MP(); 1.148 + __asm { 1.149 + mov edx, dest; 1.150 + mov eax, add_value; 1.151 + mov ecx, eax; 1.152 + LOCK_IF_MP(mp) 1.153 + xadd dword ptr [edx], eax; 1.154 + add eax, ecx; 1.155 + } 1.156 +} 1.157 + 1.158 +inline intptr_t Atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest) { 1.159 + return (intptr_t)add((jint)add_value, (volatile jint*)dest); 1.160 +} 1.161 + 1.162 +inline void* Atomic::add_ptr(intptr_t add_value, volatile void* dest) { 1.163 + return (void*)add((jint)add_value, (volatile jint*)dest); 1.164 +} 1.165 + 1.166 +inline void Atomic::inc (volatile jint* dest) { 1.167 + // alternative for InterlockedIncrement 1.168 + int mp = os::is_MP(); 1.169 + __asm { 1.170 + mov edx, dest; 1.171 + LOCK_IF_MP(mp) 1.172 + add dword ptr [edx], 1; 1.173 + } 1.174 +} 1.175 + 1.176 +inline void Atomic::inc_ptr(volatile intptr_t* dest) { 1.177 + inc((volatile jint*)dest); 1.178 +} 1.179 + 1.180 +inline void Atomic::inc_ptr(volatile void* dest) { 1.181 + inc((volatile jint*)dest); 1.182 +} 1.183 + 1.184 +inline void Atomic::dec (volatile jint* dest) { 1.185 + // alternative for InterlockedDecrement 1.186 + int mp = os::is_MP(); 1.187 + __asm { 1.188 + mov edx, dest; 1.189 + LOCK_IF_MP(mp) 1.190 + sub dword ptr [edx], 1; 1.191 + } 1.192 +} 1.193 + 1.194 +inline void Atomic::dec_ptr(volatile intptr_t* dest) { 1.195 + dec((volatile jint*)dest); 1.196 +} 1.197 + 1.198 +inline void Atomic::dec_ptr(volatile void* dest) { 1.199 + dec((volatile jint*)dest); 1.200 +} 1.201 + 1.202 +inline jint Atomic::xchg (jint exchange_value, volatile jint* dest) { 1.203 + // alternative for InterlockedExchange 1.204 + __asm { 1.205 + mov eax, exchange_value; 1.206 + mov ecx, dest; 1.207 + xchg eax, dword ptr [ecx]; 1.208 + } 1.209 +} 1.210 + 1.211 +inline intptr_t Atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest) { 1.212 + return (intptr_t)xchg((jint)exchange_value, (volatile jint*)dest); 1.213 +} 1.214 + 1.215 +inline void* Atomic::xchg_ptr(void* exchange_value, volatile void* dest) { 1.216 + return (void*)xchg((jint)exchange_value, (volatile jint*)dest); 1.217 +} 1.218 + 1.219 +inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { 1.220 + // alternative for InterlockedCompareExchange 1.221 + int mp = os::is_MP(); 1.222 + __asm { 1.223 + mov edx, dest 1.224 + mov ecx, exchange_value 1.225 + mov eax, compare_value 1.226 + LOCK_IF_MP(mp) 1.227 + cmpxchg dword ptr [edx], ecx 1.228 + } 1.229 +} 1.230 + 1.231 +inline jlong Atomic::cmpxchg (jlong exchange_value, volatile jlong* dest, jlong compare_value) { 1.232 + int mp = os::is_MP(); 1.233 + jint ex_lo = (jint)exchange_value; 1.234 + jint ex_hi = *( ((jint*)&exchange_value) + 1 ); 1.235 + jint cmp_lo = (jint)compare_value; 1.236 + jint cmp_hi = *( ((jint*)&compare_value) + 1 ); 1.237 + __asm { 1.238 + push ebx 1.239 + push edi 1.240 + mov eax, cmp_lo 1.241 + mov edx, cmp_hi 1.242 + mov edi, dest 1.243 + mov ebx, ex_lo 1.244 + mov ecx, ex_hi 1.245 + LOCK_IF_MP(mp) 1.246 + cmpxchg8b qword ptr [edi] 1.247 + pop edi 1.248 + pop ebx 1.249 + } 1.250 +} 1.251 + 1.252 +inline intptr_t Atomic::cmpxchg_ptr(intptr_t exchange_value, volatile intptr_t* dest, intptr_t compare_value) { 1.253 + return (intptr_t)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value); 1.254 +} 1.255 + 1.256 +inline void* Atomic::cmpxchg_ptr(void* exchange_value, volatile void* dest, void* compare_value) { 1.257 + return (void*)cmpxchg((jint)exchange_value, (volatile jint*)dest, (jint)compare_value); 1.258 +} 1.259 + 1.260 +inline jlong Atomic::load(volatile jlong* src) { 1.261 + volatile jlong dest; 1.262 + volatile jlong* pdest = &dest; 1.263 + __asm { 1.264 + mov eax, src 1.265 + fild qword ptr [eax] 1.266 + mov eax, pdest 1.267 + fistp qword ptr [eax] 1.268 + } 1.269 + return dest; 1.270 +} 1.271 + 1.272 +inline void Atomic::store(jlong store_value, volatile jlong* dest) { 1.273 + volatile jlong* src = &store_value; 1.274 + __asm { 1.275 + mov eax, src 1.276 + fild qword ptr [eax] 1.277 + mov eax, dest 1.278 + fistp qword ptr [eax] 1.279 + } 1.280 +} 1.281 + 1.282 +inline void Atomic::store(jlong store_value, jlong* dest) { 1.283 + Atomic::store(store_value, (volatile jlong*)dest); 1.284 +} 1.285 + 1.286 +#endif // AMD64 1.287 + 1.288 +#pragma warning(default: 4035) // Enables warnings reporting missing return statement 1.289 + 1.290 +#endif // OS_CPU_WINDOWS_X86_VM_ATOMIC_WINDOWS_X86_INLINE_HPP