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) {
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 }
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::keep_alive_if_weak(DecoratorSet decorators, oop value) {
168 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
169 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
170 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
171 if (!peek && !on_strong_oop_ref) {
172 satb_enqueue(value);
173 }
174 }
175
176 template <typename T>
177 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
178 oop value = RawAccess<>::oop_load(addr);
179 value = load_reference_barrier(decorators, value, addr);
180 keep_alive_if_weak(decorators, value);
181 return value;
182 }
183
184 template <typename T>
185 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
186 oop res;
187 oop expected = compare_value;
188 do {
189 compare_value = expected;
190 res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
191 expected = res;
192 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
193
194 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
195 // because it must be the previous value.
217 }
218
219 template <DecoratorSet decorators, typename BarrierSetT>
220 template <typename T>
221 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_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 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
229 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
230 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
231 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
232 }
233
234 template <DecoratorSet decorators, typename BarrierSetT>
235 template <typename T>
236 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
237 shenandoah_assert_marked_if(nullptr, value, !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress());
238 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
239 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
240 bs->satb_barrier<decorators>(addr);
241 Raw::oop_store(addr, value);
242 }
243
244 template <DecoratorSet decorators, typename BarrierSetT>
245 template <typename T>
246 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
247 oop_store_common(addr, value);
248 }
249
250 template <DecoratorSet decorators, typename BarrierSetT>
251 template <typename T>
252 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
253 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
254 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
255
256 oop_store_common(addr, value);
257 }
258
259 template <DecoratorSet decorators, typename BarrierSetT>
260 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
261 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
262 }
263
264 template <DecoratorSet decorators, typename BarrierSetT>
265 template <typename T>
266 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
267 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
268 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
269 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
270 }
271
272 template <DecoratorSet decorators, typename BarrierSetT>
273 template <typename T>
274 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap(T* addr, oop compare_value, oop new_value) {
275 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
276 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
277 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
278 }
279
280 template <DecoratorSet decorators, typename BarrierSetT>
281 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
282 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
283 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
284 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
285 return bs->oop_cmpxchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), compare_value, new_value);
286 }
287
288 template <DecoratorSet decorators, typename BarrierSetT>
289 template <typename T>
290 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
291 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
292 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
293 return bs->oop_xchg(decorators, addr, new_value);
294 }
295
296 template <DecoratorSet decorators, typename BarrierSetT>
297 template <typename T>
298 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap(T* addr, oop new_value) {
299 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
300 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
301 return bs->oop_xchg(decorators, addr, new_value);
302 }
303
304 template <DecoratorSet decorators, typename BarrierSetT>
305 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
306 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
307 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
308 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
309 return bs->oop_xchg(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset), new_value);
310 }
311
312 // Clone barrier support
313 template <DecoratorSet decorators, typename BarrierSetT>
314 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
315 if (ShenandoahCloneBarrier) {
316 ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
317 }
318 Raw::clone(src, dst, size);
319 }
320
321 template <DecoratorSet decorators, typename BarrierSetT>
322 template <typename T>
323 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
324 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
325 size_t length) {
326 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
327 bs->arraycopy_barrier(arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw),
328 arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw),
329 length);
330 return Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
331 }
332
333 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
334 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
335 assert(HAS_FWD == _heap->has_forwarded_objects(), "Forwarded object status is sane");
336
337 Thread* thread = Thread::current();
338 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
339 ShenandoahMarkingContext* ctx = _heap->marking_context();
340 const ShenandoahCollectionSet* const cset = _heap->collection_set();
341 T* end = src + count;
342 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
343 T o = RawAccess<>::oop_load(elem_ptr);
344 if (!CompressedOops::is_null(o)) {
345 oop obj = CompressedOops::decode_not_null(o);
346 if (HAS_FWD && cset->is_in(obj)) {
347 oop fwd = resolve_forwarded_not_null(obj);
348 if (EVAC && obj == fwd) {
349 fwd = _heap->evacuate_object(obj, thread);
350 }
351 shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
352 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
353 obj = fwd;
354 }
355 if (ENQUEUE && !ctx->is_marked_strong(obj)) {
356 _satb_mark_queue_set.enqueue_known_active(queue, obj);
357 }
358 }
359 }
360 }
361
362 template <class T>
363 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
364 if (count == 0) {
365 return;
366 }
367 int gc_state = _heap->gc_state();
368 if ((gc_state & ShenandoahHeap::MARKING) != 0) {
369 arraycopy_marking(src, dst, count);
370 } else if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
371 arraycopy_evacuation(src, count);
372 } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
373 arraycopy_update(src, count);
374 }
375 }
376
377 template <class T>
378 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count) {
379 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
380 T* array = ShenandoahSATBBarrier ? dst : src;
381 if (!_heap->marking_context()->allocated_after_mark_start(reinterpret_cast<HeapWord*>(array))) {
382 arraycopy_work<T, false, false, true>(array, count);
383 }
384 }
385
386 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
387 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
388 }
389
390 template <class T>
391 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
392 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
393 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
394 ShenandoahEvacOOMScope oom_evac;
395 arraycopy_work<T, true, true, false>(src, count);
396 }
397 }
398
399 template <class T>
400 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
401 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
402 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) {
92 oop fwd = resolve_forwarded_not_null(obj);
93 if (obj == fwd && _heap->is_evacuation_in_progress()) {
94 Thread* t = Thread::current();
95 ShenandoahEvacOOMScope oom_evac_scope(t);
96 return _heap->evacuate_object(obj, t);
97 }
98 return fwd;
99 }
100 return obj;
101 }
102
103 template <class T>
104 inline oop ShenandoahBarrierSet::load_reference_barrier(DecoratorSet decorators, oop obj, T* load_addr) {
105 if (obj == nullptr) {
106 return nullptr;
107 }
108
109 // Prevent resurrection of unreachable phantom (i.e. weak-native) references.
110 if ((decorators & ON_PHANTOM_OOP_REF) != 0 &&
111 _heap->is_concurrent_weak_root_in_progress() &&
112 _heap->is_in_active_generation(obj) &&
113 !_heap->marking_context()->is_marked(obj)) {
114 return nullptr;
115 }
116
117 // Prevent resurrection of unreachable weak references.
118 if ((decorators & ON_WEAK_OOP_REF) != 0 &&
119 _heap->is_concurrent_weak_root_in_progress() &&
120 _heap->is_in_active_generation(obj) &&
121 !_heap->marking_context()->is_marked_strong(obj)) {
122 return nullptr;
123 }
124
125 // Prevent resurrection of unreachable objects that are visited during
126 // concurrent class-unloading.
127 if ((decorators & AS_NO_KEEPALIVE) != 0 &&
128 _heap->is_evacuation_in_progress() &&
129 !_heap->marking_context()->is_marked(obj)) {
130 return obj;
131 }
132
133 oop fwd = load_reference_barrier(obj);
134 if (load_addr != nullptr && fwd != obj) {
135 // Since we are here and we know the load address, update the reference.
136 ShenandoahHeap::atomic_update_oop(fwd, load_addr, obj);
137 }
138
139 return fwd;
140 }
164 enqueue(CompressedOops::decode(heap_oop));
165 }
166 }
167 }
168
169 inline void ShenandoahBarrierSet::satb_enqueue(oop value) {
170 if (value != nullptr && ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
171 enqueue(value);
172 }
173 }
174
175 inline void ShenandoahBarrierSet::keep_alive_if_weak(DecoratorSet decorators, oop value) {
176 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "Reference strength must be known");
177 const bool on_strong_oop_ref = (decorators & ON_STRONG_OOP_REF) != 0;
178 const bool peek = (decorators & AS_NO_KEEPALIVE) != 0;
179 if (!peek && !on_strong_oop_ref) {
180 satb_enqueue(value);
181 }
182 }
183
184 template <DecoratorSet decorators, typename T>
185 inline void ShenandoahBarrierSet::write_ref_field_post(T* field) {
186 assert(ShenandoahCardBarrier, "Should have been checked by caller");
187 volatile CardTable::CardValue* byte = card_table()->byte_for(field);
188 *byte = CardTable::dirty_card_val();
189 }
190
191 template <typename T>
192 inline oop ShenandoahBarrierSet::oop_load(DecoratorSet decorators, T* addr) {
193 oop value = RawAccess<>::oop_load(addr);
194 value = load_reference_barrier(decorators, value, addr);
195 keep_alive_if_weak(decorators, value);
196 return value;
197 }
198
199 template <typename T>
200 inline oop ShenandoahBarrierSet::oop_cmpxchg(DecoratorSet decorators, T* addr, oop compare_value, oop new_value) {
201 oop res;
202 oop expected = compare_value;
203 do {
204 compare_value = expected;
205 res = RawAccess<>::oop_atomic_cmpxchg(addr, compare_value, new_value);
206 expected = res;
207 } while ((compare_value != expected) && (resolve_forwarded(compare_value) == resolve_forwarded(expected)));
208
209 // Note: We don't need a keep-alive-barrier here. We already enqueue any loaded reference for SATB anyway,
210 // because it must be the previous value.
232 }
233
234 template <DecoratorSet decorators, typename BarrierSetT>
235 template <typename T>
236 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap(T* addr) {
237 assert((decorators & ON_UNKNOWN_OOP_REF) == 0, "must be absent");
238 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
239 return bs->oop_load(decorators, addr);
240 }
241
242 template <DecoratorSet decorators, typename BarrierSetT>
243 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_load_in_heap_at(oop base, ptrdiff_t offset) {
244 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
245 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
246 return bs->oop_load(resolved_decorators, AccessInternal::oop_field_addr<decorators>(base, offset));
247 }
248
249 template <DecoratorSet decorators, typename BarrierSetT>
250 template <typename T>
251 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_common(T* addr, oop value) {
252 shenandoah_assert_marked_if(nullptr, value,
253 !CompressedOops::is_null(value) && ShenandoahHeap::heap()->is_evacuation_in_progress()
254 && !(ShenandoahHeap::heap()->active_generation()->is_young()
255 && ShenandoahHeap::heap()->heap_region_containing(value)->is_old()));
256 shenandoah_assert_not_in_cset_if(addr, value, value != nullptr && !ShenandoahHeap::heap()->cancelled_gc());
257 ShenandoahBarrierSet* const bs = ShenandoahBarrierSet::barrier_set();
258 bs->satb_barrier<decorators>(addr);
259 Raw::oop_store(addr, value);
260 }
261
262 template <DecoratorSet decorators, typename BarrierSetT>
263 template <typename T>
264 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_not_in_heap(T* addr, oop value) {
265 oop_store_common(addr, value);
266 }
267
268 template <DecoratorSet decorators, typename BarrierSetT>
269 template <typename T>
270 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap(T* addr, oop value) {
271 shenandoah_assert_not_in_cset_loc_except(addr, ShenandoahHeap::heap()->cancelled_gc());
272 shenandoah_assert_not_forwarded_except (addr, value, value == nullptr || ShenandoahHeap::heap()->cancelled_gc() || !ShenandoahHeap::heap()->is_concurrent_mark_in_progress());
273
274 oop_store_common(addr, value);
275 if (ShenandoahCardBarrier) {
276 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
277 bs->write_ref_field_post<decorators>(addr);
278 }
279 }
280
281 template <DecoratorSet decorators, typename BarrierSetT>
282 inline void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_store_in_heap_at(oop base, ptrdiff_t offset, oop value) {
283 oop_store_in_heap(AccessInternal::oop_field_addr<decorators>(base, offset), value);
284 }
285
286 template <DecoratorSet decorators, typename BarrierSetT>
287 template <typename T>
288 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_not_in_heap(T* addr, oop compare_value, oop new_value) {
289 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
290 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
291 return bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
292 }
293
294 template <DecoratorSet decorators, typename BarrierSetT>
295 template <typename T>
296 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_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 oop result = bs->oop_cmpxchg(decorators, addr, compare_value, new_value);
300 if (ShenandoahCardBarrier) {
301 bs->write_ref_field_post<decorators>(addr);
302 }
303 return result;
304 }
305
306 template <DecoratorSet decorators, typename BarrierSetT>
307 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_cmpxchg_in_heap_at(oop base, ptrdiff_t offset, oop compare_value, oop new_value) {
308 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
309 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
310 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
311 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
312 oop result = bs->oop_cmpxchg(resolved_decorators, addr, compare_value, new_value);
313 if (ShenandoahCardBarrier) {
314 bs->write_ref_field_post<decorators>(addr);
315 }
316 return result;
317 }
318
319 template <DecoratorSet decorators, typename BarrierSetT>
320 template <typename T>
321 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_not_in_heap(T* addr, oop new_value) {
322 assert((decorators & (AS_NO_KEEPALIVE | ON_UNKNOWN_OOP_REF)) == 0, "must be absent");
323 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
324 return bs->oop_xchg(decorators, addr, new_value);
325 }
326
327 template <DecoratorSet decorators, typename BarrierSetT>
328 template <typename T>
329 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_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 oop result = bs->oop_xchg(decorators, addr, new_value);
333 if (ShenandoahCardBarrier) {
334 bs->write_ref_field_post<decorators>(addr);
335 }
336 return result;
337 }
338
339 template <DecoratorSet decorators, typename BarrierSetT>
340 inline oop ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_atomic_xchg_in_heap_at(oop base, ptrdiff_t offset, oop new_value) {
341 assert((decorators & AS_NO_KEEPALIVE) == 0, "must be absent");
342 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
343 DecoratorSet resolved_decorators = AccessBarrierSupport::resolve_possibly_unknown_oop_ref_strength<decorators>(base, offset);
344 auto addr = AccessInternal::oop_field_addr<decorators>(base, offset);
345 oop result = bs->oop_xchg(resolved_decorators, addr, new_value);
346 if (ShenandoahCardBarrier) {
347 bs->write_ref_field_post<decorators>(addr);
348 }
349 return result;
350 }
351
352 // Clone barrier support
353 template <DecoratorSet decorators, typename BarrierSetT>
354 void ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::clone_in_heap(oop src, oop dst, size_t size) {
355 if (ShenandoahCloneBarrier) {
356 ShenandoahBarrierSet::barrier_set()->clone_barrier_runtime(src);
357 }
358 Raw::clone(src, dst, size);
359 }
360
361 template <DecoratorSet decorators, typename BarrierSetT>
362 template <typename T>
363 bool ShenandoahBarrierSet::AccessBarrier<decorators, BarrierSetT>::oop_arraycopy_in_heap(arrayOop src_obj, size_t src_offset_in_bytes, T* src_raw,
364 arrayOop dst_obj, size_t dst_offset_in_bytes, T* dst_raw,
365 size_t length) {
366 T* src = arrayOopDesc::obj_offset_to_raw(src_obj, src_offset_in_bytes, src_raw);
367 T* dst = arrayOopDesc::obj_offset_to_raw(dst_obj, dst_offset_in_bytes, dst_raw);
368
369 ShenandoahBarrierSet* bs = ShenandoahBarrierSet::barrier_set();
370 bs->arraycopy_barrier(src, dst, length);
371 bool result = Raw::oop_arraycopy_in_heap(src_obj, src_offset_in_bytes, src_raw, dst_obj, dst_offset_in_bytes, dst_raw, length);
372 if (ShenandoahCardBarrier) {
373 bs->write_ref_array((HeapWord*) dst, length);
374 }
375 return result;
376 }
377
378 template <class T, bool HAS_FWD, bool EVAC, bool ENQUEUE>
379 void ShenandoahBarrierSet::arraycopy_work(T* src, size_t count) {
380 // Young cycles are allowed to run when old marking is in progress. When old marking is in progress,
381 // this barrier will be called with ENQUEUE=true and HAS_FWD=false, even though the young generation
382 // may have forwarded objects. In this case, the `arraycopy_work` is first called with HAS_FWD=true and
383 // ENQUEUE=false.
384 assert(HAS_FWD == _heap->has_forwarded_objects() || (_heap->gc_state() & ShenandoahHeap::OLD_MARKING) != 0,
385 "Forwarded object status is sane");
386 // This function cannot be called to handle marking and evacuation at the same time (they operate on
387 // different sides of the copy).
388 assert((HAS_FWD || EVAC) != ENQUEUE, "Cannot evacuate and mark both sides of copy.");
389
390 Thread* thread = Thread::current();
391 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
392 ShenandoahMarkingContext* ctx = _heap->marking_context();
393 const ShenandoahCollectionSet* const cset = _heap->collection_set();
394 T* end = src + count;
395 for (T* elem_ptr = src; elem_ptr < end; elem_ptr++) {
396 T o = RawAccess<>::oop_load(elem_ptr);
397 if (!CompressedOops::is_null(o)) {
398 oop obj = CompressedOops::decode_not_null(o);
399 if (HAS_FWD && cset->is_in(obj)) {
400 oop fwd = resolve_forwarded_not_null(obj);
401 if (EVAC && obj == fwd) {
402 fwd = _heap->evacuate_object(obj, thread);
403 }
404 shenandoah_assert_forwarded_except(elem_ptr, obj, _heap->cancelled_gc());
405 ShenandoahHeap::atomic_update_oop(fwd, elem_ptr, o);
406 }
407 if (ENQUEUE && !ctx->is_marked_strong_or_old(obj)) {
408 _satb_mark_queue_set.enqueue_known_active(queue, obj);
409 }
410 }
411 }
412 }
413
414 template <class T>
415 void ShenandoahBarrierSet::arraycopy_barrier(T* src, T* dst, size_t count) {
416 if (count == 0) {
417 return;
418 }
419 int gc_state = _heap->gc_state();
420 if ((gc_state & ShenandoahHeap::YOUNG_MARKING) != 0) {
421 arraycopy_marking(src, dst, count, false);
422 return;
423 }
424
425 if ((gc_state & ShenandoahHeap::EVACUATION) != 0) {
426 arraycopy_evacuation(src, count);
427 } else if ((gc_state & ShenandoahHeap::UPDATEREFS) != 0) {
428 arraycopy_update(src, count);
429 }
430
431 if (_heap->mode()->is_generational()) {
432 assert(ShenandoahSATBBarrier, "Generational mode assumes SATB mode");
433 if ((gc_state & ShenandoahHeap::OLD_MARKING) != 0) {
434 // Note that we can't do the arraycopy marking using the 'src' array when
435 // SATB mode is enabled (so we can't do this as part of the iteration for
436 // evacuation or update references).
437 arraycopy_marking(src, dst, count, true);
438 }
439 }
440 }
441
442 template <class T>
443 void ShenandoahBarrierSet::arraycopy_marking(T* src, T* dst, size_t count, bool is_old_marking) {
444 assert(_heap->is_concurrent_mark_in_progress(), "only during marking");
445 /*
446 * Note that an old-gen object is considered live if it is live at the start of OLD marking or if it is promoted
447 * following the start of OLD marking.
448 *
449 * 1. Every object promoted following the start of OLD marking will be above TAMS within its old-gen region
450 * 2. Every object live at the start of OLD marking will be referenced from a "root" or it will be referenced from
451 * another live OLD-gen object. With regards to old-gen, roots include stack locations and all of live young-gen.
452 * All root references to old-gen are identified during a bootstrap young collection. All references from other
453 * old-gen objects will be marked during the traversal of all old objects, or will be marked by the SATB barrier.
454 *
455 * During old-gen marking (which is interleaved with young-gen collections), call arraycopy_work() if:
456 *
457 * 1. The overwritten array resides in old-gen and it is below TAMS within its old-gen region
458 * 2. Do not call arraycopy_work for any array residing in young-gen because young-gen collection is idle at this time
459 *
460 * During young-gen marking, call arraycopy_work() if:
461 *
462 * 1. The overwritten array resides in young-gen and is below TAMS within its young-gen region
463 * 2. Additionally, if array resides in old-gen, regardless of its relationship to TAMS because this old-gen array
464 * may hold references to young-gen
465 */
466 if (ShenandoahSATBBarrier) {
467 T* array = dst;
468 HeapWord* array_addr = reinterpret_cast<HeapWord*>(array);
469 ShenandoahHeapRegion* r = _heap->heap_region_containing(array_addr);
470 if (is_old_marking) {
471 // Generational, old marking
472 assert(_heap->mode()->is_generational(), "Invariant");
473 if (r->is_old() && (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
474 arraycopy_work<T, false, false, true>(array, count);
475 }
476 } else if (_heap->mode()->is_generational()) {
477 // Generational, young marking
478 if (r->is_old() || (array_addr < _heap->marking_context()->top_at_mark_start(r))) {
479 arraycopy_work<T, false, false, true>(array, count);
480 }
481 } else if (array_addr < _heap->marking_context()->top_at_mark_start(r)) {
482 // Non-generational, marking
483 arraycopy_work<T, false, false, true>(array, count);
484 }
485 }
486 }
487
488 inline bool ShenandoahBarrierSet::need_bulk_update(HeapWord* ary) {
489 return ary < _heap->heap_region_containing(ary)->get_update_watermark();
490 }
491
492 template <class T>
493 void ShenandoahBarrierSet::arraycopy_evacuation(T* src, size_t count) {
494 assert(_heap->is_evacuation_in_progress(), "only during evacuation");
495 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
496 ShenandoahEvacOOMScope oom_evac;
497 arraycopy_work<T, true, true, false>(src, count);
498 }
499 }
500
501 template <class T>
502 void ShenandoahBarrierSet::arraycopy_update(T* src, size_t count) {
503 assert(_heap->is_update_refs_in_progress(), "only during update-refs");
504 if (need_bulk_update(reinterpret_cast<HeapWord*>(src))) {
|