3333#include " utilities/align.hpp"
3434
3535G1CollectedHeap* G1AllocRegion::_g1h = nullptr ;
36- G1HeapRegion* G1AllocRegion::_dummy_region = nullptr ;
36+ Atomic< G1HeapRegion*> G1AllocRegion::_dummy_region;
3737
3838void G1AllocRegion::setup (G1CollectedHeap* g1h, G1HeapRegion* dummy_region) {
39- assert (_dummy_region == nullptr , " should be set once" );
39+ assert (_dummy_region. load_relaxed () == nullptr , " should be set once" );
4040 assert (dummy_region != nullptr , " pre-condition" );
4141 assert (dummy_region->free () == 0 , " pre-condition" );
4242
@@ -46,11 +46,11 @@ void G1AllocRegion::setup(G1CollectedHeap* g1h, G1HeapRegion* dummy_region) {
4646 assert (dummy_region->par_allocate (1 , 1 , &assert_tmp) == nullptr , " should fail" );
4747
4848 _g1h = g1h;
49- _dummy_region = dummy_region;
49+ _dummy_region. release_store ( dummy_region) ;
5050}
5151
5252size_t G1AllocRegion::fill_up_remaining_space (G1HeapRegion* alloc_region) {
53- assert (alloc_region != nullptr && alloc_region != _dummy_region,
53+ assert (alloc_region != nullptr && alloc_region != _dummy_region. load_relaxed () ,
5454 " pre-condition" );
5555 size_t result = 0 ;
5656
@@ -111,13 +111,13 @@ size_t G1AllocRegion::retire_internal(G1HeapRegion* alloc_region, bool fill_up)
111111}
112112
113113size_t G1AllocRegion::retire (bool fill_up) {
114- assert_alloc_region (_alloc_region != nullptr , " not initialized properly" );
114+ assert_alloc_region (_alloc_region. load_relaxed () != nullptr , " not initialized properly" );
115115
116116 size_t waste = 0 ;
117117
118118 trace (" retiring" );
119- G1HeapRegion* alloc_region = _alloc_region;
120- if (alloc_region != _dummy_region) {
119+ G1HeapRegion* alloc_region = _alloc_region. load_acquire () ;
120+ if (alloc_region != _dummy_region. load_relaxed () ) {
121121 waste = retire_internal (alloc_region, fill_up);
122122 reset_alloc_region ();
123123 }
@@ -127,7 +127,7 @@ size_t G1AllocRegion::retire(bool fill_up) {
127127}
128128
129129HeapWord* G1AllocRegion::new_alloc_region_and_allocate (size_t word_size) {
130- assert_alloc_region (_alloc_region == _dummy_region, " pre-condition" );
130+ assert_alloc_region (_alloc_region. load_relaxed () == _dummy_region. load_relaxed () , " pre-condition" );
131131
132132 trace (" attempting region allocation" );
133133 G1HeapRegion* new_alloc_region = allocate_new_region (word_size);
@@ -138,7 +138,6 @@ HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size) {
138138 HeapWord* result = new_alloc_region->allocate (word_size);
139139 assert_alloc_region (result != nullptr , " the allocation should succeeded" );
140140
141- OrderAccess::storestore ();
142141 // Note that we first perform the allocation and then we store the
143142 // region in _alloc_region. This is the reason why an active region
144143 // can never be empty.
@@ -154,16 +153,16 @@ HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size) {
154153
155154void G1AllocRegion::init () {
156155 trace (" initializing" );
157- assert_alloc_region (_alloc_region == nullptr , " pre-condition" );
158- assert_alloc_region (_dummy_region != nullptr , " should have been set" );
159- _alloc_region = _dummy_region;
156+ assert_alloc_region (_alloc_region. load_relaxed () == nullptr , " pre-condition" );
157+ assert_alloc_region (_dummy_region. load_relaxed () != nullptr , " should have been set" );
158+ _alloc_region. release_store ( _dummy_region. load_relaxed ()) ;
160159 _count = 0 ;
161160 trace (" initialized" );
162161}
163162
164163void G1AllocRegion::set (G1HeapRegion* alloc_region) {
165164 trace (" setting" );
166- assert_alloc_region (_alloc_region == _dummy_region && _count == 0 , " pre-condition" );
165+ assert_alloc_region (_alloc_region. load_relaxed () == _dummy_region. load_relaxed () && _count == 0 , " pre-condition" );
167166
168167 update_alloc_region (alloc_region);
169168 trace (" set" );
@@ -175,19 +174,19 @@ void G1AllocRegion::update_alloc_region(G1HeapRegion* alloc_region) {
175174 // maintain the "the alloc region cannot be empty" invariant.
176175 assert_alloc_region (alloc_region != nullptr && !alloc_region->is_empty (), " pre-condition" );
177176
178- _alloc_region = alloc_region;
177+ _alloc_region. release_store ( alloc_region) ;
179178 _count += 1 ;
180179 trace (" updated" );
181180}
182181
183182G1HeapRegion* G1AllocRegion::release () {
184183 trace (" releasing" );
185- G1HeapRegion* alloc_region = _alloc_region;
184+ G1HeapRegion* alloc_region = _alloc_region. load_acquire () ;
186185 retire (false /* fill_up */ );
187- assert_alloc_region (_alloc_region == _dummy_region, " post-condition of retire()" );
188- _alloc_region = nullptr ;
186+ assert_alloc_region (_alloc_region. load_relaxed () == _dummy_region. load_relaxed () , " post-condition of retire()" );
187+ _alloc_region. store_relaxed ( nullptr ) ;
189188 trace (" released" );
190- return (alloc_region == _dummy_region) ? nullptr : alloc_region;
189+ return (alloc_region == _dummy_region. load_relaxed () ) ? nullptr : alloc_region;
191190}
192191
193192#ifndef PRODUCT
@@ -211,12 +210,13 @@ void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_
211210
212211 out->print (" %s: %u " , _name, _count);
213212
214- if (_alloc_region == nullptr ) {
213+ G1HeapRegion* alloc_region = _alloc_region.load_acquire ();
214+ if (alloc_region == nullptr ) {
215215 out->print (" null" );
216- } else if (_alloc_region == _dummy_region) {
216+ } else if (alloc_region == _dummy_region. load_relaxed () ) {
217217 out->print (" DUMMY" );
218218 } else {
219- out->print (HR_FORMAT, HR_FORMAT_PARAMS (_alloc_region ));
219+ out->print (HR_FORMAT, HR_FORMAT_PARAMS (alloc_region ));
220220 }
221221
222222 out->print (" : %s" , str);
@@ -235,7 +235,7 @@ void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_
235235#endif // PRODUCT
236236
237237G1AllocRegion::G1AllocRegion (const char * name, uint node_index)
238- : _alloc_region(nullptr ),
238+ : _alloc_region(),
239239 _count(0 ),
240240 _name(name),
241241 _node_index(node_index)
@@ -250,7 +250,7 @@ void MutatorAllocRegion::retire_region(G1HeapRegion* alloc_region) {
250250}
251251
252252void MutatorAllocRegion::init () {
253- assert (_retained_alloc_region == nullptr , " Pre-condition" );
253+ assert (_retained_alloc_region. load_relaxed () == nullptr , " Pre-condition" );
254254 G1AllocRegion::init ();
255255 _wasted_bytes = 0 ;
256256}
@@ -261,8 +261,9 @@ bool MutatorAllocRegion::should_retain(G1HeapRegion* region) {
261261 return false ;
262262 }
263263
264- if (_retained_alloc_region != nullptr &&
265- free_bytes < _retained_alloc_region->free ()) {
264+ G1HeapRegion* retained_alloc_region = _retained_alloc_region.load_acquire ();
265+ if (retained_alloc_region != nullptr &&
266+ free_bytes < retained_alloc_region->free ()) {
266267 return false ;
267268 }
268269
@@ -278,10 +279,11 @@ size_t MutatorAllocRegion::retire(bool fill_up) {
278279 // free than the currently retained region.
279280 if (should_retain (current_region)) {
280281 trace (" mutator retained" );
281- if (_retained_alloc_region != nullptr ) {
282- waste = retire_internal (_retained_alloc_region, true );
282+ G1HeapRegion* retained_alloc_region = _retained_alloc_region.load_acquire ();
283+ if (retained_alloc_region != nullptr ) {
284+ waste = retire_internal (retained_alloc_region, true );
283285 }
284- _retained_alloc_region = current_region;
286+ _retained_alloc_region. release_store ( current_region) ;
285287 } else {
286288 waste = retire_internal (current_region, fill_up);
287289 }
@@ -300,7 +302,7 @@ size_t MutatorAllocRegion::used_in_alloc_regions() {
300302 used += hr->used ();
301303 }
302304
303- hr = _retained_alloc_region;
305+ hr = _retained_alloc_region. load_acquire () ;
304306 if (hr != nullptr ) {
305307 used += hr->used ();
306308 }
@@ -313,9 +315,10 @@ G1HeapRegion* MutatorAllocRegion::release() {
313315 // The retained alloc region must be retired and this must be
314316 // done after the above call to release the mutator alloc region,
315317 // since it might update the _retained_alloc_region member.
316- if (_retained_alloc_region != nullptr ) {
317- _wasted_bytes += retire_internal (_retained_alloc_region, false );
318- _retained_alloc_region = nullptr ;
318+ G1HeapRegion* retained_alloc_region = _retained_alloc_region.load_acquire ();
319+ if (retained_alloc_region != nullptr ) {
320+ _wasted_bytes += retire_internal (retained_alloc_region, false );
321+ _retained_alloc_region.store_relaxed (nullptr );
319322 }
320323 log_debug (gc, alloc, region)(" Mutator Allocation stats, regions: %u, wasted size: %zu%s (%4.1f%%)" ,
321324 count (),
0 commit comments