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