1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 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 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
  26 
  27 #include "gc/shared/barrierSet.hpp"
  28 #include "gc/shenandoah/brooksPointer.inline.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  30 #include "gc/shenandoah/shenandoahBaseBarrierSet.inline.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  32 
  33 template <DecoratorSet decorators, typename BarrierSetT>
  34 template <typename T>
  35 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(oop new_value, T* addr, oop compare_value) {
  36   oop res;
  37   oop expected = compare_value;
  38   do {
  39     compare_value = expected;
  40     res = Raw::oop_atomic_cmpxchg(new_value, addr, compare_value);
  41     expected = res;
  42   } while ((! oopDesc::unsafe_equals(compare_value, expected)) && oopDesc::unsafe_equals(resolve_forwarded(compare_value), resolve_forwarded(expected)));
  43   if (oopDesc::unsafe_equals(expected, compare_value)) {
  44     if (ShenandoahSATBBarrier && !CompressedOops::is_null(compare_value)) {
  45       ShenandoahBarrierSet::barrier_set()->enqueue(compare_value);
  46     }
  47   }
  48   return res;
  49 }
  50 
  51 template <DecoratorSet decorators, typename BarrierSetT>
  52 template <typename T>
  53 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(oop new_value, T* addr) {
  54   oop previous = Raw::oop_atomic_xchg(new_value, addr);
  55   if (ShenandoahSATBBarrier) {
  56     if (!CompressedOops::is_null(previous)) {
  57       ShenandoahBarrierSet::barrier_set()->enqueue(previous);
  58     }
  59   }
  60   return previous;
  61 }
  62 
  63 template <DecoratorSet decorators, typename BarrierSetT>
  64 template <typename T>
  65 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
  66                                                                                      arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
  67                                                                                      size_t length) {
  68   if (!CompressedOops::is_null(src_obj)) {
  69     src_obj = arrayOop(ShenandoahBarrierSet::barrier_set()->read_barrier(src_obj));
  70   }
  71   if (!CompressedOops::is_null(dst_obj)) {
  72     dst_obj = arrayOop(ShenandoahBarrierSet::barrier_set()->write_barrier(dst_obj));
  73   }
  74   Raw::arraycopy(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
  75 }
  76 
  77 // Clone barrier support
  78 template <DecoratorSet decorators, typename BarrierSetT>
  79 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
  80   src = arrayOop(ShenandoahBarrierSet::barrier_set()->read_barrier(src));
  81   dst = arrayOop(ShenandoahBarrierSet::barrier_set()->write_barrier(dst));
  82   Raw::clone(src, dst, size);
  83   ShenandoahBarrierSet::barrier_set()->write_region(MemRegion((HeapWord*) dst, size));
  84 }
  85 
  86 
  87 template <DecoratorSet decorators, typename BarrierSetT>
  88 template <typename T>
  89 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
  90                                                                                          arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
  91                                                                                          size_t length) {
  92   ShenandoahHeap* heap = ShenandoahHeap::heap();
  93   if (!CompressedOops::is_null(src_obj)) {
  94     src_obj = arrayOop(ShenandoahBaseBarrierSet::barrier_set()->read_barrier(src_obj));
  95   }
  96   if (!CompressedOops::is_null(dst_obj)) {
  97     dst_obj = arrayOop(ShenandoahBaseBarrierSet::barrier_set()->write_barrier(dst_obj));
  98   }
  99 
 100   bool satb = ShenandoahSATBBarrier && heap->is_concurrent_mark_in_progress();
 101   bool checkcast = HasDecorator<decorators, ARRAYCOPY_CHECKCAST>::value;
 102   ArrayCopyStoreValMode storeval_mode;
 103   if (heap->has_forwarded_objects()) {
 104     if (heap->is_concurrent_traversal_in_progress()) {
 105       storeval_mode = WRITE_BARRIER;
 106     } else if (heap->is_concurrent_mark_in_progress() || heap->is_update_refs_in_progress()) {
 107       storeval_mode = READ_BARRIER;
 108     } else {
 109       assert(heap->is_idle() || heap->is_evacuation_in_progress(), "must not have anything in progress");
 110       storeval_mode = NONE; // E.g. during evac or outside cycle
 111     }
 112   } else {
 113     assert(heap->is_stable() || heap->is_concurrent_mark_in_progress(), "must not have anything in progress");
 114     storeval_mode = NONE;
 115   }
 116 
 117   if (!satb && !checkcast && storeval_mode == NONE) {
 118     // Short-circuit to bulk copy.
 119     return Raw::oop_arraycopy(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
 120   }
 121 
 122   src_raw = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
 123   dst_raw = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
 124 
 125   Klass* bound = objArrayOop(dst_obj)->element_klass();
 126   ShenandoahBaseBarrierSet* bs = ShenandoahBaseBarrierSet::barrier_set();
 127   return bs->arraycopy_loop_1(src_raw, dst_raw, length, bound, checkcast, satb, storeval_mode);
 128 }
 129 
 130 #endif //SHARE_VM_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP