Compat lua compile as cpp (#1568)

* Compat lua compile as cpp

* Compat lua compile as cpp
This commit is contained in:
Bruce
2022-04-07 17:22:32 +08:00
committed by GitHub
parent 2660b10442
commit 9fd7a51b34
6 changed files with 68 additions and 58 deletions

View File

@@ -24,41 +24,49 @@
#else
#if defined (__cplusplus)
#include <atomic>
#define STD_ std::
#define atomic_value_type_(p, v) decltype((p)->load())(v)
#else
#include <stdatomic.h>
#define STD_
#define atomic_value_type_(p, v) v
#endif
#define ATOM_INT atomic_int
#define ATOM_POINTER atomic_uintptr_t
#define ATOM_SIZET atomic_size_t
#define ATOM_ULONG atomic_ulong
#define ATOM_INIT(ref, v) atomic_init(ref, v)
#define ATOM_LOAD(ptr) atomic_load(ptr)
#define ATOM_STORE(ptr, v) atomic_store(ptr, v)
#define ATOM_INT STD_ atomic_int
#define ATOM_POINTER STD_ atomic_uintptr_t
#define ATOM_SIZET STD_ atomic_size_t
#define ATOM_ULONG STD_ atomic_ulong
#define ATOM_INIT(ref, v) STD_ atomic_init(ref, v)
#define ATOM_LOAD(ptr) STD_ atomic_load(ptr)
#define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v)
static inline int
ATOM_CAS(atomic_int *ptr, int oval, int nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) {
return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) {
return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) {
return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
}
static inline int
ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval);
ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) {
return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
}
#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1)
#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1)
#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n)
#define ATOM_FSUB(ptr,n) atomic_fetch_sub(ptr, n)
#define ATOM_FAND(ptr,n) atomic_fetch_and(ptr, n)
#define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1))
#define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1))
#define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n))
#define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n))
#define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n))
#endif

View File

@@ -47,10 +47,11 @@ spinlock_destroy(struct spinlock *lock) {
#else // __STDC_NO_ATOMICS__
#include <stdatomic.h>
#define atomic_test_and_set_(ptr) atomic_exchange_explicit(ptr, 1, memory_order_acquire)
#define atomic_clear_(ptr) atomic_store_explicit(ptr, 0, memory_order_release);
#define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed)
#include "atomic.h"
#define atomic_test_and_set_(ptr) STD_ atomic_exchange_explicit(ptr, 1, STD_ memory_order_acquire)
#define atomic_clear_(ptr) STD_ atomic_store_explicit(ptr, 0, STD_ memory_order_release);
#define atomic_load_relaxed_(ptr) STD_ atomic_load_explicit(ptr, STD_ memory_order_relaxed)
#if defined(__x86_64__)
#include <immintrin.h> // For _mm_pause
@@ -60,12 +61,12 @@ spinlock_destroy(struct spinlock *lock) {
#endif
struct spinlock {
atomic_int lock;
STD_ atomic_int lock;
};
static inline void
spinlock_init(struct spinlock *lock) {
atomic_init(&lock->lock, 0);
STD_ atomic_init(&lock->lock, 0);
}
static inline void