Path: blob/master/src/hotspot/share/gc/shared/barrierSet.hpp
40957 views
/*1* Copyright (c) 2000, 2019, 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_GC_SHARED_BARRIERSET_HPP25#define SHARE_GC_SHARED_BARRIERSET_HPP2627#include "gc/shared/barrierSetConfig.hpp"28#include "memory/memRegion.hpp"29#include "oops/access.hpp"30#include "oops/accessBackend.hpp"31#include "oops/oopsHierarchy.hpp"32#include "utilities/fakeRttiSupport.hpp"33#include "utilities/macros.hpp"3435class BarrierSetAssembler;36class BarrierSetC1;37class BarrierSetC2;38class BarrierSetNMethod;39class JavaThread;4041// This class provides the interface between a barrier implementation and42// the rest of the system.4344class BarrierSet: public CHeapObj<mtGC> {45friend class VMStructs;4647static BarrierSet* _barrier_set;4849public:50enum Name {51#define BARRIER_SET_DECLARE_BS_ENUM(bs_name) bs_name ,52FOR_EACH_BARRIER_SET_DO(BARRIER_SET_DECLARE_BS_ENUM)53#undef BARRIER_SET_DECLARE_BS_ENUM54UnknownBS55};5657protected:58// Fake RTTI support. For a derived class T to participate59// - T must have a corresponding Name entry.60// - GetName<T> must be specialized to return the corresponding Name61// entry.62// - If T is a base class, the constructor must have a FakeRtti63// parameter and pass it up to its base class, with the tag set64// augmented with the corresponding Name entry.65// - If T is a concrete class, the constructor must create a66// FakeRtti object whose tag set includes the corresponding Name67// entry, and pass it up to its base class.68typedef FakeRttiSupport<BarrierSet, Name> FakeRtti;6970private:71FakeRtti _fake_rtti;72BarrierSetAssembler* _barrier_set_assembler;73BarrierSetC1* _barrier_set_c1;74BarrierSetC2* _barrier_set_c2;75BarrierSetNMethod* _barrier_set_nmethod;7677public:78// Metafunction mapping a class derived from BarrierSet to the79// corresponding Name enum tag.80template<typename T> struct GetName;8182// Metafunction mapping a Name enum type to the corresponding83// lass derived from BarrierSet.84template<BarrierSet::Name T> struct GetType;8586// Note: This is not presently the Name corresponding to the87// concrete class of this object.88BarrierSet::Name kind() const { return _fake_rtti.concrete_tag(); }8990// Test whether this object is of the type corresponding to bsn.91bool is_a(BarrierSet::Name bsn) const { return _fake_rtti.has_tag(bsn); }9293// End of fake RTTI support.9495protected:96BarrierSet(BarrierSetAssembler* barrier_set_assembler,97BarrierSetC1* barrier_set_c1,98BarrierSetC2* barrier_set_c2,99BarrierSetNMethod* barrier_set_nmethod,100const FakeRtti& fake_rtti) :101_fake_rtti(fake_rtti),102_barrier_set_assembler(barrier_set_assembler),103_barrier_set_c1(barrier_set_c1),104_barrier_set_c2(barrier_set_c2),105_barrier_set_nmethod(barrier_set_nmethod) {}106~BarrierSet() { }107108template <class BarrierSetAssemblerT>109static BarrierSetAssembler* make_barrier_set_assembler() {110return NOT_ZERO(new BarrierSetAssemblerT()) ZERO_ONLY(NULL);111}112113template <class BarrierSetC1T>114static BarrierSetC1* make_barrier_set_c1() {115return COMPILER1_PRESENT(new BarrierSetC1T()) NOT_COMPILER1(NULL);116}117118template <class BarrierSetC2T>119static BarrierSetC2* make_barrier_set_c2() {120return COMPILER2_PRESENT(new BarrierSetC2T()) NOT_COMPILER2(NULL);121}122123public:124// Support for optimizing compilers to call the barrier set on slow path allocations125// that did not enter a TLAB. Used for e.g. ReduceInitialCardMarks.126// The allocation is safe to use iff it returns true. If not, the slow-path allocation127// is redone until it succeeds. This can e.g. prevent allocations from the slow path128// to be in old.129virtual void on_slowpath_allocation_exit(JavaThread* thread, oop new_obj) {}130virtual void on_thread_create(Thread* thread) {}131virtual void on_thread_destroy(Thread* thread) {}132133// These perform BarrierSet-related initialization/cleanup before the thread134// is added to or removed from the corresponding set of threads. The135// argument thread is the current thread. These are called either holding136// the Threads_lock (for a JavaThread) and so not at a safepoint, or holding137// the NonJavaThreadsList_lock (for a NonJavaThread) locked by the138// caller. That locking ensures the operation is "atomic" with the list139// modification wrto operations that hold the NJTList_lock and either also140// hold the Threads_lock or are at a safepoint.141virtual void on_thread_attach(Thread* thread) {}142virtual void on_thread_detach(Thread* thread) {}143144virtual void make_parsable(JavaThread* thread) {}145146public:147// Print a description of the memory for the barrier set148virtual void print_on(outputStream* st) const = 0;149150static BarrierSet* barrier_set() { return _barrier_set; }151static void set_barrier_set(BarrierSet* barrier_set);152153BarrierSetAssembler* barrier_set_assembler() {154assert(_barrier_set_assembler != NULL, "should be set");155return _barrier_set_assembler;156}157158BarrierSetC1* barrier_set_c1() {159assert(_barrier_set_c1 != NULL, "should be set");160return _barrier_set_c1;161}162163BarrierSetC2* barrier_set_c2() {164assert(_barrier_set_c2 != NULL, "should be set");165return _barrier_set_c2;166}167168BarrierSetNMethod* barrier_set_nmethod() {169return _barrier_set_nmethod;170}171172// The AccessBarrier of a BarrierSet subclass is called by the Access API173// (cf. oops/access.hpp) to perform decorated accesses. GC implementations174// may override these default access operations by declaring an175// AccessBarrier class in its BarrierSet. Its accessors will then be176// automatically resolved at runtime.177//178// In order to register a new FooBarrierSet::AccessBarrier with the Access API,179// the following steps should be taken:180// 1) Provide an enum "name" for the BarrierSet in barrierSetConfig.hpp181// 2) Make sure the barrier set headers are included from barrierSetConfig.inline.hpp182// 3) Provide specializations for BarrierSet::GetName and BarrierSet::GetType.183template <DecoratorSet decorators, typename BarrierSetT>184class AccessBarrier: protected RawAccessBarrier<decorators> {185private:186typedef RawAccessBarrier<decorators> Raw;187188public:189// Primitive heap accesses. These accessors get resolved when190// IN_HEAP is set (e.g. when using the HeapAccess API), it is191// not an oop_* overload, and the barrier strength is AS_NORMAL.192template <typename T>193static T load_in_heap(T* addr) {194return Raw::template load<T>(addr);195}196197template <typename T>198static T load_in_heap_at(oop base, ptrdiff_t offset) {199return Raw::template load_at<T>(base, offset);200}201202template <typename T>203static void store_in_heap(T* addr, T value) {204Raw::store(addr, value);205}206207template <typename T>208static void store_in_heap_at(oop base, ptrdiff_t offset, T value) {209Raw::store_at(base, offset, value);210}211212template <typename T>213static T atomic_cmpxchg_in_heap(T* addr, T compare_value, T new_value) {214return Raw::atomic_cmpxchg(addr, compare_value, new_value);215}216217template <typename T>218static T atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, T compare_value, T new_value) {219return Raw::atomic_cmpxchg_at(base, offset, compare_value, new_value);220}221222template <typename T>223static T atomic_xchg_in_heap(T* addr, T new_value) {224return Raw::atomic_xchg(addr, new_value);225}226227template <typename T>228static T atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, T new_value) {229return Raw::atomic_xchg_at(base, offset, new_value);230}231232template <typename T>233static void arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,234arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,235size_t length) {236Raw::arraycopy(src_obj, src_offset_in_bytes, src_raw,237dst_obj, dst_offset_in_bytes, dst_raw,238length);239}240241// Heap oop accesses. These accessors get resolved when242// IN_HEAP is set (e.g. when using the HeapAccess API), it is243// an oop_* overload, and the barrier strength is AS_NORMAL.244template <typename T>245static oop oop_load_in_heap(T* addr) {246return Raw::template oop_load<oop>(addr);247}248249static oop oop_load_in_heap_at(oop base, ptrdiff_t offset) {250return Raw::template oop_load_at<oop>(base, offset);251}252253template <typename T>254static void oop_store_in_heap(T* addr, oop value) {255Raw::oop_store(addr, value);256}257258static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {259Raw::oop_store_at(base, offset, value);260}261262template <typename T>263static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {264return Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);265}266267static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {268return Raw::oop_atomic_cmpxchg_at(base, offset, compare_value, new_value);269}270271template <typename T>272static oop oop_atomic_xchg_in_heap(T* addr, oop new_value) {273return Raw::oop_atomic_xchg(addr, new_value);274}275276static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {277return Raw::oop_atomic_xchg_at(base, offset, new_value);278}279280template <typename T>281static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,282arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,283size_t length);284285// Off-heap oop accesses. These accessors get resolved when286// IN_HEAP is not set (e.g. when using the NativeAccess API), it is287// an oop* overload, and the barrier strength is AS_NORMAL.288template <typename T>289static oop oop_load_not_in_heap(T* addr) {290return Raw::template oop_load<oop>(addr);291}292293template <typename T>294static void oop_store_not_in_heap(T* addr, oop value) {295Raw::oop_store(addr, value);296}297298template <typename T>299static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {300return Raw::oop_atomic_cmpxchg(addr, compare_value, new_value);301}302303template <typename T>304static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {305return Raw::oop_atomic_xchg(addr, new_value);306}307308// Clone barrier support309static void clone_in_heap(oop src, oop dst, size_t size) {310Raw::clone(src, dst, size);311}312313static oop resolve(oop obj) {314return Raw::resolve(obj);315}316};317};318319template<typename T>320inline T* barrier_set_cast(BarrierSet* bs) {321assert(bs->is_a(BarrierSet::GetName<T>::value), "wrong type of barrier set");322return static_cast<T*>(bs);323}324325#endif // SHARE_GC_SHARED_BARRIERSET_HPP326327328