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