Path: blob/master/src/hotspot/share/oops/access.inline.hpp
40951 views
/*1* Copyright (c) 2017, 2018, 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_ACCESS_INLINE_HPP25#define SHARE_OOPS_ACCESS_INLINE_HPP2627#include "oops/access.hpp"2829#include "gc/shared/barrierSet.inline.hpp"30#include "gc/shared/barrierSetConfig.inline.hpp"31#include "oops/accessBackend.inline.hpp"3233// This file outlines the last 2 steps of the template pipeline of accesses going through34// the Access API.35// * Step 5.a: Barrier resolution. This step is invoked the first time a runtime-dispatch36// happens for an access. The appropriate BarrierSet::AccessBarrier accessor37// is resolved, then the function pointer is updated to that accessor for38// future invocations.39// * Step 5.b: Post-runtime dispatch. This step now casts previously unknown types such40// as the address type of an oop on the heap (is it oop* or narrowOop*) to41// the appropriate type. It also splits sufficiently orthogonal accesses into42// different functions, such as whether the access involves oops or primitives43// and whether the access is performed on the heap or outside. Then the44// appropriate BarrierSet::AccessBarrier is called to perform the access.4546namespace AccessInternal {47// Step 5.b: Post-runtime dispatch.48// This class is the last step before calling the BarrierSet::AccessBarrier.49// Here we make sure to figure out types that were not known prior to the50// runtime dispatch, such as whether an oop on the heap is oop or narrowOop.51// We also split orthogonal barriers such as handling primitives vs oops52// and on-heap vs off-heap into different calls to the barrier set.53template <class GCBarrierType, BarrierType type, DecoratorSet decorators>54struct PostRuntimeDispatch: public AllStatic { };5556template <class GCBarrierType, DecoratorSet decorators>57struct PostRuntimeDispatch<GCBarrierType, BARRIER_STORE, decorators>: public AllStatic {58template <typename T>59static void access_barrier(void* addr, T value) {60GCBarrierType::store_in_heap(reinterpret_cast<T*>(addr), value);61}6263static void oop_access_barrier(void* addr, oop value) {64typedef typename HeapOopType<decorators>::type OopType;65if (HasDecorator<decorators, IN_HEAP>::value) {66GCBarrierType::oop_store_in_heap(reinterpret_cast<OopType*>(addr), value);67} else {68GCBarrierType::oop_store_not_in_heap(reinterpret_cast<OopType*>(addr), value);69}70}71};7273template <class GCBarrierType, DecoratorSet decorators>74struct PostRuntimeDispatch<GCBarrierType, BARRIER_LOAD, decorators>: public AllStatic {75template <typename T>76static T access_barrier(void* addr) {77return GCBarrierType::load_in_heap(reinterpret_cast<T*>(addr));78}7980static oop oop_access_barrier(void* addr) {81typedef typename HeapOopType<decorators>::type OopType;82if (HasDecorator<decorators, IN_HEAP>::value) {83return GCBarrierType::oop_load_in_heap(reinterpret_cast<OopType*>(addr));84} else {85return GCBarrierType::oop_load_not_in_heap(reinterpret_cast<OopType*>(addr));86}87}88};8990template <class GCBarrierType, DecoratorSet decorators>91struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_XCHG, decorators>: public AllStatic {92template <typename T>93static T access_barrier(void* addr, T new_value) {94return GCBarrierType::atomic_xchg_in_heap(reinterpret_cast<T*>(addr), new_value);95}9697static oop oop_access_barrier(void* addr, oop new_value) {98typedef typename HeapOopType<decorators>::type OopType;99if (HasDecorator<decorators, IN_HEAP>::value) {100return GCBarrierType::oop_atomic_xchg_in_heap(reinterpret_cast<OopType*>(addr), new_value);101} else {102return GCBarrierType::oop_atomic_xchg_not_in_heap(reinterpret_cast<OopType*>(addr), new_value);103}104}105};106107template <class GCBarrierType, DecoratorSet decorators>108struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_CMPXCHG, decorators>: public AllStatic {109template <typename T>110static T access_barrier(void* addr, T compare_value, T new_value) {111return GCBarrierType::atomic_cmpxchg_in_heap(reinterpret_cast<T*>(addr), compare_value, new_value);112}113114static oop oop_access_barrier(void* addr, oop compare_value, oop new_value) {115typedef typename HeapOopType<decorators>::type OopType;116if (HasDecorator<decorators, IN_HEAP>::value) {117return GCBarrierType::oop_atomic_cmpxchg_in_heap(reinterpret_cast<OopType*>(addr), compare_value, new_value);118} else {119return GCBarrierType::oop_atomic_cmpxchg_not_in_heap(reinterpret_cast<OopType*>(addr), compare_value, new_value);120}121}122};123124template <class GCBarrierType, DecoratorSet decorators>125struct PostRuntimeDispatch<GCBarrierType, BARRIER_ARRAYCOPY, decorators>: public AllStatic {126template <typename T>127static bool access_barrier(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,128arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,129size_t length) {130GCBarrierType::arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw,131dst_obj, dst_offset_in_bytes, dst_raw,132length);133return true;134}135136template <typename T>137static bool oop_access_barrier(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,138arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,139size_t length) {140typedef typename HeapOopType<decorators>::type OopType;141return GCBarrierType::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, reinterpret_cast<OopType*>(src_raw),142dst_obj, dst_offset_in_bytes, reinterpret_cast<OopType*>(dst_raw),143length);144}145};146147template <class GCBarrierType, DecoratorSet decorators>148struct PostRuntimeDispatch<GCBarrierType, BARRIER_STORE_AT, decorators>: public AllStatic {149template <typename T>150static void access_barrier(oop base, ptrdiff_t offset, T value) {151GCBarrierType::store_in_heap_at(base, offset, value);152}153154static void oop_access_barrier(oop base, ptrdiff_t offset, oop value) {155GCBarrierType::oop_store_in_heap_at(base, offset, value);156}157};158159template <class GCBarrierType, DecoratorSet decorators>160struct PostRuntimeDispatch<GCBarrierType, BARRIER_LOAD_AT, decorators>: public AllStatic {161template <typename T>162static T access_barrier(oop base, ptrdiff_t offset) {163return GCBarrierType::template load_in_heap_at<T>(base, offset);164}165166static oop oop_access_barrier(oop base, ptrdiff_t offset) {167return GCBarrierType::oop_load_in_heap_at(base, offset);168}169};170171template <class GCBarrierType, DecoratorSet decorators>172struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_XCHG_AT, decorators>: public AllStatic {173template <typename T>174static T access_barrier(oop base, ptrdiff_t offset, T new_value) {175return GCBarrierType::atomic_xchg_in_heap_at(base, offset, new_value);176}177178static oop oop_access_barrier(oop base, ptrdiff_t offset, oop new_value) {179return GCBarrierType::oop_atomic_xchg_in_heap_at(base, offset, new_value);180}181};182183template <class GCBarrierType, DecoratorSet decorators>184struct PostRuntimeDispatch<GCBarrierType, BARRIER_ATOMIC_CMPXCHG_AT, decorators>: public AllStatic {185template <typename T>186static T access_barrier(oop base, ptrdiff_t offset, T compare_value, T new_value) {187return GCBarrierType::atomic_cmpxchg_in_heap_at(base, offset, compare_value, new_value);188}189190static oop oop_access_barrier(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {191return GCBarrierType::oop_atomic_cmpxchg_in_heap_at(base, offset, compare_value, new_value);192}193};194195template <class GCBarrierType, DecoratorSet decorators>196struct PostRuntimeDispatch<GCBarrierType, BARRIER_CLONE, decorators>: public AllStatic {197static void access_barrier(oop src, oop dst, size_t size) {198GCBarrierType::clone_in_heap(src, dst, size);199}200};201202template <class GCBarrierType, DecoratorSet decorators>203struct PostRuntimeDispatch<GCBarrierType, BARRIER_RESOLVE, decorators>: public AllStatic {204static oop access_barrier(oop obj) {205return GCBarrierType::resolve(obj);206}207};208209// Resolving accessors with barriers from the barrier set happens in two steps.210// 1. Expand paths with runtime-decorators, e.g. is UseCompressedOops on or off.211// 2. Expand paths for each BarrierSet available in the system.212template <DecoratorSet decorators, typename FunctionPointerT, BarrierType barrier_type>213struct BarrierResolver: public AllStatic {214template <DecoratorSet ds>215static typename EnableIf<216HasDecorator<ds, INTERNAL_VALUE_IS_OOP>::value,217FunctionPointerT>::type218resolve_barrier_gc() {219BarrierSet* bs = BarrierSet::barrier_set();220assert(bs != NULL, "GC barriers invoked before BarrierSet is set");221switch (bs->kind()) {222#define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name) \223case BarrierSet::bs_name: { \224return PostRuntimeDispatch<typename BarrierSet::GetType<BarrierSet::bs_name>::type:: \225AccessBarrier<ds>, barrier_type, ds>::oop_access_barrier; \226} \227break;228FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)229#undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE230231default:232fatal("BarrierSet AccessBarrier resolving not implemented");233return NULL;234};235}236237template <DecoratorSet ds>238static typename EnableIf<239!HasDecorator<ds, INTERNAL_VALUE_IS_OOP>::value,240FunctionPointerT>::type241resolve_barrier_gc() {242BarrierSet* bs = BarrierSet::barrier_set();243assert(bs != NULL, "GC barriers invoked before BarrierSet is set");244switch (bs->kind()) {245#define BARRIER_SET_RESOLVE_BARRIER_CLOSURE(bs_name) \246case BarrierSet::bs_name: { \247return PostRuntimeDispatch<typename BarrierSet::GetType<BarrierSet::bs_name>::type:: \248AccessBarrier<ds>, barrier_type, ds>::access_barrier; \249} \250break;251FOR_EACH_CONCRETE_BARRIER_SET_DO(BARRIER_SET_RESOLVE_BARRIER_CLOSURE)252#undef BARRIER_SET_RESOLVE_BARRIER_CLOSURE253254default:255fatal("BarrierSet AccessBarrier resolving not implemented");256return NULL;257};258}259260static FunctionPointerT resolve_barrier_rt() {261if (UseCompressedOops) {262const DecoratorSet expanded_decorators = decorators | INTERNAL_RT_USE_COMPRESSED_OOPS;263return resolve_barrier_gc<expanded_decorators>();264} else {265return resolve_barrier_gc<decorators>();266}267}268269static FunctionPointerT resolve_barrier() {270return resolve_barrier_rt();271}272};273274// Step 5.a: Barrier resolution275// The RuntimeDispatch class is responsible for performing a runtime dispatch of the276// accessor. This is required when the access either depends on whether compressed oops277// is being used, or it depends on which GC implementation was chosen (e.g. requires GC278// barriers). The way it works is that a function pointer initially pointing to an279// accessor resolution function gets called for each access. Upon first invocation,280// it resolves which accessor to be used in future invocations and patches the281// function pointer to this new accessor.282283template <DecoratorSet decorators, typename T>284void RuntimeDispatch<decorators, T, BARRIER_STORE>::store_init(void* addr, T value) {285func_t function = BarrierResolver<decorators, func_t, BARRIER_STORE>::resolve_barrier();286_store_func = function;287function(addr, value);288}289290template <DecoratorSet decorators, typename T>291void RuntimeDispatch<decorators, T, BARRIER_STORE_AT>::store_at_init(oop base, ptrdiff_t offset, T value) {292func_t function = BarrierResolver<decorators, func_t, BARRIER_STORE_AT>::resolve_barrier();293_store_at_func = function;294function(base, offset, value);295}296297template <DecoratorSet decorators, typename T>298T RuntimeDispatch<decorators, T, BARRIER_LOAD>::load_init(void* addr) {299func_t function = BarrierResolver<decorators, func_t, BARRIER_LOAD>::resolve_barrier();300_load_func = function;301return function(addr);302}303304template <DecoratorSet decorators, typename T>305T RuntimeDispatch<decorators, T, BARRIER_LOAD_AT>::load_at_init(oop base, ptrdiff_t offset) {306func_t function = BarrierResolver<decorators, func_t, BARRIER_LOAD_AT>::resolve_barrier();307_load_at_func = function;308return function(base, offset);309}310311template <DecoratorSet decorators, typename T>312T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_CMPXCHG>::atomic_cmpxchg_init(void* addr, T compare_value, T new_value) {313func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_CMPXCHG>::resolve_barrier();314_atomic_cmpxchg_func = function;315return function(addr, compare_value, new_value);316}317318template <DecoratorSet decorators, typename T>319T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_CMPXCHG_AT>::atomic_cmpxchg_at_init(oop base, ptrdiff_t offset, T compare_value, T new_value) {320func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_CMPXCHG_AT>::resolve_barrier();321_atomic_cmpxchg_at_func = function;322return function(base, offset, compare_value, new_value);323}324325template <DecoratorSet decorators, typename T>326T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_XCHG>::atomic_xchg_init(void* addr, T new_value) {327func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_XCHG>::resolve_barrier();328_atomic_xchg_func = function;329return function(addr, new_value);330}331332template <DecoratorSet decorators, typename T>333T RuntimeDispatch<decorators, T, BARRIER_ATOMIC_XCHG_AT>::atomic_xchg_at_init(oop base, ptrdiff_t offset, T new_value) {334func_t function = BarrierResolver<decorators, func_t, BARRIER_ATOMIC_XCHG_AT>::resolve_barrier();335_atomic_xchg_at_func = function;336return function(base, offset, new_value);337}338339template <DecoratorSet decorators, typename T>340bool RuntimeDispatch<decorators, T, BARRIER_ARRAYCOPY>::arraycopy_init(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) {343func_t function = BarrierResolver<decorators, func_t, BARRIER_ARRAYCOPY>::resolve_barrier();344_arraycopy_func = function;345return function(src_obj, src_offset_in_bytes, src_raw,346dst_obj, dst_offset_in_bytes, dst_raw,347length);348}349350template <DecoratorSet decorators, typename T>351void RuntimeDispatch<decorators, T, BARRIER_CLONE>::clone_init(oop src, oop dst, size_t size) {352func_t function = BarrierResolver<decorators, func_t, BARRIER_CLONE>::resolve_barrier();353_clone_func = function;354function(src, dst, size);355}356357template <DecoratorSet decorators, typename T>358oop RuntimeDispatch<decorators, T, BARRIER_RESOLVE>::resolve_init(oop obj) {359func_t function = BarrierResolver<decorators, func_t, BARRIER_RESOLVE>::resolve_barrier();360_resolve_func = function;361return function(obj);362}363}364365#endif // SHARE_OOPS_ACCESS_INLINE_HPP366367368