Fix non-builtin BH_ATOMIC_32_FETCH_OR and BH_ATOMIC_32_FETCH_AND (#2400)

This commit is contained in:
YAMAMOTO Takashi 2023-07-30 20:23:30 +09:00 committed by GitHub
parent 10b18d85cd
commit 6d6cea1a73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 2 deletions

View File

@ -65,8 +65,24 @@ typedef uint32 bh_atomic_32_t;
__atomic_fetch_and(&(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) ((v) |= (val))
#define BH_ATOMIC_32_FETCH_AND(v, val) ((v) &= (val))
#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)
static inline uint32
nonatomic_32_fetch_or(bh_atomic_32_t *p, uint32 val)
{
uint32 old = *p;
*p |= val;
return old;
}
static inline uint32
nonatomic_32_fetch_and(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. */

View File

@ -0,0 +1,20 @@
# Copyright (C) 2023 Midokura Japan KK. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.0)
project(bh_atomic)
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
if(APPLE)
add_definitions(-DBH_PLATFORM_DARWIN)
endif()
set(WAMR_BUILD_INTERP 1)
set(WAMR_BUILD_LIBC_BUILTIN 0)
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..)
include(${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
add_executable(bh_atomic main.c)
target_link_libraries(bh_atomic)

32
samples/bh_atomic/main.c Normal file
View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include <assert.h>
#include "bh_platform.h"
#include "bh_atomic.h"
int
main(int argc, char **argv)
{
bh_atomic_32_t v;
uint32 o;
v = 0x00ff00ff;
o = BH_ATOMIC_32_LOAD(v);
assert(o == 0x00ff00ff);
v = 0x00ff00ff;
o = BH_ATOMIC_32_FETCH_OR(v, 0xffff0000);
assert(o == 0x00ff00ff);
assert(v == 0xffff00ff);
v = 0x00ff00ff;
o = BH_ATOMIC_32_FETCH_AND(v, 0xffff0000);
assert(o == 0x00ff00ff);
assert(v == 0x00ff0000);
return 0;
}