bh_atomic.h: Add BH_ATOMIC_32_FETCH_ADD/BH_ATOMIC_32_FETCH_SUB (#2408)

This commit is contained in:
YAMAMOTO Takashi 2023-07-31 18:56:15 +09:00 committed by GitHub
parent 151600fef2
commit b1fa27e91d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -63,10 +63,16 @@ typedef uint32 bh_atomic_32_t;
__atomic_fetch_or(&(v), (val), __ATOMIC_SEQ_CST)
#define BH_ATOMIC_32_FETCH_AND(v, val) \
__atomic_fetch_and(&(v), (val), __ATOMIC_SEQ_CST)
#define BH_ATOMIC_32_FETCH_ADD(v, val) \
__atomic_fetch_add(&(v), (val), __ATOMIC_SEQ_CST)
#define BH_ATOMIC_32_FETCH_SUB(v, val) \
__atomic_fetch_sub(&(v), (val), __ATOMIC_SEQ_CST)
#else /* else of defined(CLANG_GCC_HAS_ATOMIC_BUILTIN) */
#define BH_ATOMIC_32_LOAD(v) (v)
#define BH_ATOMIC_32_FETCH_OR(v, val) nonatomic_32_fetch_or(&(v), val)
#define BH_ATOMIC_32_FETCH_AND(v, val) nonatomic_32_fetch_and(&(v), val)
#define BH_ATOMIC_32_FETCH_ADD(v, val) nonatomic_32_fetch_add(&(v), val)
#define BH_ATOMIC_32_FETCH_SUB(v, val) nonatomic_32_fetch_sub(&(v), val)
static inline uint32
nonatomic_32_fetch_or(bh_atomic_32_t *p, uint32 val)
@ -84,6 +90,22 @@ nonatomic_32_fetch_and(bh_atomic_32_t *p, uint32 val)
return old;
}
static inline uint32
nonatomic_32_fetch_add(bh_atomic_32_t *p, uint32 val)
{
uint32 old = *p;
*p += val;
return old;
}
static inline uint32
nonatomic_32_fetch_sub(bh_atomic_32_t *p, uint32 val)
{
uint32 old = *p;
*p -= val;
return old;
}
/* The flag can be defined by the user if the platform
supports atomic access to uint32 aligned memory. */
#ifdef WASM_UINT32_IS_ATOMIC

View File

@ -28,5 +28,15 @@ main(int argc, char **argv)
assert(o == 0x00ff00ff);
assert(v == 0x00ff0000);
v = 0x00ff00ff;
o = BH_ATOMIC_32_FETCH_ADD(v, 0x10101);
assert(o == 0x00ff00ff);
assert(v == 0x00ff00ff + 0x10101);
v = 0x00ff00ff;
o = BH_ATOMIC_32_FETCH_SUB(v, 0x10101);
assert(o == 0x00ff00ff);
assert(v == 0x00ff00ff - 0x10101);
return 0;
}