Path: blob/master/src/hotspot/share/oops/accessBackend.inline.hpp
40951 views
/*1* Copyright (c) 2017, 2021, 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#ifndef SHARE_OOPS_ACCESSBACKEND_INLINE_HPP25#define SHARE_OOPS_ACCESSBACKEND_INLINE_HPP2627#include "oops/accessBackend.hpp"2829#include "oops/access.hpp"30#include "oops/arrayOop.hpp"31#include "oops/compressedOops.inline.hpp"32#include "oops/oopsHierarchy.hpp"33#include "runtime/atomic.hpp"34#include "runtime/orderAccess.hpp"3536template <DecoratorSet decorators>37template <DecoratorSet idecorators, typename T>38inline typename EnableIf<39AccessInternal::MustConvertCompressedOop<idecorators, T>::value, T>::type40RawAccessBarrier<decorators>::decode_internal(typename HeapOopType<idecorators>::type value) {41if (HasDecorator<decorators, IS_NOT_NULL>::value) {42return CompressedOops::decode_not_null(value);43} else {44return CompressedOops::decode(value);45}46}4748template <DecoratorSet decorators>49template <DecoratorSet idecorators, typename T>50inline typename EnableIf<51AccessInternal::MustConvertCompressedOop<idecorators, T>::value,52typename HeapOopType<idecorators>::type>::type53RawAccessBarrier<decorators>::encode_internal(T value) {54if (HasDecorator<decorators, IS_NOT_NULL>::value) {55return CompressedOops::encode_not_null(value);56} else {57return CompressedOops::encode(value);58}59}6061template <DecoratorSet decorators>62template <typename T>63inline void RawAccessBarrier<decorators>::oop_store(void* addr, T value) {64typedef typename AccessInternal::EncodedType<decorators, T>::type Encoded;65Encoded encoded = encode(value);66store(reinterpret_cast<Encoded*>(addr), encoded);67}6869template <DecoratorSet decorators>70template <typename T>71inline void RawAccessBarrier<decorators>::oop_store_at(oop base, ptrdiff_t offset, T value) {72oop_store(field_addr(base, offset), value);73}7475template <DecoratorSet decorators>76template <typename T>77inline T RawAccessBarrier<decorators>::oop_load(void* addr) {78typedef typename AccessInternal::EncodedType<decorators, T>::type Encoded;79Encoded encoded = load<Encoded>(reinterpret_cast<Encoded*>(addr));80return decode<T>(encoded);81}8283template <DecoratorSet decorators>84template <typename T>85inline T RawAccessBarrier<decorators>::oop_load_at(oop base, ptrdiff_t offset) {86return oop_load<T>(field_addr(base, offset));87}8889template <DecoratorSet decorators>90template <typename T>91inline T RawAccessBarrier<decorators>::oop_atomic_cmpxchg(void* addr, T compare_value, T new_value) {92typedef typename AccessInternal::EncodedType<decorators, T>::type Encoded;93Encoded encoded_new = encode(new_value);94Encoded encoded_compare = encode(compare_value);95Encoded encoded_result = atomic_cmpxchg(reinterpret_cast<Encoded*>(addr),96encoded_compare,97encoded_new);98return decode<T>(encoded_result);99}100101template <DecoratorSet decorators>102template <typename T>103inline T RawAccessBarrier<decorators>::oop_atomic_cmpxchg_at(oop base, ptrdiff_t offset, T compare_value, T new_value) {104return oop_atomic_cmpxchg(field_addr(base, offset), compare_value, new_value);105}106107template <DecoratorSet decorators>108template <typename T>109inline T RawAccessBarrier<decorators>::oop_atomic_xchg(void* addr, T new_value) {110typedef typename AccessInternal::EncodedType<decorators, T>::type Encoded;111Encoded encoded_new = encode(new_value);112Encoded encoded_result = atomic_xchg(reinterpret_cast<Encoded*>(addr), encoded_new);113return decode<T>(encoded_result);114}115116template <DecoratorSet decorators>117template <typename T>118inline T RawAccessBarrier<decorators>::oop_atomic_xchg_at(oop base, ptrdiff_t offset, T new_value) {119return oop_atomic_xchg(field_addr(base, offset), new_value);120}121122template <DecoratorSet decorators>123template <typename T>124inline bool RawAccessBarrier<decorators>::oop_arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,125arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,126size_t length) {127return arraycopy(src_obj, src_offset_in_bytes, src_raw,128dst_obj, dst_offset_in_bytes, dst_raw,129length);130}131132template <DecoratorSet decorators>133template <DecoratorSet ds, typename T>134inline typename EnableIf<135HasDecorator<ds, MO_SEQ_CST>::value, T>::type136RawAccessBarrier<decorators>::load_internal(void* addr) {137if (support_IRIW_for_not_multiple_copy_atomic_cpu) {138OrderAccess::fence();139}140return Atomic::load_acquire(reinterpret_cast<const volatile T*>(addr));141}142143template <DecoratorSet decorators>144template <DecoratorSet ds, typename T>145inline typename EnableIf<146HasDecorator<ds, MO_ACQUIRE>::value, T>::type147RawAccessBarrier<decorators>::load_internal(void* addr) {148return Atomic::load_acquire(reinterpret_cast<const volatile T*>(addr));149}150151template <DecoratorSet decorators>152template <DecoratorSet ds, typename T>153inline typename EnableIf<154HasDecorator<ds, MO_RELAXED>::value, T>::type155RawAccessBarrier<decorators>::load_internal(void* addr) {156return Atomic::load(reinterpret_cast<const volatile T*>(addr));157}158159template <DecoratorSet decorators>160template <DecoratorSet ds, typename T>161inline typename EnableIf<162HasDecorator<ds, MO_SEQ_CST>::value>::type163RawAccessBarrier<decorators>::store_internal(void* addr, T value) {164Atomic::release_store_fence(reinterpret_cast<volatile T*>(addr), value);165}166167template <DecoratorSet decorators>168template <DecoratorSet ds, typename T>169inline typename EnableIf<170HasDecorator<ds, MO_RELEASE>::value>::type171RawAccessBarrier<decorators>::store_internal(void* addr, T value) {172Atomic::release_store(reinterpret_cast<volatile T*>(addr), value);173}174175template <DecoratorSet decorators>176template <DecoratorSet ds, typename T>177inline typename EnableIf<178HasDecorator<ds, MO_RELAXED>::value>::type179RawAccessBarrier<decorators>::store_internal(void* addr, T value) {180Atomic::store(reinterpret_cast<volatile T*>(addr), value);181}182183template <DecoratorSet decorators>184template <DecoratorSet ds, typename T>185inline typename EnableIf<186HasDecorator<ds, MO_RELAXED>::value, T>::type187RawAccessBarrier<decorators>::atomic_cmpxchg_internal(void* addr, T compare_value, T new_value) {188return Atomic::cmpxchg(reinterpret_cast<volatile T*>(addr),189compare_value,190new_value,191memory_order_relaxed);192}193194template <DecoratorSet decorators>195template <DecoratorSet ds, typename T>196inline typename EnableIf<197HasDecorator<ds, MO_SEQ_CST>::value, T>::type198RawAccessBarrier<decorators>::atomic_cmpxchg_internal(void* addr, T compare_value, T new_value) {199return Atomic::cmpxchg(reinterpret_cast<volatile T*>(addr),200compare_value,201new_value,202memory_order_conservative);203}204205template <DecoratorSet decorators>206template <DecoratorSet ds, typename T>207inline typename EnableIf<208HasDecorator<ds, MO_SEQ_CST>::value, T>::type209RawAccessBarrier<decorators>::atomic_xchg_internal(void* addr, T new_value) {210return Atomic::xchg(reinterpret_cast<volatile T*>(addr),211new_value);212}213214// For platforms that do not have native support for wide atomics,215// we can emulate the atomicity using a lock. So here we check216// whether that is necessary or not.217218template <DecoratorSet ds>219template <DecoratorSet decorators, typename T>220inline typename EnableIf<221AccessInternal::PossiblyLockedAccess<T>::value, T>::type222RawAccessBarrier<ds>::atomic_xchg_maybe_locked(void* addr, T new_value) {223if (!AccessInternal::wide_atomic_needs_locking()) {224return atomic_xchg_internal<ds>(addr, new_value);225} else {226AccessInternal::AccessLocker access_lock;227volatile T* p = reinterpret_cast<volatile T*>(addr);228T old_val = RawAccess<>::load(p);229RawAccess<>::store(p, new_value);230return old_val;231}232}233234template <DecoratorSet ds>235template <DecoratorSet decorators, typename T>236inline typename EnableIf<237AccessInternal::PossiblyLockedAccess<T>::value, T>::type238RawAccessBarrier<ds>::atomic_cmpxchg_maybe_locked(void* addr, T compare_value, T new_value) {239if (!AccessInternal::wide_atomic_needs_locking()) {240return atomic_cmpxchg_internal<ds>(addr, compare_value, new_value);241} else {242AccessInternal::AccessLocker access_lock;243volatile T* p = reinterpret_cast<volatile T*>(addr);244T old_val = RawAccess<>::load(p);245if (old_val == compare_value) {246RawAccess<>::store(p, new_value);247}248return old_val;249}250}251252class RawAccessBarrierArrayCopy: public AllStatic {253template<typename T> struct IsHeapWordSized: public IntegralConstant<bool, sizeof(T) == HeapWordSize> { };254public:255template <DecoratorSet decorators, typename T>256static inline typename EnableIf<257HasDecorator<decorators, INTERNAL_VALUE_IS_OOP>::value>::type258arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,259arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,260size_t length) {261src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);262dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);263264// We do not check for ARRAYCOPY_ATOMIC for oops, because they are unconditionally always atomic.265if (HasDecorator<decorators, ARRAYCOPY_ARRAYOF>::value) {266AccessInternal::arraycopy_arrayof_conjoint_oops(src_raw, dst_raw, length);267} else {268typedef typename HeapOopType<decorators>::type OopType;269AccessInternal::arraycopy_conjoint_oops(reinterpret_cast<OopType*>(src_raw),270reinterpret_cast<OopType*>(dst_raw), length);271}272}273274template <DecoratorSet decorators, typename T>275static inline typename EnableIf<276!HasDecorator<decorators, INTERNAL_VALUE_IS_OOP>::value &&277HasDecorator<decorators, ARRAYCOPY_ARRAYOF>::value>::type278arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,279arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,280size_t length) {281src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);282dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);283284AccessInternal::arraycopy_arrayof_conjoint(src_raw, dst_raw, length);285}286287template <DecoratorSet decorators, typename T>288static inline typename EnableIf<289!HasDecorator<decorators, INTERNAL_VALUE_IS_OOP>::value &&290HasDecorator<decorators, ARRAYCOPY_DISJOINT>::value && IsHeapWordSized<T>::value>::type291arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,292arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,293size_t length) {294src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);295dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);296297// There is only a disjoint optimization for word granularity copying298if (HasDecorator<decorators, ARRAYCOPY_ATOMIC>::value) {299AccessInternal::arraycopy_disjoint_words_atomic(src_raw, dst_raw, length);300} else {301AccessInternal::arraycopy_disjoint_words(src_raw, dst_raw, length);302}303}304305template <DecoratorSet decorators, typename T>306static inline typename EnableIf<307!HasDecorator<decorators, INTERNAL_VALUE_IS_OOP>::value &&308!(HasDecorator<decorators, ARRAYCOPY_DISJOINT>::value && IsHeapWordSized<T>::value) &&309!HasDecorator<decorators, ARRAYCOPY_ARRAYOF>::value &&310!HasDecorator<decorators, ARRAYCOPY_ATOMIC>::value>::type311arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,312arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,313size_t length) {314src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);315dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);316317AccessInternal::arraycopy_conjoint(src_raw, dst_raw, length);318}319320template <DecoratorSet decorators, typename T>321static inline typename EnableIf<322!HasDecorator<decorators, INTERNAL_VALUE_IS_OOP>::value &&323!(HasDecorator<decorators, ARRAYCOPY_DISJOINT>::value && IsHeapWordSized<T>::value) &&324!HasDecorator<decorators, ARRAYCOPY_ARRAYOF>::value &&325HasDecorator<decorators, ARRAYCOPY_ATOMIC>::value>::type326arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,327arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,328size_t length) {329src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);330dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);331332AccessInternal::arraycopy_conjoint_atomic(src_raw, dst_raw, length);333}334};335336template<> struct RawAccessBarrierArrayCopy::IsHeapWordSized<void>: public IntegralConstant<bool, false> { };337338template <DecoratorSet decorators>339template <typename T>340inline bool RawAccessBarrier<decorators>::arraycopy(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,341arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,342size_t length) {343RawAccessBarrierArrayCopy::arraycopy<decorators>(src_obj, src_offset_in_bytes, src_raw,344dst_obj, dst_offset_in_bytes, dst_raw,345length);346return true;347}348349template <DecoratorSet decorators>350inline void RawAccessBarrier<decorators>::clone(oop src, oop dst, size_t size) {351// 4839641 (4840070): We must do an oop-atomic copy, because if another thread352// is modifying a reference field in the clonee, a non-oop-atomic copy might353// be suspended in the middle of copying the pointer and end up with parts354// of two different pointers in the field. Subsequent dereferences will crash.355// 4846409: an oop-copy of objects with long or double fields or arrays of same356// won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead357// of oops. We know objects are aligned on a minimum of an jlong boundary.358// The same is true of StubRoutines::object_copy and the various oop_copy359// variants, and of the code generated by the inline_native_clone intrinsic.360361assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");362AccessInternal::arraycopy_conjoint_atomic(reinterpret_cast<jlong*>((oopDesc*)src),363reinterpret_cast<jlong*>((oopDesc*)dst),364align_object_size(size) / HeapWordsPerLong);365// Clear the header366dst->init_mark();367}368369#endif // SHARE_OOPS_ACCESSBACKEND_INLINE_HPP370371372