tinycc/tests/tests2/136_atomic_gcc_style.c
kbkpbot 36ff4f52b5
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
Add GCC-style atomic builtins: __atomic_*_n
This commit adds GCC-compatible atomic builtins to stdatomic.h for
better compatibility with code relying on GCC's atomic primitives.

1. ​**​New Macros​**​:
   - `__atomic_compare_exchange_n`.
   - `__atomic_load_n`.
   - `__atomic_store_n`.

2. ​**​Motivation​**​:
   - Improves compatibility with codebases using GCC's atomic builtins
     directly.
   - Simplifies atomic operations by allowing direct value passing
     (instead of pointers), enhancing code readability.
   - Aligns TCC's atomic support closer to GCC/Clang for cross-compiler
     projects.

3. ​**​Testing​**​:
   - Verified with basic test cases (integer CAS, load/store) on x86.
   - Confirmed no regressions in existing atomic operations.

- This change does not affect existing C11 `atomic_*` APIs.
- Struct types are intentionally unsupported in `_n` variants due to
  complexity.
2025-04-19 08:05:27 +08:00

76 lines
2.2 KiB
C

#include <stdatomic.h>
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
int main() {
// Test 1: Basic functionality of __atomic_store_n and __atomic_load_n
{
int atomic_var = 0;
__atomic_store_n(&atomic_var, 42, __ATOMIC_SEQ_CST);
int loaded = __atomic_load_n(&atomic_var, __ATOMIC_SEQ_CST);
assert(loaded == 42);
}
// Test 2: Successful exchange with __atomic_compare_exchange_n
{
int atomic_var = 100;
int expected = 100;
bool success = __atomic_compare_exchange_n(
&atomic_var, &expected, 200,
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
);
assert(success);
assert(atomic_var == 200);
assert(expected == 100); // expected remains unchanged on success
}
// Test 3: Failed exchange with __atomic_compare_exchange_n (update expected)
{
int atomic_var = 100;
int expected = 99;
bool success = __atomic_compare_exchange_n(
&atomic_var, &expected, 200,
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST
);
assert(!success);
assert(atomic_var == 100);
assert(expected == 100); // expected updated to current value on failure
}
// Test 4: Weak version (spurious failure handling)
{
int atomic_var = 50;
int expected = 50;
for (int i = 0; i < 10; i++) {
if (__atomic_compare_exchange_n(
&atomic_var, &expected, 60,
true, __ATOMIC_RELAXED, __ATOMIC_RELAXED
)) {
break;
}
}
assert(atomic_var == 60);
}
// Test 5: Pointer type operations
{
int value = 100;
int* atomic_ptr = &value;
int* new_ptr = NULL;
__atomic_store_n(&atomic_ptr, new_ptr, __ATOMIC_RELEASE);
int* loaded_ptr = __atomic_load_n(&atomic_ptr, __ATOMIC_ACQUIRE);
assert(loaded_ptr == NULL);
}
// Test 6: Relaxed memory ordering
{
int atomic_var = 0;
__atomic_store_n(&atomic_var, 10, __ATOMIC_RELAXED);
assert(__atomic_load_n(&atomic_var, __ATOMIC_RELAXED) == 10);
}
printf("All atomic tests passed!\n");
return 0;
}