Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahBarrierSet.inline.hpp
66644 views
/*1* Copyright (c) 2015, 2021, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP25#define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP2627#include "gc/shenandoah/shenandoahBarrierSet.hpp"2829#include "gc/shared/accessBarrierSupport.inline.hpp"30#include "gc/shenandoah/shenandoahAsserts.hpp"31#include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"32#include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"33#include "gc/shenandoah/shenandoahForwarding.inline.hpp"34#include "gc/shenandoah/shenandoahHeap.inline.hpp"35#include "gc/shenandoah/shenandoahHeapRegion.hpp"36#include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"37#include "gc/shenandoah/shenandoahThreadLocalData.hpp"38#include "oops/oop.inline.hpp"3940inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {41return ShenandoahForwarding::get_forwardee(p);42}4344inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {45if (p != NULL) {46return resolve_forwarded_not_null(p);47} else {48return p;49}50}5152inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {53return ShenandoahForwarding::get_forwardee_mutator(p);54}5556template <class T>57inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {58assert(ShenandoahLoadRefBarrier, "should be enabled");59shenandoah_assert_in_cset(load_addr, obj);6061oop fwd = resolve_forwarded_not_null_mutator(obj);62if (obj == fwd) {63assert(_heap->is_evacuation_in_progress(),64"evac should be in progress");65Thread* const t = Thread::current();66ShenandoahEvacOOMScope scope(t);67fwd = _heap->evacuate_object(obj, t);68}6970if (load_addr != NULL && fwd != obj) {71// Since we are here and we know the load address, update the reference.72ShenandoahHeap::cas_oop(fwd, load_addr, obj);73}7475return fwd;76}7778inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {79if (!ShenandoahLoadRefBarrier) {80return obj;81}82if (_heap->has_forwarded_objects() &&83_heap->in_collection_set(obj)) { // Subsumes NULL-check84assert(obj != NULL, "cset check must have subsumed NULL-check");85oop fwd = resolve_forwarded_not_null(obj);86// TODO: It should not be necessary to check evac-in-progress here.87// We do it for mark-compact, which may have forwarded objects,88// and objects in cset and gets here via runtime barriers.89// We can probably fix this as soon as mark-compact has its own90// marking phase.91if (obj == fwd && _heap->is_evacuation_in_progress()) {92Thread* t = Thread::current();93ShenandoahEvacOOMScope oom_evac_scope(t);94return _heap->evacuate_object(obj, t);95}96return fwd;97}98return obj;99}100101template <class T>102inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {103if (obj == NULL) {104return NULL;105}106107// Prevent resurrection of unreachable phantom (i.e. weak-native) references.108if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&109_heap->is_concurrent_weak_root_in_progress() &&110!_heap->marking_context()->is_marked(obj)) {111return NULL;112}113114// Prevent resurrection of unreachable weak references.115if ((decorators & ON_WEAK_OOP_REF) != 0 &&116_heap->is_concurrent_weak_root_in_progress() &&117!_heap->marking_context()->is_marked_strong(obj)) {118return NULL;119}120121// Prevent resurrection of unreachable objects that are visited during122// concurrent class-unloading.123if ((decorators & AS_NO_KEEPALIVE) != 0 &&124_heap->is_evacuation_in_progress() &&125!_heap->marking_context()->is_marked(obj)) {126return obj;127}128129oop fwd = load_reference_barrier(obj);130if (ShenandoahSelfFixing && load_addr != NULL && fwd != obj) {131// Since we are here and we know the load address, update the reference.132ShenandoahHeap::cas_oop(fwd, load_addr, obj);133}134135return fwd;136}137138inline void ShenandoahBarrierSet::enqueue(oop obj) {139assert(obj != NULL, "checked by caller");140assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");141142// Filter marked objects before hitting the SATB queues. The same predicate would143// be used by SATBMQ::filter to eliminate already marked objects downstream, but144// filtering here helps to avoid wasteful SATB queueing work to begin with.145if (!_heap->requires_marking(obj)) return;146147SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());148_satb_mark_queue_set.enqueue_known_active(queue, obj);149}150151template <DecoratorSet decorators, typename T>152inline void ShenandoahBarrierSet::satb_barrier(T *field) {153if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||154HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {155return;156}157if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {158T heap_oop = RawAccess<>::oop_load(field);159if (!CompressedOops::is_null(heap_oop)) {160enqueue(CompressedOops::decode(heap_oop));161}162}163}164165inline void ShenandoahBarrierSet::satb_enqueue(oop value) {166if (value != NULL && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {167enqueue(value);168}169}170171inline void ShenandoahBarrierSet::iu_barrier(oop obj) {172if (ShenandoahIUBarrier && obj != NULL && _heap->is_concurrent_mark_in_progress()) {173enqueue(obj);174}175}176177inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {178assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");179const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;180const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;181if (!peek && !on_strong_oop_ref) {182satb_enqueue(value);183}184}185186template <typename T>187inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {188oop value = RawAccess<>::oop_load(addr);189value = load_reference_barrier(decorators, value, addr);190keep_alive_if_weak(decorators, value);191return value;192}193194template <typename T>195inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {196iu_barrier(new_value);197oop res;198oop expected = compare_value;199do {200compare_value = expected;201res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);202expected = res;203} while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));204205// Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,206// because it must be the previous value.207res = load_reference_barrier(decorators, res, reinterpret_cast<T*>(NULL));208satb_enqueue(res);209return res;210}211212template <typename T>213inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {214iu_barrier(new_value);215oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);216// Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,217// because it must be the previous value.218previous = load_reference_barrier<T>(decorators, previous, reinterpret_cast<T*>(NULL));219satb_enqueue(previous);220return previous;221}222223template <DecoratorSet decorators, typename BarrierSetT>224template <typename T>225inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {226assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");227ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();228return bs->oop_load(decorators, addr);229}230231template <DecoratorSet decorators, typename BarrierSetT>232template <typename T>233inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {234assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");235ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();236return bs->oop_load(decorators, addr);237}238239template <DecoratorSet decorators, typename BarrierSetT>240inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {241ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();242DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);243return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));244}245246template <DecoratorSet decorators, typename BarrierSetT>247template <typename T>248inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {249shenandoah_assert_marked_if(NULL, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());250shenandoah_assert_not_in_cset_if(addr, value, value != NULL && !ShenandoahHeap::heap()->cancelled_gc());251ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();252bs->iu_barrier(value);253bs->satb_barrier<decorators>(addr);254Raw::oop_store(addr, value);255}256257template <DecoratorSet decorators, typename BarrierSetT>258template <typename T>259inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {260shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());261shenandoah_assert_not_forwarded_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());262shenandoah_assert_not_in_cset_except (addr, value, value == NULL || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());263264oop_store_not_in_heap(addr, value);265}266267template <DecoratorSet decorators, typename BarrierSetT>268inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {269oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);270}271272template <DecoratorSet decorators, typename BarrierSetT>273template <typename T>274inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {275assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");276ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();277return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);278}279280template <DecoratorSet decorators, typename BarrierSetT>281template <typename T>282inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {283assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");284ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();285return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);286}287288template <DecoratorSet decorators, typename BarrierSetT>289inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {290assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");291ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();292DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);293return bs->oop_cmpxchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);294}295296template <DecoratorSet decorators, typename BarrierSetT>297template <typename T>298inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {299assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");300ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();301return bs->oop_xchg(decorators, addr, new_value);302}303304template <DecoratorSet decorators, typename BarrierSetT>305template <typename T>306inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {307assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");308ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();309return bs->oop_xchg(decorators, addr, new_value);310}311312template <DecoratorSet decorators, typename BarrierSetT>313inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {314assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");315ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();316DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);317return bs->oop_xchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), new_value);318}319320// Clone barrier support321template <DecoratorSet decorators, typename BarrierSetT>322void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {323if (ShenandoahCloneBarrier) {324ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);325}326Raw::clone(src, dst, size);327}328329template <DecoratorSet decorators, typename BarrierSetT>330template <typename T>331bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,332arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,333size_t length) {334ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();335bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),336arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),337length);338return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);339}340341template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>342void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {343assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");344345Thread* thread = Thread::current();346SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);347ShenandoahMarkingContext* ctx = _heap->marking_context();348const ShenandoahCollectionSet* const cset = _heap->collection_set();349T* end = src + count;350for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {351T o = RawAccess<>::oop_load(elem_ptr);352if (!CompressedOops::is_null(o)) {353oop obj = CompressedOops::decode_not_null(o);354if (HAS_FWD && cset->is_in(obj)) {355oop fwd = resolve_forwarded_not_null(obj);356if (EVAC && obj == fwd) {357fwd = _heap->evacuate_object(obj, thread);358}359assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");360oop witness = ShenandoahHeap::cas_oop(fwd, elem_ptr, o);361obj = fwd;362}363if (ENQUEUE && !ctx->is_marked_strong(obj)) {364_satb_mark_queue_set.enqueue_known_active(queue, obj);365}366}367}368}369370template <class T>371void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {372if (count == 0) {373return;374}375int gc_state = _heap->gc_state();376if ((gc_state & ShenandoahHeap::MARKING) != 0) {377arraycopy_marking(src, dst, count);378} else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {379arraycopy_evacuation(src, count);380} else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {381arraycopy_update(src, count);382}383}384385template <class T>386void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {387assert(_heap->is_concurrent_mark_in_progress(), "only during marking");388T* array = ShenandoahSATBBarrier ? dst : src;389if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {390arraycopy_work<T, false, false, true>(array, count);391}392}393394inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {395return ary < _heap->heap_region_containing(ary)->get_update_watermark();396}397398template <class T>399void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {400assert(_heap->is_evacuation_in_progress(), "only during evacuation");401if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {402ShenandoahEvacOOMScope oom_evac;403arraycopy_work<T, true, true, false>(src, count);404}405}406407template <class T>408void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {409assert(_heap->is_update_refs_in_progress(), "only during update-refs");410if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {411arraycopy_work<T, true, false, false>(src, count);412}413}414415#endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP416417418