Unify uses of gcc atomics (#1704)

This commit is contained in:
Andrei Pangin
2026-03-20 16:14:48 +00:00
committed by GitHub
parent cc0eab1789
commit 8d653dd5e0
13 changed files with 39 additions and 41 deletions

View File

@@ -27,28 +27,24 @@ typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
static inline u64 atomicInc(volatile u64& var, u64 increment = 1) {
return __sync_fetch_and_add(&var, increment);
template<typename T>
static inline T atomicInc(T& var, T increment = 1) {
return __atomic_fetch_add(&var, increment, __ATOMIC_ACQ_REL);
}
static inline int atomicInc(volatile u32& var, int increment = 1) {
return __sync_fetch_and_add(&var, increment);
template<typename T>
static inline T atomicDec(T& var, T decrement = 1) {
return __atomic_fetch_sub(&var, decrement, __ATOMIC_ACQ_REL);
}
static inline int atomicInc(volatile int& var, int increment = 1) {
return __sync_fetch_and_add(&var, increment);
}
static inline u64 loadAcquire(u64& var) {
template<typename T>
static inline T loadAcquire(T& var) {
return __atomic_load_n(&var, __ATOMIC_ACQUIRE);
}
static inline u32 loadAcquire(u32& var) {
return __atomic_load_n(&var, __ATOMIC_ACQUIRE);
}
static inline void storeRelease(u64& var, u64 value) {
__atomic_store_n(&var, value, __ATOMIC_RELEASE);
template<typename T, typename U>
static inline void storeRelease(T& var, U value) {
__atomic_store_n(&var, static_cast<T>(value), __ATOMIC_RELEASE);
}
#if defined(__x86_64__) || defined(__i386__)

View File

@@ -250,7 +250,7 @@ u32 CallTraceStorage::put(int num_frames, ASGCT_CallFrame* frames, u64 counter)
if (table->incSize() == capacity * 3 / 4) {
LongHashTable* new_table = LongHashTable::allocate(table, capacity * 2);
if (new_table != NULL) {
__atomic_store_n(&_current_table, new_table, __ATOMIC_RELEASE);
storeRelease(_current_table, new_table);
}
}

View File

@@ -26,11 +26,11 @@ struct CallTraceSample {
u64 counter;
CallTrace* acquireTrace() {
return __atomic_load_n(&trace, __ATOMIC_ACQUIRE);
return loadAcquire(trace);
}
void setTrace(CallTrace* value) {
return __atomic_store_n(&trace, value, __ATOMIC_RELEASE);
storeRelease(trace, value);
}
CallTraceSample& operator+=(const CallTraceSample& s) {

View File

@@ -7,6 +7,7 @@
#define _CODECACHE_H
#include <jvmti.h>
#include "arch.h"
#define NO_MIN_ADDRESS ((const void*)-1)
@@ -230,7 +231,7 @@ class CodeCacheArray {
}
int count() {
return __atomic_load_n(&_count, __ATOMIC_ACQUIRE);
return loadAcquire(_count);
}
size_t usedMemory() {
@@ -238,10 +239,10 @@ class CodeCacheArray {
}
void add(CodeCache* lib) {
int index = __atomic_load_n(&_count, __ATOMIC_ACQUIRE);
int index = loadAcquire(_count);
_libs[index] = lib;
_used_memory += lib->usedMemory();
__atomic_store_n(&_count, index + 1, __ATOMIC_RELEASE);
storeRelease(_count, index + 1);
}
};

View File

@@ -42,14 +42,14 @@ static int pthread_setspecific_hook(pthread_key_t key, const void* value) {
}
void CpuEngine::onThreadStart() {
CpuEngine* current = __atomic_load_n(&_current, __ATOMIC_ACQUIRE);
CpuEngine* current = loadAcquire(_current);
if (current != NULL) {
current->createForThread(OS::threadId());
}
}
void CpuEngine::onThreadEnd() {
CpuEngine* current = __atomic_load_n(&_current, __ATOMIC_ACQUIRE);
CpuEngine* current = loadAcquire(_current);
if (current != NULL) {
current->destroyForThread(OS::threadId());
}
@@ -80,12 +80,12 @@ bool CpuEngine::setupThreadHook() {
void CpuEngine::enableThreadHook() {
*_pthread_entry = (void*)pthread_setspecific_hook;
__atomic_store_n(&_current, this, __ATOMIC_RELEASE);
storeRelease(_current, this);
}
void CpuEngine::disableThreadHook() {
*_pthread_entry = (void*)pthread_setspecific;
__atomic_store_n(&_current, NULL, __ATOMIC_RELEASE);
storeRelease(_current, nullptr);
}
bool CpuEngine::isResourceLimit(int err) {

View File

@@ -553,7 +553,7 @@ class Recording {
void flush(Buffer* buf) {
ssize_t result = write(_in_memory ? _memfd : _fd, buf->data(), buf->offset());
if (result > 0) {
atomicInc(_bytes_written, result);
atomicInc(_bytes_written, (u64)result);
}
buf->reset();
}
@@ -1396,9 +1396,9 @@ Error FlightRecorder::startMasterRecording(Arguments& args, const char* filename
int event_mask = args.eventMask() |
((args._jfr_options ^ JFR_SYNC_OPTS) << EVENT_MASK_SIZE);
__atomic_store_n(&_jfr_starting, true, __ATOMIC_RELEASE);
storeRelease(_jfr_starting, true);
env->CallStaticVoidMethod(_jfr_sync_class, _start_method, jfilename, jsettings, event_mask);
__atomic_store_n(&_jfr_starting, false, __ATOMIC_RELEASE);
storeRelease(_jfr_starting, false);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
@@ -1489,5 +1489,5 @@ void FlightRecorder::recordLog(LogLevel level, const char* message, size_t len)
}
bool FlightRecorder::isJfrStarting() {
return __atomic_load_n(&_jfr_starting, __ATOMIC_ACQUIRE);
return loadAcquire(_jfr_starting);
}

View File

@@ -38,7 +38,7 @@ class J9VMThread {
}
void setOverflowMark() {
__atomic_store_n(&_overflow_mark, (uintptr_t)-1, __ATOMIC_RELEASE);
storeRelease(_overflow_mark, (uintptr_t)-1);
}
};
@@ -47,7 +47,7 @@ pthread_t J9StackTraces::_thread = 0;
int J9StackTraces::_max_stack_depth;
int J9StackTraces::_pipe[2];
static JNIEnv* _self_env = NULL;
static JNIEnv* _self_env = nullptr;
Error J9StackTraces::start(Arguments& args) {
@@ -78,7 +78,7 @@ void J9StackTraces::stop() {
void J9StackTraces::timerLoop() {
JNIEnv* jni = VM::attachThread("Async-profiler Sampler");
__atomic_store_n(&_self_env, jni, __ATOMIC_RELEASE);
storeRelease(_self_env, jni);
jni->PushLocalFrame(64);
@@ -145,13 +145,13 @@ void J9StackTraces::timerLoop() {
free(jvmti_frames);
free(frames);
__atomic_store_n(&_self_env, NULL, __ATOMIC_RELEASE);
storeRelease(_self_env, nullptr);
VM::detachThread();
}
void J9StackTraces::checkpoint(u64 counter, J9StackTraceNotification* notif) {
JNIEnv* self_env = __atomic_load_n(&_self_env, __ATOMIC_ACQUIRE);
if (self_env == NULL) {
JNIEnv* self_env = loadAcquire(_self_env);
if (self_env == nullptr) {
// Sampler thread is not ready
return;
}

View File

@@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "demangle.h"
#include "dictionary.h"
#include "index.h"

View File

@@ -573,7 +573,7 @@ void* Profiler::dlopen_hook(const char* filename, int flags) {
void Profiler::switchLibraryTrap(bool enable) {
if (_dlopen_entry != NULL) {
void* impl = enable ? (void*)dlopen_hook : (void*)dlopen;
__atomic_store_n(_dlopen_entry, impl, __ATOMIC_RELEASE);
storeRelease(*_dlopen_entry, impl);
}
}

View File

@@ -102,7 +102,7 @@ void ThreadFilter::remove(int thread_id) {
u32 bit = 1 << (thread_id & 0x1f);
if (__sync_fetch_and_and(&word(b, thread_id), ~bit) & bit) {
atomicInc(_size, -1);
atomicDec(_size);
}
}

View File

@@ -25,8 +25,8 @@
const int ARGUMENTS_ERROR = 100;
const int COMMAND_ERROR = 200;
static constexpr u32 JMETHOD_ID_LIMIT = 1024 * 1024 * 500 / 8; // 500 MiB memory, about 65 million methods
static u32 _jmethod_id_count = 0;
static constexpr int JMETHOD_ID_LIMIT = 1024 * 1024 * 500 / 8; // 500 MiB memory, about 65 million methods
static int _jmethod_id_count = 0;
static bool _jmethod_id_count_warned = false;
JavaVM* VM::_vm;

View File

@@ -293,7 +293,7 @@ class VMKlass : VMStructs {
}
jmethodID* jmethodIDs() {
return __atomic_load_n((jmethodID**) at(_jmethod_ids_offset), __ATOMIC_ACQUIRE);
return loadAcquire(*(jmethodID**) at(_jmethod_ids_offset));
}
};

View File

@@ -67,7 +67,7 @@ class ThreadCpuTimeBuffer {
void reset() {
memset(_ringbuf, 0, sizeof(_ringbuf));
_read_ptr = 0;
__atomic_store_n(&_write_ptr, 0, __ATOMIC_RELEASE);
storeRelease(_write_ptr, 0);
}
void add(u64 trace) {