1 /*
  2  * Copyright (c) 2019, 2022, 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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP
 25 #define SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP
 26 
 27 #include "gc/shared/stringdedup/stringDedup.hpp"
 28 #include "gc/shenandoah/shenandoahGenerationType.hpp"
 29 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
 30 #include "memory/iterator.hpp"
 31 #include "runtime/javaThread.hpp"
 32 
 33 class BarrierSetNMethod;
 34 class ShenandoahBarrierSet;
 35 class ShenandoahHeap;
 36 class ShenandoahMarkingContext;
 37 class ShenandoahReferenceProcessor;
 38 class SATBMarkQueueSet;
 39 
 40 //
 41 // ========= Super
 42 //
 43 
 44 class ShenandoahSuperClosure : public MetadataVisitingOopIterateClosure {
 45 protected:
 46   ShenandoahHeap* const _heap;
 47 
 48 public:
 49   inline ShenandoahSuperClosure();
 50   inline ShenandoahSuperClosure(ShenandoahReferenceProcessor* rp);
 51   inline void do_nmethod(nmethod* nm);
 52 };
 53 
 54 //
 55 // ========= Marking
 56 //
 57 
 58 class ShenandoahFlushSATBHandshakeClosure : public HandshakeClosure {
 59 private:
 60   SATBMarkQueueSet& _qset;
 61 public:
 62   inline explicit ShenandoahFlushSATBHandshakeClosure(SATBMarkQueueSet& qset);
 63   inline void do_thread(Thread* thread) override;
 64 };
 65 
 66 class ShenandoahMarkRefsSuperClosure : public ShenandoahSuperClosure {
 67 private:
 68   ShenandoahObjToScanQueue* _queue;
 69   ShenandoahObjToScanQueue* _old_queue;
 70   ShenandoahMarkingContext* const _mark_context;
 71   bool _weak;
 72 
 73 protected:
 74   template <class T, ShenandoahGenerationType GENERATION>
 75   void work(T *p);
 76 
 77 public:
 78   inline ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q);
 79 
 80   bool is_weak() const {
 81     return _weak;
 82   }
 83 
 84   void set_weak(bool weak) {
 85     _weak = weak;
 86   }
 87 
 88   virtual void do_nmethod(nmethod* nm) {
 89     assert(!is_weak(), "Can't handle weak marking of nmethods");
 90     ShenandoahSuperClosure::do_nmethod(nm);
 91   }
 92 };
 93 
 94 template <ShenandoahGenerationType GENERATION>
 95 class ShenandoahMarkRefsClosure : public ShenandoahMarkRefsSuperClosure {
 96 private:
 97   template <class T>
 98   inline void do_oop_work(T* p)     { work<T, GENERATION>(p); }
 99 
100 public:
101   ShenandoahMarkRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q) :
102           ShenandoahMarkRefsSuperClosure(q, rp, old_q) {};
103 
104   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
105   virtual void do_oop(oop* p)       { do_oop_work(p); }
106 };
107 
108 class ShenandoahForwardedIsAliveClosure : public BoolObjectClosure {
109 private:
110   ShenandoahMarkingContext* const _mark_context;
111 public:
112   inline ShenandoahForwardedIsAliveClosure();
113   inline bool do_object_b(oop obj);
114 };
115 
116 class ShenandoahIsAliveClosure : public BoolObjectClosure {
117 private:
118   ShenandoahMarkingContext* const _mark_context;
119 public:
120   inline ShenandoahIsAliveClosure();
121   inline bool do_object_b(oop obj);
122 };
123 
124 class ShenandoahIsAliveSelector : public StackObj {
125 private:
126   ShenandoahIsAliveClosure _alive_cl;
127   ShenandoahForwardedIsAliveClosure _fwd_alive_cl;
128 public:
129   inline BoolObjectClosure* is_alive_closure();
130 };
131 
132 class ShenandoahKeepAliveClosure : public OopClosure {
133 private:
134   ShenandoahBarrierSet* const _bs;
135   template <typename T>
136   void do_oop_work(T* p);
137 
138 public:
139   inline ShenandoahKeepAliveClosure();
140   inline void do_oop(oop* p)       { do_oop_work(p); }
141   inline void do_oop(narrowOop* p) { do_oop_work(p); }
142 };
143 
144 
145 //
146 // ========= Evacuating + Roots
147 //
148 
149 template <bool CONCURRENT, bool STABLE_THREAD>
150 class ShenandoahEvacuateUpdateRootClosureBase : public ShenandoahSuperClosure {
151 protected:
152   Thread* const _thread;
153 public:
154   inline ShenandoahEvacuateUpdateRootClosureBase() :
155     ShenandoahSuperClosure(),
156     _thread(STABLE_THREAD ? Thread::current() : nullptr) {}
157 
158   inline void do_oop(oop* p);
159   inline void do_oop(narrowOop* p);
160 protected:
161   template <class T>
162   inline void do_oop_work(T* p);
163 };
164 
165 using ShenandoahEvacuateUpdateMetadataClosure     = ShenandoahEvacuateUpdateRootClosureBase<false, true>;
166 using ShenandoahEvacuateUpdateRootsClosure        = ShenandoahEvacuateUpdateRootClosureBase<true, false>;
167 using ShenandoahContextEvacuateUpdateRootsClosure = ShenandoahEvacuateUpdateRootClosureBase<true, true>;
168 
169 
170 template <bool CONCURRENT, typename IsAlive, typename KeepAlive>
171 class ShenandoahCleanUpdateWeakOopsClosure : public OopClosure {
172 private:
173   IsAlive*    _is_alive;
174   KeepAlive*  _keep_alive;
175 
176 public:
177   inline ShenandoahCleanUpdateWeakOopsClosure(IsAlive* is_alive, KeepAlive* keep_alive);
178   inline void do_oop(oop* p);
179   inline void do_oop(narrowOop* p);
180 };
181 
182 class ShenandoahNMethodAndDisarmClosure : public NMethodToOopClosure {
183 public:
184   inline ShenandoahNMethodAndDisarmClosure(OopClosure* cl);
185   inline void do_nmethod(nmethod* nm);
186 };
187 
188 
189 //
190 // ========= Update References
191 //
192 
193 template <ShenandoahGenerationType GENERATION>
194 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkRefsSuperClosure {
195 private:
196   template <class T>
197   inline void work(T* p);
198 
199 public:
200   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q);
201 
202   virtual void do_oop(narrowOop* p) { work(p); }
203   virtual void do_oop(oop* p)       { work(p); }
204 };
205 
206 class ShenandoahUpdateRefsSuperClosure : public ShenandoahSuperClosure {};
207 
208 class ShenandoahNonConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
209 private:
210   template<class T>
211   inline void work(T* p);
212 
213 public:
214   virtual void do_oop(narrowOop* p) { work(p); }
215   virtual void do_oop(oop* p)       { work(p); }
216 };
217 
218 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
219 private:
220   template<class T>
221   inline void work(T* p);
222 
223 public:
224   virtual void do_oop(narrowOop* p) { work(p); }
225   virtual void do_oop(oop* p)       { work(p); }
226 };
227 
228 
229 class ShenandoahFlushSATB : public ThreadClosure {
230 private:
231   SATBMarkQueueSet& _satb_qset;
232 
233 public:
234   explicit ShenandoahFlushSATB(SATBMarkQueueSet& satb_qset) : _satb_qset(satb_qset) {}
235 
236   inline void do_thread(Thread* thread) override;
237 };
238 
239 
240 //
241 // ========= Utilities
242 //
243 
244 class ShenandoahNoOpClosure : public OopClosure {
245 public:
246   inline void do_oop(oop* p)       { }
247   inline void do_oop(narrowOop* p) { }
248 };
249 
250 #ifdef ASSERT
251 class ShenandoahAssertNotForwardedClosure : public OopClosure {
252 private:
253   template <class T>
254   inline void do_oop_work(T* p);
255 
256 public:
257   inline void do_oop(narrowOop* p);
258   inline void do_oop(oop* p);
259 };
260 #endif // ASSERT
261 
262 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP