Path: blob/master/src/hotspot/share/oops/accessBackend.cpp
40951 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "accessBackend.inline.hpp"26#include "gc/shared/collectedHeap.hpp"27#include "oops/oop.inline.hpp"28#include "runtime/mutexLocker.hpp"29#include "runtime/vm_version.hpp"30#include "utilities/copy.hpp"3132namespace AccessInternal {33// VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'.34//35// On platforms which do not support atomic compare-and-swap of jlong (8 byte)36// values we have to use a lock-based scheme to enforce atomicity. This has to be37// applied to all Unsafe operations that set the value of a jlong field. Even so38// the compareAndSwapLong operation will not be atomic with respect to direct stores39// to the field from Java code. It is important therefore that any Java code that40// utilizes these Unsafe jlong operations does not perform direct stores. To permit41// direct loads of the field from Java code we must also use Atomic::store within the42// locked regions. And for good measure, in case there are direct stores, we also43// employ Atomic::load within those regions. Note that the field in question must be44// volatile and so must have atomic load/store accesses applied at the Java level.45//46// The locking scheme could utilize a range of strategies for controlling the locking47// granularity: from a lock per-field through to a single global lock. The latter is48// the simplest and is used for the current implementation. Note that the Java object49// that contains the field, can not, in general, be used for locking. To do so can lead50// to deadlocks as we may introduce locking into what appears to the Java code to be a51// lock-free path.52//53// As all the locked-regions are very short and themselves non-blocking we can treat54// them as leaf routines and elide safepoint checks (ie we don't perform any thread55// state transitions even when blocking for the lock). Note that if we do choose to56// add safepoint checks and thread state transitions, we must ensure that we calculate57// the address of the field _after_ we have acquired the lock, else the object may have58// been moved by the GC5960#ifndef SUPPORTS_NATIVE_CX86162// This is intentionally in the cpp file rather than the .inline.hpp file. It seems63// desirable to trade faster JDK build times (not propagating vm_version.hpp)64// for slightly worse runtime atomic jlong performance on 32 bit machines with65// support for 64 bit atomics.66bool wide_atomic_needs_locking() {67return !VM_Version::supports_cx8();68}6970AccessLocker::AccessLocker() {71assert(!VM_Version::supports_cx8(), "why else?");72UnsafeJlong_lock->lock_without_safepoint_check();73}7475AccessLocker::~AccessLocker() {76UnsafeJlong_lock->unlock();77}7879#endif8081// These forward copying calls to Copy without exposing the Copy type in headers unnecessarily8283void arraycopy_arrayof_conjoint_oops(void* src, void* dst, size_t length) {84Copy::arrayof_conjoint_oops(reinterpret_cast<HeapWord*>(src),85reinterpret_cast<HeapWord*>(dst), length);86}8788void arraycopy_conjoint_oops(oop* src, oop* dst, size_t length) {89Copy::conjoint_oops_atomic(src, dst, length);90}9192void arraycopy_conjoint_oops(narrowOop* src, narrowOop* dst, size_t length) {93Copy::conjoint_oops_atomic(src, dst, length);94}9596void arraycopy_disjoint_words(void* src, void* dst, size_t length) {97Copy::disjoint_words(reinterpret_cast<HeapWord*>(src),98reinterpret_cast<HeapWord*>(dst), length);99}100101void arraycopy_disjoint_words_atomic(void* src, void* dst, size_t length) {102Copy::disjoint_words_atomic(reinterpret_cast<HeapWord*>(src),103reinterpret_cast<HeapWord*>(dst), length);104}105106template<>107void arraycopy_conjoint<jboolean>(jboolean* src, jboolean* dst, size_t length) {108Copy::conjoint_jbytes(reinterpret_cast<jbyte*>(src), reinterpret_cast<jbyte*>(dst), length);109}110111template<>112void arraycopy_conjoint<jbyte>(jbyte* src, jbyte* dst, size_t length) {113Copy::conjoint_jbytes(src, dst, length);114}115116template<>117void arraycopy_conjoint<jchar>(jchar* src, jchar* dst, size_t length) {118Copy::conjoint_jshorts_atomic(reinterpret_cast<jshort*>(src), reinterpret_cast<jshort*>(dst), length);119}120121template<>122void arraycopy_conjoint<jshort>(jshort* src, jshort* dst, size_t length) {123Copy::conjoint_jshorts_atomic(src, dst, length);124}125126template<>127void arraycopy_conjoint<jint>(jint* src, jint* dst, size_t length) {128Copy::conjoint_jints_atomic(src, dst, length);129}130131template<>132void arraycopy_conjoint<jfloat>(jfloat* src, jfloat* dst, size_t length) {133Copy::conjoint_jints_atomic(reinterpret_cast<jint*>(src), reinterpret_cast<jint*>(dst), length);134}135136template<>137void arraycopy_conjoint<jlong>(jlong* src, jlong* dst, size_t length) {138Copy::conjoint_jlongs_atomic(src, dst, length);139}140141template<>142void arraycopy_conjoint<jdouble>(jdouble* src, jdouble* dst, size_t length) {143Copy::conjoint_jlongs_atomic(reinterpret_cast<jlong*>(src), reinterpret_cast<jlong*>(dst), length);144}145146template<>147void arraycopy_arrayof_conjoint<jbyte>(jbyte* src, jbyte* dst, size_t length) {148Copy::arrayof_conjoint_jbytes(reinterpret_cast<HeapWord*>(src),149reinterpret_cast<HeapWord*>(dst),150length);151}152153template<>154void arraycopy_arrayof_conjoint<jshort>(jshort* src, jshort* dst, size_t length) {155Copy::arrayof_conjoint_jshorts(reinterpret_cast<HeapWord*>(src),156reinterpret_cast<HeapWord*>(dst),157length);158}159160template<>161void arraycopy_arrayof_conjoint<jint>(jint* src, jint* dst, size_t length) {162Copy::arrayof_conjoint_jints(reinterpret_cast<HeapWord*>(src),163reinterpret_cast<HeapWord*>(dst),164length);165}166167template<>168void arraycopy_arrayof_conjoint<jlong>(jlong* src, jlong* dst, size_t length) {169Copy::arrayof_conjoint_jlongs(reinterpret_cast<HeapWord*>(src),170reinterpret_cast<HeapWord*>(dst),171length);172}173174template<>175void arraycopy_conjoint<void>(void* src, void* dst, size_t length) {176Copy::conjoint_jbytes(reinterpret_cast<jbyte*>(src),177reinterpret_cast<jbyte*>(dst),178length);179}180181template<>182void arraycopy_conjoint_atomic<jbyte>(jbyte* src, jbyte* dst, size_t length) {183Copy::conjoint_jbytes_atomic(src, dst, length);184}185186template<>187void arraycopy_conjoint_atomic<jshort>(jshort* src, jshort* dst, size_t length) {188Copy::conjoint_jshorts_atomic(src, dst, length);189}190191template<>192void arraycopy_conjoint_atomic<jint>(jint* src, jint* dst, size_t length) {193Copy::conjoint_jints_atomic(src, dst, length);194}195196template<>197void arraycopy_conjoint_atomic<jlong>(jlong* src, jlong* dst, size_t length) {198Copy::conjoint_jlongs_atomic(src, dst, length);199}200201template<>202void arraycopy_conjoint_atomic<void>(void* src, void* dst, size_t length) {203Copy::conjoint_memory_atomic(src, dst, length);204}205}206207208