1 /*
2 * Copyright (c) 2015, 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
25 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
27
28 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
29
30 #include "gc/shared/accessBarrierSupport.inline.hpp"
31 #include "gc/shenandoah/shenandoahAsserts.hpp"
32 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
33 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
34 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
35 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
36 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
37 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
38 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
39 #include "oops/oop.inline.hpp"
40
41 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
42 return ShenandoahForwarding::get_forwardee(p);
43 }
44
45 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
46 if (p != nullptr) {
47 return resolve_forwarded_not_null(p);
48 } else {
49 return p;
50 }
51 }
52
53 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {
54 return ShenandoahForwarding::get_forwardee_mutator(p);
55 }
56
57 template <class T>
58 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
59 assert(ShenandoahLoadRefBarrier, "should be enabled");
60 shenandoah_assert_in_cset(load_addr, obj);
61
62 oop fwd = resolve_forwarded_not_null_mutator(obj);
63 if (obj == fwd) {
64 assert(_heap->is_evacuation_in_progress(),
65 "evac should be in progress");
66 Thread* const t = Thread::current();
67 ShenandoahEvacOOMScope scope(t);
68 fwd = _heap->evacuate_object(obj, t);
69 }
70
71 if (load_addr != nullptr && fwd != obj) {
72 // Since we are here and we know the load address, update the reference.
73 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
74 }
75
76 return fwd;
77 }
78
79 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
80 if (!ShenandoahLoadRefBarrier) {
81 return obj;
82 }
83 if (_heap->has_forwarded_objects() &&
84 _heap->in_collection_set(obj)) { // Subsumes null-check
85 assert(obj != nullptr, "cset check must have subsumed null-check");
86 oop fwd = resolve_forwarded_not_null(obj);
87 if (obj == fwd && _heap->is_evacuation_in_progress()) {
88 Thread* t = Thread::current();
89 ShenandoahEvacOOMScope oom_evac_scope(t);
90 return _heap->evacuate_object(obj, t);
91 }
92 return fwd;
93 }
94 return obj;
95 }
96
97 template <class T>
98 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
99 if (obj == nullptr) {
100 return nullptr;
101 }
102
103 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
104 if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
105 _heap->is_concurrent_weak_root_in_progress() &&
106 !_heap->marking_context()->is_marked(obj)) {
107 return nullptr;
108 }
109
110 // Prevent resurrection of unreachable weak references.
111 if ((decorators & ON_WEAK_OOP_REF) != 0 &&
112 _heap->is_concurrent_weak_root_in_progress() &&
113 !_heap->marking_context()->is_marked_strong(obj)) {
114 return nullptr;
115 }
116
117 // Prevent resurrection of unreachable objects that are visited during
118 // concurrent class-unloading.
119 if ((decorators & AS_NO_KEEPALIVE) != 0 &&
120 _heap->is_evacuation_in_progress() &&
121 !_heap->marking_context()->is_marked(obj)) {
122 return obj;
123 }
124
125 oop fwd = load_reference_barrier(obj);
126 if (load_addr != nullptr && fwd != obj) {
127 // Since we are here and we know the load address, update the reference.
128 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
129 }
130
131 return fwd;
132 }
133
134 inline void ShenandoahBarrierSet::enqueue(oop obj) {
135 assert(obj != nullptr, "checked by caller");
136 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
137
138 // Filter marked objects before hitting the SATB queues. The same predicate would
139 // be used by SATBMQ::filter to eliminate already marked objects downstream, but
140 // filtering here helps to avoid wasteful SATB queueing work to begin with.
141 if (!_heap->requires_marking(obj)) return;
142
143 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
144 _satb_mark_queue_set.enqueue_known_active(queue, obj);
145 }
146
147 template <DecoratorSet decorators, typename T>
148 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
149 if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
150 HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
151 return;
152 }
153 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
154 T heap_oop = RawAccess<>::oop_load(field);
155 if (!CompressedOops::is_null(heap_oop)) {
156 enqueue(CompressedOops::decode(heap_oop));
157 }
158 }
159 }
160
161 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
162 if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
163 enqueue(value);
164 }
165 }
166
167 inline void ShenandoahBarrierSet::iu_barrier(oop obj) {
168 if (ShenandoahIUBarrier && obj != nullptr && _heap->is_concurrent_mark_in_progress()) {
169 enqueue(obj);
170 }
171 }
172
173 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
174 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
175 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
176 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
177 if (!peek && !on_strong_oop_ref) {
178 satb_enqueue(value);
179 }
180 }
181
182 template <typename T>
183 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
184 oop value = RawAccess<>::oop_load(addr);
185 value = load_reference_barrier(decorators, value, addr);
186 keep_alive_if_weak(decorators, value);
187 return value;
188 }
189
190 template <typename T>
191 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
192 iu_barrier(new_value);
193 oop res;
194 oop expected = compare_value;
195 do {
196 compare_value = expected;
197 res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
198 expected = res;
199 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
200
201 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
202 // because it must be the previous value.
203 res = load_reference_barrier(decorators, res, static_cast<T*>(nullptr));
204 satb_enqueue(res);
205 return res;
206 }
207
208 template <typename T>
209 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
210 iu_barrier(new_value);
211 oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);
212 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
213 // because it must be the previous value.
214 previous = load_reference_barrier<T>(decorators, previous, static_cast<T*>(nullptr));
215 satb_enqueue(previous);
216 return previous;
217 }
218
219 template <DecoratorSet decorators, typename BarrierSetT>
220 template <typename T>
221 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
222 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
223 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
224 return bs->oop_load(decorators, addr);
225 }
226
227 template <DecoratorSet decorators, typename BarrierSetT>
228 template <typename T>
229 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
230 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
231 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
232 return bs->oop_load(decorators, addr);
233 }
234
235 template <DecoratorSet decorators, typename BarrierSetT>
236 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
237 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
238 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
239 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
240 }
241
242 template <DecoratorSet decorators, typename BarrierSetT>
243 template <typename T>
244 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
245 shenandoah_assert_marked_if(nullptr, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());
246 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
247 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
248 bs->iu_barrier(value);
249 bs->satb_barrier<decorators>(addr);
250 Raw::oop_store(addr, value);
251 }
252
253 template <DecoratorSet decorators, typename BarrierSetT>
254 template <typename T>
255 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
256 oop_store_common(addr, value);
257 }
258
259 template <DecoratorSet decorators, typename BarrierSetT>
260 template <typename T>
261 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
262 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
263 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
264
265 oop_store_common(addr, value);
266 }
267
268 template <DecoratorSet decorators, typename BarrierSetT>
269 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
270 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
271 }
272
273 template <DecoratorSet decorators, typename BarrierSetT>
274 template <typename T>
275 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
276 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
277 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
278 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
279 }
280
281 template <DecoratorSet decorators, typename BarrierSetT>
282 template <typename T>
283 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
284 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
285 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
286 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
287 }
288
289 template <DecoratorSet decorators, typename BarrierSetT>
290 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
291 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
292 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
293 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
294 return bs->oop_cmpxchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);
295 }
296
297 template <DecoratorSet decorators, typename BarrierSetT>
298 template <typename T>
299 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
300 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
301 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
302 return bs->oop_xchg(decorators, addr, new_value);
303 }
304
305 template <DecoratorSet decorators, typename BarrierSetT>
306 template <typename T>
307 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
308 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
309 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
310 return bs->oop_xchg(decorators, addr, new_value);
311 }
312
313 template <DecoratorSet decorators, typename BarrierSetT>
314 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
315 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
316 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
317 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
318 return bs->oop_xchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), new_value);
319 }
320
321 // Clone barrier support
322 template <DecoratorSet decorators, typename BarrierSetT>
323 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
324 if (ShenandoahCloneBarrier) {
325 ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
326 }
327 Raw::clone(src, dst, size);
328 }
329
330 template <DecoratorSet decorators, typename BarrierSetT>
331 template <typename T>
332 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
333 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
334 size_t length) {
335 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
336 bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
337 arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
338 length);
339 return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
340 }
341
342 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
343 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
344 assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");
345
346 Thread* thread = Thread::current();
347 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
348 ShenandoahMarkingContext* ctx = _heap->marking_context();
349 const ShenandoahCollectionSet* const cset = _heap->collection_set();
350 T* end = src + count;
351 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
352 T o = RawAccess<>::oop_load(elem_ptr);
353 if (!CompressedOops::is_null(o)) {
354 oop obj = CompressedOops::decode_not_null(o);
355 if (HAS_FWD && cset->is_in(obj)) {
356 oop fwd = resolve_forwarded_not_null(obj);
357 if (EVAC && obj == fwd) {
358 fwd = _heap->evacuate_object(obj, thread);
359 }
360 assert(obj != fwd || _heap->cancelled_gc(), "must be forwarded");
361 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
362 obj = fwd;
363 }
364 if (ENQUEUE && !ctx->is_marked_strong(obj)) {
365 _satb_mark_queue_set.enqueue_known_active(queue, obj);
366 }
367 }
368 }
369 }
370
371 template <class T>
372 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
373 if (count == 0) {
374 return;
375 }
376 int gc_state = _heap->gc_state();
377 if ((gc_state & ShenandoahHeap::MARKING) != 0) {
378 arraycopy_marking(src, dst, count);
379 } else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
380 arraycopy_evacuation(src, count);
381 } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
382 arraycopy_update(src, count);
383 }
384 }
385
386 template <class T>
387 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {
388 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
389 T* array = ShenandoahSATBBarrier ? dst : src;
390 if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {
391 arraycopy_work<T, false, false, true>(array, count);
392 }
393 }
394
395 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
396 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
397 }
398
399 template <class T>
400 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
401 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
402 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
403 ShenandoahEvacOOMScope oom_evac;
404 arraycopy_work<T, true, true, false>(src, count);
405 }
406 }
407
408 template <class T>
409 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
410 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
411 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
|
1 /*
2 * Copyright (c) 2015, 2022, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
27 #define SHARE_GC_SHENANDOAH_SHENANDOAHBARRIERSET_INLINE_HPP
28
29 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
30
31 #include "gc/shared/accessBarrierSupport.inline.hpp"
32 #include "gc/shared/cardTable.hpp"
33 #include "gc/shenandoah/shenandoahAsserts.hpp"
34 #include "gc/shenandoah/shenandoahCardTable.hpp"
35 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
36 #include "gc/shenandoah/shenandoahEvacOOMHandler.inline.hpp"
37 #include "gc/shenandoah/shenandoahForwarding.inline.hpp"
38 #include "gc/shenandoah/shenandoahGeneration.hpp"
39 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
40 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
41 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
42 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
43 #include "gc/shenandoah/mode/shenandoahMode.hpp"
44 #include "memory/iterator.inline.hpp"
45 #include "oops/oop.inline.hpp"
46
47 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null(oop p) {
48 return ShenandoahForwarding::get_forwardee(p);
49 }
50
51 inline oop ShenandoahBarrierSet::resolve_forwarded(oop p) {
52 if (p != nullptr) {
53 return resolve_forwarded_not_null(p);
54 } else {
55 return p;
56 }
57 }
58
59 inline oop ShenandoahBarrierSet::resolve_forwarded_not_null_mutator(oop p) {
60 return ShenandoahForwarding::get_forwardee_mutator(p);
61 }
62
63 template <class T>
64 inline oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, T* load_addr) {
65 assert(ShenandoahLoadRefBarrier, "should be enabled");
66 shenandoah_assert_in_cset(load_addr, obj);
67
68 oop fwd = resolve_forwarded_not_null_mutator(obj);
69 if (obj == fwd) {
70 assert(_heap->is_evacuation_in_progress(), "evac should be in progress");
71 Thread* const t = Thread::current();
72 ShenandoahEvacOOMScope scope(t);
73 fwd = _heap->evacuate_object(obj, t);
74 }
75
76 if (load_addr != nullptr && fwd != obj) {
77 // Since we are here and we know the load address, update the reference.
78 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
79 }
80
81 return fwd;
82 }
83
84 inline oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
85 if (!ShenandoahLoadRefBarrier) {
86 return obj;
87 }
88 if (_heap->has_forwarded_objects() && _heap->in_collection_set(obj)) {
89 // Subsumes null-check
90 assert(obj != nullptr, "cset check must have subsumed null-check");
91 oop fwd = resolve_forwarded_not_null(obj);
92 if (obj == fwd && _heap->is_evacuation_in_progress()) {
93 Thread* t = Thread::current();
94 ShenandoahEvacOOMScope oom_evac_scope(t);
95 return _heap->evacuate_object(obj, t);
96 }
97 return fwd;
98 }
99 return obj;
100 }
101
102 template <class T>
103 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
104 if (obj == nullptr) {
105 return nullptr;
106 }
107
108 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
109 if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
110 _heap->is_concurrent_weak_root_in_progress() &&
111 _heap->is_in_active_generation(obj) &&
112 !_heap->marking_context()->is_marked(obj)) {
113 return nullptr;
114 }
115
116 // Prevent resurrection of unreachable weak references.
117 if ((decorators & ON_WEAK_OOP_REF) != 0 &&
118 _heap->is_concurrent_weak_root_in_progress() &&
119 _heap->is_in_active_generation(obj) &&
120 !_heap->marking_context()->is_marked_strong(obj)) {
121 return nullptr;
122 }
123
124 // Allow runtime to see unreachable objects that are visited during concurrent class-unloading.
125 if ((decorators & AS_NO_KEEPALIVE) != 0 &&
126 _heap->is_concurrent_weak_root_in_progress() &&
127 !_heap->marking_context()->is_marked(obj)) {
128 return obj;
129 }
130
131 oop fwd = load_reference_barrier(obj);
132 if (load_addr != nullptr && fwd != obj) {
133 // Since we are here and we know the load address, update the reference.
134 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
135 }
136
137 return fwd;
138 }
139
140 inline void ShenandoahBarrierSet::enqueue(oop obj) {
141 assert(obj != nullptr, "checked by caller");
142 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
143
144 // Filter marked objects before hitting the SATB queues. The same predicate would
145 // be used by SATBMQ::filter to eliminate already marked objects downstream, but
146 // filtering here helps to avoid wasteful SATB queueing work to begin with.
147 if (!_heap->requires_marking(obj)) return;
148
149 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(Thread::current());
150 _satb_mark_queue_set.enqueue_known_active(queue, obj);
151 }
152
153 template <DecoratorSet decorators, typename T>
154 inline void ShenandoahBarrierSet::satb_barrier(T *field) {
155 // Uninitialized and no-keepalive stores do not need barrier.
156 if (HasDecorator<decorators, IS_DEST_UNINITIALIZED>::value ||
157 HasDecorator<decorators, AS_NO_KEEPALIVE>::value) {
158 return;
159 }
160
161 // Stores to weak/phantom require no barrier. The original references would
162 // have been enqueued in the SATB buffer by the load barrier if they were needed.
163 if (HasDecorator<decorators, ON_WEAK_OOP_REF>::value ||
164 HasDecorator<decorators, ON_PHANTOM_OOP_REF>::value) {
165 return;
166 }
167
168 if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
169 T heap_oop = RawAccess<>::oop_load(field);
170 if (!CompressedOops::is_null(heap_oop)) {
171 enqueue(CompressedOops::decode(heap_oop));
172 }
173 }
174 }
175
176 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
177 if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
178 enqueue(value);
179 }
180 }
181
182 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
183 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
184 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
185 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
186 if (!peek && !on_strong_oop_ref) {
187 satb_enqueue(value);
188 }
189 }
190
191 template <DecoratorSet decorators, typename T>
192 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
193 assert(ShenandoahCardBarrier, "Should have been checked by caller");
194 volatile CardTable::CardValue* byte = card_table()->byte_for(field);
195 *byte = CardTable::dirty_card_val();
196 }
197
198 template <typename T>
199 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
200 oop value = RawAccess<>::oop_load(addr);
201 value = load_reference_barrier(decorators, value, addr);
202 keep_alive_if_weak(decorators, value);
203 return value;
204 }
205
206 template <typename T>
207 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
208 oop res;
209 oop expected = compare_value;
210 do {
211 compare_value = expected;
212 res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
213 expected = res;
214 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
215
216 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
217 // because it must be the previous value.
218 res = load_reference_barrier(decorators, res, static_cast<T*>(nullptr));
219 satb_enqueue(res);
220 return res;
221 }
222
223 template <typename T>
224 inline oop ShenandoahBarrierSet::oop_xchg(DecoratorSet decorators, T* addr, oop new_value) {
225 oop previous = RawAccess<>::oop_atomic_xchg(addr, new_value);
226 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
227 // because it must be the previous value.
228 previous = load_reference_barrier<T>(decorators, previous, static_cast<T*>(nullptr));
229 satb_enqueue(previous);
230 return previous;
231 }
232
233 template <DecoratorSet decorators, typename BarrierSetT>
234 template <typename T>
235 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_not_in_heap(T* addr) {
236 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
237 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
238 return bs->oop_load(decorators, addr);
239 }
240
241 template <DecoratorSet decorators, typename BarrierSetT>
242 template <typename T>
243 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
244 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
245 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
246 return bs->oop_load(decorators, addr);
247 }
248
249 template <DecoratorSet decorators, typename BarrierSetT>
250 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
251 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
252 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
253 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
254 }
255
256 template <DecoratorSet decorators, typename BarrierSetT>
257 template <typename T>
258 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
259 shenandoah_assert_marked_if(nullptr, value,
260 !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
261 && !(ShenandoahHeap::heap()->active_generation()->is_young()
262 && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
263 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
264 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
265 bs->satb_barrier<decorators>(addr);
266 Raw::oop_store(addr, value);
267 }
268
269 template <DecoratorSet decorators, typename BarrierSetT>
270 template <typename T>
271 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
272 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
273 oop_store_common(addr, value);
274 }
275
276 template <DecoratorSet decorators, typename BarrierSetT>
277 template <typename T>
278 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
279 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
280 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
281
282 oop_store_common(addr, value);
283 if (ShenandoahCardBarrier) {
284 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
285 bs->write_ref_field_post<decorators>(addr);
286 }
287 }
288
289 template <DecoratorSet decorators, typename BarrierSetT>
290 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
291 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
292 }
293
294 template <DecoratorSet decorators, typename BarrierSetT>
295 template <typename T>
296 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
297 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
298 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
299 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
300 }
301
302 template <DecoratorSet decorators, typename BarrierSetT>
303 template <typename T>
304 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
305 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
306 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
307 oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
308 if (ShenandoahCardBarrier) {
309 bs->write_ref_field_post<decorators>(addr);
310 }
311 return result;
312 }
313
314 template <DecoratorSet decorators, typename BarrierSetT>
315 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
316 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
317 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
318 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
319 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
320 oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
321 if (ShenandoahCardBarrier) {
322 bs->write_ref_field_post<decorators>(addr);
323 }
324 return result;
325 }
326
327 template <DecoratorSet decorators, typename BarrierSetT>
328 template <typename T>
329 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
330 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
331 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
332 return bs->oop_xchg(decorators, addr, new_value);
333 }
334
335 template <DecoratorSet decorators, typename BarrierSetT>
336 template <typename T>
337 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
338 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
339 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
340 oop result = bs->oop_xchg(decorators, addr, new_value);
341 if (ShenandoahCardBarrier) {
342 bs->write_ref_field_post<decorators>(addr);
343 }
344 return result;
345 }
346
347 template <DecoratorSet decorators, typename BarrierSetT>
348 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
349 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
350 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
351 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
352 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
353 oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
354 if (ShenandoahCardBarrier) {
355 bs->write_ref_field_post<decorators>(addr);
356 }
357 return result;
358 }
359
360 // Clone barrier support
361 template <DecoratorSet decorators, typename BarrierSetT>
362 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
363 if (ShenandoahCloneBarrier) {
364 ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
365 }
366 Raw::clone(src, dst, size);
367 }
368
369 template <DecoratorSet decorators, typename BarrierSetT>
370 template <typename T>
371 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
372 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
373 size_t length) {
374 T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
375 T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
376
377 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
378 bs->arraycopy_barrier(src, dst, length);
379 bool result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
380 if (ShenandoahCardBarrier) {
381 bs->write_ref_array((HeapWord*) dst, length);
382 }
383 return result;
384 }
385
386 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
387 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
388 // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
389 // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
390 // may have forwarded objects. In this case, the `arraycopy_work` is first called with HAS_FWD=true and
391 // ENQUEUE=false.
392 assert(HAS_FWD == _heap->has_forwarded_objects() || _heap->is_concurrent_old_mark_in_progress(),
393 "Forwarded object status is sane");
394 // This function cannot be called to handle marking and evacuation at the same time (they operate on
395 // different sides of the copy).
396 assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
397
398 Thread* thread = Thread::current();
399 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
400 ShenandoahMarkingContext* ctx = _heap->marking_context();
401 const ShenandoahCollectionSet* const cset = _heap->collection_set();
402 T* end = src + count;
403 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
404 T o = RawAccess<>::oop_load(elem_ptr);
405 if (!CompressedOops::is_null(o)) {
406 oop obj = CompressedOops::decode_not_null(o);
407 if (HAS_FWD && cset->is_in(obj)) {
408 oop fwd = resolve_forwarded_not_null(obj);
409 if (EVAC && obj == fwd) {
410 fwd = _heap->evacuate_object(obj, thread);
411 }
412 shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
413 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
414 }
415 if (ENQUEUE && !ctx->is_marked_strong_or_old(obj)) {
416 _satb_mark_queue_set.enqueue_known_active(queue, obj);
417 }
418 }
419 }
420 }
421
422 template <class T>
423 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
424 if (count == 0) {
425 // No elements to copy, no need for barrier
426 return;
427 }
428
429 char gc_state = ShenandoahThreadLocalData::gc_state(Thread::current());
430 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
431 arraycopy_evacuation(src, count);
432 } else if ((gc_state & ShenandoahHeap::UPDATE_REFS) != 0) {
433 arraycopy_update(src, count);
434 }
435
436 if (_heap->mode()->is_generational()) {
437 assert(ShenandoahSATBBarrier, "Generational mode assumes SATB mode");
438 if ((gc_state & ShenandoahHeap::YOUNG_MARKING) != 0) {
439 arraycopy_marking(src, dst, count, false);
440 }
441 if ((gc_state & ShenandoahHeap::OLD_MARKING) != 0) {
442 arraycopy_marking(src, dst, count, true);
443 }
444 } else if ((gc_state & ShenandoahHeap::MARKING) != 0) {
445 arraycopy_marking(src, dst, count, false);
446 }
447 }
448
449 template <class T>
450 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count, bool is_old_marking) {
451 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
452 /*
453 * Note that an old-gen object is considered live if it is live at the start of OLD marking or if it is promoted
454 * following the start of OLD marking.
455 *
456 * 1. Every object promoted following the start of OLD marking will be above TAMS within its old-gen region
457 * 2. Every object live at the start of OLD marking will be referenced from a "root" or it will be referenced from
458 * another live OLD-gen object. With regards to old-gen, roots include stack locations and all of live young-gen.
459 * All root references to old-gen are identified during a bootstrap young collection. All references from other
460 * old-gen objects will be marked during the traversal of all old objects, or will be marked by the SATB barrier.
461 *
462 * During old-gen marking (which is interleaved with young-gen collections), call arraycopy_work() if:
463 *
464 * 1. The overwritten array resides in old-gen and it is below TAMS within its old-gen region
465 * 2. Do not call arraycopy_work for any array residing in young-gen because young-gen collection is idle at this time
466 *
467 * During young-gen marking, call arraycopy_work() if:
468 *
469 * 1. The overwritten array resides in young-gen and is below TAMS within its young-gen region
470 * 2. Additionally, if array resides in old-gen, regardless of its relationship to TAMS because this old-gen array
471 * may hold references to young-gen
472 */
473 if (ShenandoahSATBBarrier) {
474 T* array = dst;
475 HeapWord* array_addr = reinterpret_cast<HeapWord*>(array);
476 ShenandoahHeapRegion* r = _heap->heap_region_containing(array_addr);
477 if (is_old_marking) {
478 // Generational, old marking
479 assert(_heap->mode()->is_generational(), "Invariant");
480 if (r->is_old() && (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
481 arraycopy_work<T, false, false, true>(array, count);
482 }
483 } else if (_heap->mode()->is_generational()) {
484 // Generational, young marking
485 if (r->is_old() || (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
486 arraycopy_work<T, false, false, true>(array, count);
487 }
488 } else if (array_addr < _heap->marking_context()->top_at_mark_start(r)) {
489 // Non-generational, marking
490 arraycopy_work<T, false, false, true>(array, count);
491 }
492 }
493 }
494
495 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
496 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
497 }
498
499 template <class T>
500 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
501 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
502 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
503 ShenandoahEvacOOMScope oom_evac;
504 arraycopy_work<T, true, true, false>(src, count);
505 }
506 }
507
508 template <class T>
509 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
510 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
511 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
|