Skip to content

Commit c5159fc

Browse files
author
Kim Barrett
committed
8374328: Convert simple AtomicAccess uses in gc/shared to use Atomic<T>
Reviewed-by: dholmes, tschatzl
1 parent 904ba5f commit c5159fc

12 files changed

+62
-56
lines changed

src/hotspot/share/gc/shared/barrierSetNMethod.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@
3333
#include "memory/universe.hpp"
3434
#include "oops/access.inline.hpp"
3535
#include "oops/method.inline.hpp"
36+
#include "runtime/atomic.hpp"
3637
#include "runtime/frame.inline.hpp"
3738
#include "runtime/javaThread.hpp"
3839
#include "runtime/threads.hpp"
@@ -196,8 +197,8 @@ int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
196197
// Diagnostic option to force deoptimization 1 in 10 times. It is otherwise
197198
// a very rare event.
198199
if (DeoptimizeNMethodBarriersALot && !nm->is_osr_method()) {
199-
static volatile uint32_t counter=0;
200-
if (AtomicAccess::add(&counter, 1u) % 10 == 0) {
200+
static Atomic<uint32_t> counter{0};
201+
if (counter.add_then_fetch(1u) % 10 == 0) {
201202
may_enter = false;
202203
}
203204
}

src/hotspot/share/gc/shared/concurrentGCThread.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323
*/
2424

2525
#include "gc/shared/concurrentGCThread.hpp"
26-
#include "runtime/atomicAccess.hpp"
26+
#include "runtime/atomic.hpp"
2727
#include "runtime/init.hpp"
2828
#include "runtime/jniHandles.hpp"
2929
#include "runtime/mutexLocker.hpp"
@@ -48,7 +48,7 @@ void ConcurrentGCThread::run() {
4848

4949
// Signal thread has terminated
5050
MonitorLocker ml(Terminator_lock);
51-
AtomicAccess::release_store(&_has_terminated, true);
51+
_has_terminated.release_store(true);
5252
ml.notify_all();
5353
}
5454

@@ -57,21 +57,21 @@ void ConcurrentGCThread::stop() {
5757
assert(!has_terminated(), "Invalid state");
5858

5959
// Signal thread to terminate
60-
AtomicAccess::release_store_fence(&_should_terminate, true);
60+
_should_terminate.release_store_fence(true);
6161

6262
stop_service();
6363

6464
// Wait for thread to terminate
6565
MonitorLocker ml(Terminator_lock);
66-
while (!_has_terminated) {
66+
while (!_has_terminated.load_relaxed()) {
6767
ml.wait();
6868
}
6969
}
7070

7171
bool ConcurrentGCThread::should_terminate() const {
72-
return AtomicAccess::load_acquire(&_should_terminate);
72+
return _should_terminate.load_acquire();
7373
}
7474

7575
bool ConcurrentGCThread::has_terminated() const {
76-
return AtomicAccess::load_acquire(&_has_terminated);
76+
return _has_terminated.load_acquire();
7777
}

src/hotspot/share/gc/shared/concurrentGCThread.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,14 +25,15 @@
2525
#ifndef SHARE_GC_SHARED_CONCURRENTGCTHREAD_HPP
2626
#define SHARE_GC_SHARED_CONCURRENTGCTHREAD_HPP
2727

28+
#include "runtime/atomic.hpp"
2829
#include "runtime/javaThread.hpp"
2930
#include "runtime/nonJavaThread.hpp"
3031
#include "utilities/debug.hpp"
3132

3233
class ConcurrentGCThread: public NamedThread {
3334
private:
34-
volatile bool _should_terminate;
35-
volatile bool _has_terminated;
35+
Atomic<bool> _should_terminate;
36+
Atomic<bool> _has_terminated;
3637

3738
protected:
3839
void create_and_start(ThreadPriority prio = NearMaxPriority);

src/hotspot/share/gc/shared/gcLocker.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +28,7 @@
2828
#include "logging/log.hpp"
2929
#include "memory/resourceArea.hpp"
3030
#include "memory/universe.hpp"
31-
#include "runtime/atomicAccess.hpp"
31+
#include "runtime/atomic.hpp"
3232
#include "runtime/interfaceSupport.inline.hpp"
3333
#include "runtime/javaThread.inline.hpp"
3434
#include "runtime/safepoint.hpp"
@@ -60,16 +60,13 @@ class GCLockerTimingDebugLogger : public StackObj {
6060
};
6161

6262
Monitor* GCLocker::_lock;
63-
volatile bool GCLocker::_is_gc_request_pending;
63+
Atomic<bool> GCLocker::_is_gc_request_pending{false};
6464

65-
DEBUG_ONLY(uint64_t GCLocker::_verify_in_cr_count;)
65+
DEBUG_ONLY(Atomic<uint64_t> GCLocker::_verify_in_cr_count{0};)
6666

6767
void GCLocker::initialize() {
6868
assert(JNICritical_lock != nullptr, "inv");
6969
_lock = JNICritical_lock;
70-
_is_gc_request_pending = false;
71-
72-
DEBUG_ONLY(_verify_in_cr_count = 0;)
7370
}
7471

7572
bool GCLocker::is_active() {
@@ -84,11 +81,11 @@ bool GCLocker::is_active() {
8481
void GCLocker::block() {
8582
// _lock is held from the beginning of block() to the end of of unblock().
8683
_lock->lock();
87-
assert(AtomicAccess::load(&_is_gc_request_pending) == false, "precondition");
84+
assert(_is_gc_request_pending.load_relaxed() == false, "precondition");
8885

8986
GCLockerTimingDebugLogger logger("Thread blocked to start GC.");
9087

91-
AtomicAccess::store(&_is_gc_request_pending, true);
88+
_is_gc_request_pending.store_relaxed(true);
9289

9390
// The _is_gc_request_pending and _jni_active_critical (inside
9491
// in_critical_atomic()) variables form a Dekker duality. On the GC side, the
@@ -112,14 +109,14 @@ void GCLocker::block() {
112109
#ifdef ASSERT
113110
// Matching the storestore in GCLocker::exit.
114111
OrderAccess::loadload();
115-
assert(AtomicAccess::load(&_verify_in_cr_count) == 0, "inv");
112+
assert(_verify_in_cr_count.load_relaxed() == 0, "inv");
116113
#endif
117114
}
118115

119116
void GCLocker::unblock() {
120-
assert(AtomicAccess::load(&_is_gc_request_pending) == true, "precondition");
117+
assert(_is_gc_request_pending.load_relaxed() == true, "precondition");
121118

122-
AtomicAccess::store(&_is_gc_request_pending, false);
119+
_is_gc_request_pending.store_relaxed(false);
123120
_lock->unlock();
124121
}
125122

@@ -139,7 +136,7 @@ void GCLocker::enter_slow(JavaThread* current_thread) {
139136
// Same as fast path.
140137
OrderAccess::fence();
141138

142-
if (!AtomicAccess::load(&_is_gc_request_pending)) {
139+
if (!_is_gc_request_pending.load_relaxed()) {
143140
return;
144141
}
145142

src/hotspot/share/gc/shared/gcLocker.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
2727

2828
#include "gc/shared/gcCause.hpp"
2929
#include "memory/allStatic.hpp"
30+
#include "runtime/atomic.hpp"
3031
#include "runtime/mutex.hpp"
3132

3233
// GCLocker provides synchronization between the garbage collector (GC) and
@@ -43,11 +44,11 @@
4344

4445
class GCLocker: public AllStatic {
4546
static Monitor* _lock;
46-
static volatile bool _is_gc_request_pending;
47+
static Atomic<bool> _is_gc_request_pending;
4748

4849
#ifdef ASSERT
4950
// Debug-only: to track the number of java threads in critical-region.
50-
static uint64_t _verify_in_cr_count;
51+
static Atomic<uint64_t> _verify_in_cr_count;
5152
#endif
5253
static void enter_slow(JavaThread* current_thread);
5354

src/hotspot/share/gc/shared/gcLocker.inline.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,13 +38,13 @@ void GCLocker::enter(JavaThread* current_thread) {
3838
// Matching the fence in GCLocker::block.
3939
OrderAccess::fence();
4040

41-
if (AtomicAccess::load(&_is_gc_request_pending)) {
41+
if (_is_gc_request_pending.load_relaxed()) {
4242
current_thread->exit_critical();
4343
// slow-path
4444
enter_slow(current_thread);
4545
}
4646

47-
DEBUG_ONLY(AtomicAccess::add(&_verify_in_cr_count, (uint64_t)1);)
47+
DEBUG_ONLY(_verify_in_cr_count.add_then_fetch(1u);)
4848
} else {
4949
current_thread->enter_critical();
5050
}
@@ -55,7 +55,7 @@ void GCLocker::exit(JavaThread* current_thread) {
5555

5656
#ifdef ASSERT
5757
if (current_thread->in_last_critical()) {
58-
AtomicAccess::add(&_verify_in_cr_count, (uint64_t)-1);
58+
_verify_in_cr_count.sub_then_fetch(1u);
5959
// Matching the loadload in GCLocker::block.
6060
OrderAccess::storestore();
6161
}

src/hotspot/share/gc/shared/pretouchTask.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
2525
#include "gc/shared/gc_globals.hpp"
2626
#include "gc/shared/pretouchTask.hpp"
2727
#include "logging/log.hpp"
28-
#include "runtime/atomicAccess.hpp"
28+
#include "runtime/atomic.hpp"
2929
#include "runtime/globals.hpp"
3030
#include "runtime/os.hpp"
3131
#include "utilities/align.hpp"
@@ -52,11 +52,11 @@ size_t PretouchTask::chunk_size() {
5252

5353
void PretouchTask::work(uint worker_id) {
5454
while (true) {
55-
char* cur_start = AtomicAccess::load(&_cur_addr);
55+
char* cur_start = _cur_addr.load_relaxed();
5656
char* cur_end = cur_start + MIN2(_chunk_size, pointer_delta(_end_addr, cur_start, 1));
5757
if (cur_start >= cur_end) {
5858
break;
59-
} else if (cur_start == AtomicAccess::cmpxchg(&_cur_addr, cur_start, cur_end)) {
59+
} else if (cur_start == _cur_addr.compare_exchange(cur_start, cur_end)) {
6060
os::pretouch_memory(cur_start, cur_end, _page_size);
6161
} // Else attempt to claim chunk failed, so try again.
6262
}

src/hotspot/share/gc/shared/pretouchTask.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,9 +26,11 @@
2626
#define SHARE_GC_SHARED_PRETOUCH_HPP
2727

2828
#include "gc/shared/workerThread.hpp"
29+
#include "runtime/atomic.hpp"
30+
#include "utilities/globalDefinitions.hpp"
2931

3032
class PretouchTask : public WorkerTask {
31-
char* volatile _cur_addr;
33+
Atomic<char*> _cur_addr;
3234
char* const _end_addr;
3335
size_t _page_size;
3436
size_t _chunk_size;

src/hotspot/share/gc/shared/suspendibleThreadSet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,7 @@
3030

3131
uint SuspendibleThreadSet::_nthreads = 0;
3232
uint SuspendibleThreadSet::_nthreads_stopped = 0;
33-
volatile bool SuspendibleThreadSet::_suspend_all = false;
33+
Atomic<bool> SuspendibleThreadSet::_suspend_all{false};
3434
double SuspendibleThreadSet::_suspend_all_start = 0.0;
3535

3636
static Semaphore* _synchronize_wakeup = nullptr;
@@ -96,7 +96,7 @@ void SuspendibleThreadSet::synchronize() {
9696
{
9797
MonitorLocker ml(STS_lock, Mutex::_no_safepoint_check_flag);
9898
assert(!should_yield(), "Only one at a time");
99-
AtomicAccess::store(&_suspend_all, true);
99+
_suspend_all.store_relaxed(true);
100100
if (is_synchronized()) {
101101
return;
102102
}
@@ -127,6 +127,6 @@ void SuspendibleThreadSet::desynchronize() {
127127
MonitorLocker ml(STS_lock, Mutex::_no_safepoint_check_flag);
128128
assert(should_yield(), "STS not synchronizing");
129129
assert(is_synchronized(), "STS not synchronized");
130-
AtomicAccess::store(&_suspend_all, false);
130+
_suspend_all.store_relaxed(false);
131131
ml.notify_all();
132132
}

src/hotspot/share/gc/shared/suspendibleThreadSet.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
2626
#define SHARE_GC_SHARED_SUSPENDIBLETHREADSET_HPP
2727

2828
#include "memory/allocation.hpp"
29-
#include "runtime/atomicAccess.hpp"
29+
#include "runtime/atomic.hpp"
3030

3131
// A SuspendibleThreadSet is a set of threads that can be suspended.
3232
// A thread can join and later leave the set, and periodically yield.
@@ -43,7 +43,7 @@ class SuspendibleThreadSet : public AllStatic {
4343
private:
4444
static uint _nthreads;
4545
static uint _nthreads_stopped;
46-
static volatile bool _suspend_all;
46+
static Atomic<bool> _suspend_all;
4747
static double _suspend_all_start;
4848

4949
static bool is_synchronized();
@@ -59,7 +59,7 @@ class SuspendibleThreadSet : public AllStatic {
5959

6060
public:
6161
// Returns true if an suspension is in progress.
62-
static bool should_yield() { return AtomicAccess::load(&_suspend_all); }
62+
static bool should_yield() { return _suspend_all.load_relaxed(); }
6363

6464
// Suspends the current thread if a suspension is in progress.
6565
static void yield() {

0 commit comments

Comments
 (0)