1 /*
  2  * Copyright (c) 2013, 2019, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP
 27 
 28 #include "gc/shared/accessBarrierSupport.hpp"
 29 #include "gc/shared/barrierSet.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.hpp"
 31 #include "gc/shenandoah/shenandoahSATBMarkQueueSet.hpp"
 32 
 33 class ShenandoahBarrierSetAssembler;
 34 
 35 class ShenandoahBarrierSet: public BarrierSet {
 36 private:
 37 
 38   ShenandoahHeap* _heap;
 39   BufferNode::Allocator _satb_mark_queue_buffer_allocator;
 40   ShenandoahSATBMarkQueueSet _satb_mark_queue_set;
 41 
 42 public:
 43   ShenandoahBarrierSet(ShenandoahHeap* heap);
 44 
 45   static ShenandoahBarrierSetAssembler* assembler();
 46 
 47   inline static ShenandoahBarrierSet* barrier_set() {
 48     return barrier_set_cast<ShenandoahBarrierSet>(BarrierSet::barrier_set());
 49   }
 50 
 51   static ShenandoahSATBMarkQueueSet& satb_mark_queue_set() {
 52     return barrier_set()->_satb_mark_queue_set;
 53   }
 54 
 55   static bool need_load_reference_barrier(DecoratorSet decorators, BasicType type);
 56   static bool use_load_reference_barrier_native(DecoratorSet decorators, BasicType type);
 57   static bool need_keep_alive_barrier(DecoratorSet decorators, BasicType type);
 58 
 59   void print_on(outputStream* st) const;
 60 
 61   bool is_a(BarrierSet::Name bsn);
 62 
 63   bool is_aligned(HeapWord* hw);
 64 
 65   template <class T>
 66   inline void arraycopy_barrier(T* src, T* dst, size_t count);
 67   inline void clone_barrier(oop src);
 68   void clone_barrier_runtime(oop src);
 69 
 70   virtual void on_thread_create(Thread* thread);
 71   virtual void on_thread_destroy(Thread* thread);
 72   virtual void on_thread_attach(Thread* thread);
 73   virtual void on_thread_detach(Thread* thread);
 74 
 75   static inline oop resolve_forwarded_not_null(oop p);
 76   static inline oop resolve_forwarded_not_null_mutator(oop p);
 77   static inline oop resolve_forwarded(oop p);
 78 
 79   template <DecoratorSet decorators, typename T>
 80   inline void satb_barrier(T* field);
 81   inline void satb_enqueue(oop value);
 82   inline void storeval_barrier(oop obj);
 83 
 84   template <DecoratorSet decorators>
 85   inline void keep_alive_if_weak(oop value);
 86   inline void keep_alive_if_weak(DecoratorSet decorators, oop value);
 87 
 88   inline void enqueue(oop obj);
 89 
 90   oop load_reference_barrier(oop obj);
 91   oop load_reference_barrier_not_null(oop obj);
 92 
 93   template <class T>
 94   inline oop load_reference_barrier_mutator(oop obj, T* load_addr);
 95 
 96   oop load_reference_barrier_native(oop obj, oop* load_addr);
 97   oop load_reference_barrier_native(oop obj, narrowOop* load_addr);
 98 
 99 private:
100   template <class T>
101   inline void arraycopy_marking(T* src, T* dst, size_t count);
102   template <class T>
103   inline void arraycopy_evacuation(T* src, size_t count);
104   template <class T>
105   inline void arraycopy_update(T* src, size_t count);
106 
107   inline void clone_marking(oop src);
108   inline void clone_evacuation(oop src);
109   inline void clone_update(oop src);
110 
111   template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
112   inline void arraycopy_work(T* src, size_t count);
113 
114   oop load_reference_barrier_impl(oop obj);
115 
116   template <class T>
117   oop load_reference_barrier_native_impl(oop obj, T* load_addr);
118 
119   inline bool need_bulk_update(HeapWord* dst);
120 public:
121   // Callbacks for runtime accesses.
122   template <DecoratorSet decorators, typename BarrierSetT = ShenandoahBarrierSet>
123   class AccessBarrier: public BarrierSet::AccessBarrier<decorators, BarrierSetT> {
124     typedef BarrierSet::AccessBarrier<decorators, BarrierSetT> Raw;
125 
126   public:
127     // Heap oop accesses. These accessors get resolved when
128     // IN_HEAP is set (e.g. when using the HeapAccess API), it is
129     // an oop_* overload, and the barrier strength is AS_NORMAL.
130     template <typename T>
131     static oop oop_load_in_heap(T* addr);
132     static oop oop_load_in_heap_at(oop base, ptrdiff_t offset);
133 
134     template <typename T>
135     static void oop_store_in_heap(T* addr, oop value);
136     static void oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value);
137 
138     template <typename T>
139     static oop oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value);
140     static oop oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value);
141 
142     template <typename T>
143     static oop oop_atomic_xchg_in_heap(T* addr, oop new_value);
144     static oop oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value);
145 
146     template <typename T>
147     static bool oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
148                                       arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
149                                       size_t length);
150 
151     // Clone barrier support
152     static void clone_in_heap(oop src, oop dst, size_t size);
153 
154     // Needed for loads on non-heap weak references
155     template <typename T>
156     static oop oop_load_not_in_heap(T* addr);
157 
158     // Used for catching bad stores
159     template <typename T>
160     static void oop_store_not_in_heap(T* addr, oop value);
161 
162     template <typename T>
163     static oop oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value);
164 
165     template <typename T>
166     static oop oop_atomic_xchg_not_in_heap(T* addr, oop new_value);
167 
168   };
169 
170 };
171 
172 template<>
173 struct BarrierSet::GetName<ShenandoahBarrierSet> {
174   static const BarrierSet::Name value = BarrierSet::ShenandoahBarrierSet;
175 };
176 
177 template<>
178 struct BarrierSet::GetType<BarrierSet::ShenandoahBarrierSet> {
179   typedef ::ShenandoahBarrierSet type;
180 };
181 
182 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_HPP