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 private:
184   BarrierSetNMethod* const _bs;
185 
186 public:
187   inline ShenandoahNMethodAndDisarmClosure(OopClosure* cl);
188   inline void do_nmethod(nmethod* nm);
189 };
190 
191 
192 //
193 // ========= Update References
194 //
195 
196 template <ShenandoahGenerationType GENERATION>
197 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkRefsSuperClosure {
198 private:
199   template <class T>
200   inline void work(T* p);
201 
202 public:
203   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q);
204 
205   virtual void do_oop(narrowOop* p) { work(p); }
206   virtual void do_oop(oop* p)       { work(p); }
207 };
208 
209 class ShenandoahUpdateRefsSuperClosure : public ShenandoahSuperClosure {};
210 
211 class ShenandoahNonConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
212 private:
213   template<class T>
214   inline void work(T* p);
215 
216 public:
217   virtual void do_oop(narrowOop* p) { work(p); }
218   virtual void do_oop(oop* p)       { work(p); }
219 };
220 
221 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
222 private:
223   template<class T>
224   inline void work(T* p);
225 
226 public:
227   virtual void do_oop(narrowOop* p) { work(p); }
228   virtual void do_oop(oop* p)       { work(p); }
229 };
230 
231 
232 class ShenandoahFlushSATB : public ThreadClosure {
233 private:
234   SATBMarkQueueSet& _satb_qset;
235 
236 public:
237   explicit ShenandoahFlushSATB(SATBMarkQueueSet& satb_qset) : _satb_qset(satb_qset) {}
238 
239   inline void do_thread(Thread* thread) override;
240 };
241 
242 
243 //
244 // ========= Utilities
245 //
246 
247 #ifdef ASSERT
248 class ShenandoahAssertNotForwardedClosure : public OopClosure {
249 private:
250   template <class T>
251   inline void do_oop_work(T* p);
252 
253 public:
254   inline void do_oop(narrowOop* p);
255   inline void do_oop(oop* p);
256 };
257 #endif // ASSERT
258 
259 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP