mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Add atomics to chibicc
This change also fixes #434 and makes the chibicc assembler better.
This commit is contained in:
parent
5ddf43332e
commit
a988896048
21 changed files with 650 additions and 445 deletions
41
third_party/chibicc/test/atomic_test.c
vendored
Normal file
41
third_party/chibicc/test/atomic_test.c
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "third_party/chibicc/test/test.h"
|
||||
|
||||
_Atomic(int) lock;
|
||||
|
||||
main() {
|
||||
int x;
|
||||
|
||||
ASSERT(0, __atomic_exchange_n(&lock, 1, __ATOMIC_SEQ_CST));
|
||||
__atomic_load(&lock, &x, __ATOMIC_SEQ_CST);
|
||||
ASSERT(1, x);
|
||||
ASSERT(1, __atomic_exchange_n(&lock, 2, __ATOMIC_SEQ_CST));
|
||||
ASSERT(2, __atomic_exchange_n(&lock, 3, __ATOMIC_SEQ_CST));
|
||||
ASSERT(3, __atomic_load_n(&lock, __ATOMIC_SEQ_CST));
|
||||
__atomic_store_n(&lock, 0, __ATOMIC_SEQ_CST);
|
||||
ASSERT(0, __atomic_fetch_xor(&lock, 3, __ATOMIC_SEQ_CST));
|
||||
ASSERT(3, __atomic_fetch_xor(&lock, 1, __ATOMIC_SEQ_CST));
|
||||
ASSERT(2, __atomic_load_n(&lock, __ATOMIC_SEQ_CST));
|
||||
|
||||
// CAS success #1
|
||||
x = 2;
|
||||
ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 3, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(2, x);
|
||||
ASSERT(3, lock);
|
||||
|
||||
// CAS success #2
|
||||
x = 3;
|
||||
ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 4, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(3, x);
|
||||
ASSERT(4, lock);
|
||||
|
||||
// CAS fail
|
||||
x = 3;
|
||||
ASSERT(0, __atomic_compare_exchange_n(&lock, &x, 7, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(4, x);
|
||||
ASSERT(4, lock);
|
||||
|
||||
//
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue