1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_GC_G1_G1BARRIERSET_INLINE_HPP
  26 #define SHARE_GC_G1_G1BARRIERSET_INLINE_HPP
  27 
  28 #include "gc/g1/g1BarrierSet.hpp"
  29 #include "gc/g1/g1CardTable.hpp"
  30 #include "gc/shared/accessBarrierSupport.inline.hpp"
  31 #include "oops/access.inline.hpp"
  32 #include "oops/compressedOops.inline.hpp"
  33 #include "oops/oop.hpp"
  34 #ifdef ASSERT
  35 #include "runtime/thread.inline.hpp"
  36 #endif
  37 
  38 template <DecoratorSet decorators, typename T>
  39 inline void G1BarrierSet::write_ref_field_pre(T* field) {
  40   if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
  41       HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
  42     return;
  43   }
  44 
  45   T heap_oop = RawAccess<MO_VOLATILE>::oop_load(field);
  46   if (!CompressedOops::is_null(heap_oop)) {
  47     enqueue(CompressedOops::decode_not_null(heap_oop));
  48   }
  49 }
  50 
  51 template <DecoratorSet decorators, typename T>
  52 inline void G1BarrierSet::write_ref_field_post(T* field, oop new_val) {
  53 /*
  54 #ifdef ASSERT
  55   if (Thread::current()->is_Java_thread()) {
  56     JavaThreadState s = ((JavaThread*)Thread::current())->thread_state();
  57     if (s != _thread_in_vm) {
  58       tty->print_cr("T(%s).state = %d", Thread::current()->name(), s);
  59       assert(false, "fail");
  60     }
  61   } else {
  62     tty->print_cr("T(%s) not JavaThread", Thread::current()->name());
  63     tty->print_cr("At safepoint? %d", SafepointSynchronize::is_at_safepoint());
  64     assert(false, "fail");
  65   }
  66 #endif
  67 */
  68 //  assert(SafepointSynchronize::is_at_safepoint() ||
  69 //         (Thread::current()->is_Java_thread() &&
  70 //             ((JavaThread*)Thread::current())->thread_state() == _thread_in_vm),
  71 //         "sanity");
  72 
  73   volatile CardValue* byte = _card_table->byte_for(field);
  74   if (*byte != G1CardTable::g1_young_card_val()) {
  75     // Take a slow path for cards in old
  76     write_ref_field_post_slow(byte);
  77   }
  78 }
  79 
  80 inline void G1BarrierSet::enqueue_if_weak(DecoratorSet decorators, oop value) {
  81   assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
  82   // Loading from a weak or phantom reference needs enqueueing, as
  83   // the object may not have been reachable (part of the snapshot)
  84   // when marking started.
  85   const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
  86   const bool peek              = (decorators & AS_NO_KEEPALIVE) != 0;
  87   const bool needs_enqueue     = (!peek && !on_strong_oop_ref);
  88 
  89   if (needs_enqueue && value != NULL) {
  90     enqueue(value);
  91   }
  92 }
  93 
  94 template <DecoratorSet decorators, typename BarrierSetT>
  95 template <typename T>
  96 inline oop G1BarrierSet::AccessBarrier<decorators, BarrierSetT>::
  97 oop_load_not_in_heap(T* addr) {
  98   oop value = ModRef::oop_load_not_in_heap(addr);
  99   enqueue_if_weak(decorators, value);
 100   return value;
 101 }
 102 
 103 template <DecoratorSet decorators, typename BarrierSetT>
 104 template <typename T>
 105 inline oop G1BarrierSet::AccessBarrier<decorators, BarrierSetT>::
 106 oop_load_in_heap(T* addr) {
 107   oop value = ModRef::oop_load_in_heap(addr);
 108   enqueue_if_weak(decorators, value);
 109   return value;
 110 }
 111 
 112 template <DecoratorSet decorators, typename BarrierSetT>
 113 inline oop G1BarrierSet::AccessBarrier<decorators, BarrierSetT>::
 114 oop_load_in_heap_at(oop base, ptrdiff_t offset) {
 115   oop value = ModRef::oop_load_in_heap_at(base, offset);
 116   enqueue_if_weak(AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset), value);
 117   return value;
 118 }
 119 
 120 template <DecoratorSet decorators, typename BarrierSetT>
 121 template <typename T>
 122 inline void G1BarrierSet::AccessBarrier<decorators, BarrierSetT>::
 123 oop_store_not_in_heap(T* addr, oop new_value) {
 124   // Apply SATB barriers for all non-heap references, to allow
 125   // concurrent scanning of such references.
 126   G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set());
 127   bs->write_ref_field_pre<decorators>(addr);
 128   Raw::oop_store(addr, new_value);
 129 }
 130 
 131 #endif // SHARE_GC_G1_G1BARRIERSET_INLINE_HPP