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   ALWAYSINLINE
 99   void do_oop_work(T* p) { work<T, GENERATION>(p); }
100 
101 public:
102   ShenandoahMarkRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q) :
103           ShenandoahMarkRefsSuperClosure(q, rp, old_q) {};
104 
105   ALWAYSINLINE
106   void do_oop(narrowOop* p) override { do_oop_work(p); }
107 
108   ALWAYSINLINE
109   void do_oop(oop* p) override { do_oop_work(p); }
110 };
111 
112 class ShenandoahForwardedIsAliveClosure : public BoolObjectClosure {
113 private:
114   ShenandoahMarkingContext* const _mark_context;
115 public:
116   inline ShenandoahForwardedIsAliveClosure();
117   inline bool do_object_b(oop obj);
118 };
119 
120 class ShenandoahIsAliveClosure : public BoolObjectClosure {
121 private:
122   ShenandoahMarkingContext* const _mark_context;
123 public:
124   inline ShenandoahIsAliveClosure();
125   inline bool do_object_b(oop obj);
126 };
127 
128 class ShenandoahIsAliveSelector : public StackObj {
129 private:
130   ShenandoahIsAliveClosure _alive_cl;
131   ShenandoahForwardedIsAliveClosure _fwd_alive_cl;
132 public:
133   inline BoolObjectClosure* is_alive_closure();
134 };
135 
136 class ShenandoahKeepAliveClosure : public OopClosure {
137 private:
138   ShenandoahBarrierSet* const _bs;
139   template <typename T>
140   void do_oop_work(T* p);
141 
142 public:
143   inline ShenandoahKeepAliveClosure();
144   inline void do_oop(oop* p)       { do_oop_work(p); }
145   inline void do_oop(narrowOop* p) { do_oop_work(p); }
146 };
147 
148 
149 //
150 // ========= Evacuating + Roots
151 //
152 
153 template <bool CONCURRENT, bool STABLE_THREAD>
154 class ShenandoahEvacuateUpdateRootClosureBase : public ShenandoahSuperClosure {
155 protected:
156   Thread* const _thread;
157 public:
158   inline ShenandoahEvacuateUpdateRootClosureBase() :
159     ShenandoahSuperClosure(),
160     _thread(STABLE_THREAD ? Thread::current() : nullptr) {}
161 
162   inline void do_oop(oop* p);
163   inline void do_oop(narrowOop* p);
164 protected:
165   template <class T>
166   inline void do_oop_work(T* p);
167 };
168 
169 using ShenandoahEvacuateUpdateMetadataClosure     = ShenandoahEvacuateUpdateRootClosureBase<false, true>;
170 using ShenandoahEvacuateUpdateRootsClosure        = ShenandoahEvacuateUpdateRootClosureBase<true, false>;
171 using ShenandoahContextEvacuateUpdateRootsClosure = ShenandoahEvacuateUpdateRootClosureBase<true, true>;
172 
173 
174 template <bool CONCURRENT, typename IsAlive, typename KeepAlive>
175 class ShenandoahCleanUpdateWeakOopsClosure : public OopClosure {
176 private:
177   IsAlive*    _is_alive;
178   KeepAlive*  _keep_alive;
179 
180 public:
181   inline ShenandoahCleanUpdateWeakOopsClosure(IsAlive* is_alive, KeepAlive* keep_alive);
182   inline void do_oop(oop* p);
183   inline void do_oop(narrowOop* p);
184 };
185 
186 //
187 // ========= Update References
188 //
189 
190 template <ShenandoahGenerationType GENERATION>
191 class ShenandoahMarkUpdateRefsClosure : public ShenandoahMarkRefsSuperClosure {
192 private:
193   template <class T>
194   inline void work(T* p);
195 
196 public:
197   ShenandoahMarkUpdateRefsClosure(ShenandoahObjToScanQueue* q, ShenandoahReferenceProcessor* rp, ShenandoahObjToScanQueue* old_q);
198 
199   virtual void do_oop(narrowOop* p) { work(p); }
200   virtual void do_oop(oop* p)       { work(p); }
201 };
202 
203 class ShenandoahUpdateRefsSuperClosure : public ShenandoahSuperClosure {};
204 
205 class ShenandoahNonConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
206 private:
207   template<class T>
208   inline void work(T* p);
209 
210 public:
211   virtual void do_oop(narrowOop* p) { work(p); }
212   virtual void do_oop(oop* p)       { work(p); }
213 };
214 
215 class ShenandoahConcUpdateRefsClosure : public ShenandoahUpdateRefsSuperClosure {
216 private:
217   template<class T>
218   inline void work(T* p);
219 
220 public:
221   virtual void do_oop(narrowOop* p) { work(p); }
222   virtual void do_oop(oop* p)       { work(p); }
223 };
224 
225 
226 class ShenandoahFlushSATB : public ThreadClosure {
227 private:
228   SATBMarkQueueSet& _satb_qset;
229 
230 public:
231   explicit ShenandoahFlushSATB(SATBMarkQueueSet& satb_qset) : _satb_qset(satb_qset) {}
232 
233   inline void do_thread(Thread* thread) override;
234 };
235 
236 
237 //
238 // ========= Utilities
239 //
240 
241 class ShenandoahNoOpClosure : public OopClosure {
242 public:
243   inline void do_oop(oop* p)       { }
244   inline void do_oop(narrowOop* p) { }
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 class ShenandoahMultiThreadClosure : public ThreadClosure {
260   ThreadClosure& _cl1;
261   ThreadClosure& _cl2;
262 public:
263   ShenandoahMultiThreadClosure(ThreadClosure& cl1, ThreadClosure& cl2) :
264     _cl1(cl1), _cl2(cl2) {}
265   inline void do_thread(Thread* thread) override {
266     _cl1.do_thread(thread);
267     _cl2.do_thread(thread);
268   }
269 };
270 
271 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCLOSURES_HPP