Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 7 additions & 33 deletions core/iwasm/common/wasm_suspend_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef _WASM_SUSPEND_FLAGS_H
#define _WASM_SUSPEND_FLAGS_H

#include "bh_platform.h"
#include "bh_atomic.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -22,42 +22,16 @@ extern "C" {
#define WASM_SUSPEND_FLAG_EXIT 0x8

typedef union WASMSuspendFlags {
uint32 flags;
bh_atomic_32_t flags;
uintptr_t __padding__;
} WASMSuspendFlags;

#if defined(__GNUC_PREREQ)
#if __GNUC_PREREQ(4, 7)
#define CLANG_GCC_HAS_ATOMIC_BUILTIN
#endif
#elif defined(__clang__)
#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0)
#define CLANG_GCC_HAS_ATOMIC_BUILTIN
#endif
#endif

#if defined(CLANG_GCC_HAS_ATOMIC_BUILTIN)
#define WASM_SUSPEND_FLAGS_IS_ATOMIC 1
#define WASM_SUSPEND_FLAGS_GET(s_flags) \
__atomic_load_n(&s_flags.flags, __ATOMIC_SEQ_CST)
#define WASM_SUSPEND_FLAGS_IS_ATOMIC BH_ATOMIC_32_IS_ATOMIC
#define WASM_SUSPEND_FLAGS_GET(s_flags) BH_ATOMIC_32_LOAD(s_flags.flags)
#define WASM_SUSPEND_FLAGS_FETCH_OR(s_flags, val) \
__atomic_fetch_or(&s_flags.flags, val, __ATOMIC_SEQ_CST)
BH_ATOMIC_32_FETCH_OR(s_flags.flags, val)
#define WASM_SUSPEND_FLAGS_FETCH_AND(s_flags, val) \
__atomic_fetch_and(&s_flags.flags, val, __ATOMIC_SEQ_CST)
#else /* else of defined(CLANG_GCC_HAS_ATOMIC_BUILTIN) */
#define WASM_SUSPEND_FLAGS_GET(s_flags) (s_flags.flags)
#define WASM_SUSPEND_FLAGS_FETCH_OR(s_flags, val) (s_flags.flags |= val)
#define WASM_SUSPEND_FLAGS_FETCH_AND(s_flags, val) (s_flags.flags &= val)

/* The flag can be defined by the user if the platform
supports atomic access to uint32 aligned memory. */
#ifdef WASM_UINT32_IS_ATOMIC
#define WASM_SUSPEND_FLAGS_IS_ATOMIC 1
#else /* else of WASM_UINT32_IS_ATOMIC */
#define WASM_SUSPEND_FLAGS_IS_ATOMIC 0
#endif /* WASM_UINT32_IS_ATOMIC */

#endif
BH_ATOMIC_32_FETCH_AND(s_flags.flags, val)

#if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0
#define WASM_SUSPEND_FLAGS_LOCK(lock) (void)0
Expand All @@ -71,4 +45,4 @@ typedef union WASMSuspendFlags {
}
#endif

#endif /* end of _WASM_SUSPEND_FLAGS_H */
#endif /* end of _WASM_SUSPEND_FLAGS_H */
53 changes: 53 additions & 0 deletions core/shared/utils/bh_atomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Amazon Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#ifndef _BH_ATOMIC_H
#define _BH_ATOMIC_H

#include "gnuc.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef uint32 bh_atomic_32_t;

#if defined(__GNUC_PREREQ)
#if __GNUC_PREREQ(4, 7)
#define CLANG_GCC_HAS_ATOMIC_BUILTIN
#endif
#elif defined(__clang__)
#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 0)
#define CLANG_GCC_HAS_ATOMIC_BUILTIN
#endif
#endif

#if defined(CLANG_GCC_HAS_ATOMIC_BUILTIN)
#define BH_ATOMIC_32_IS_ATOMIC 1
Comment thread
wenyongh marked this conversation as resolved.
#define BH_ATOMIC_32_LOAD(v) __atomic_load_n(&(v), __ATOMIC_SEQ_CST)
#define BH_ATOMIC_32_FETCH_OR(v, val) \
__atomic_fetch_or(&(v), (val), __ATOMIC_SEQ_CST)
#define BH_ATOMIC_32_FETCH_AND(v, val) \
__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))

/* The flag can be defined by the user if the platform
supports atomic access to uint32 aligned memory. */
#ifdef WASM_UINT32_IS_ATOMIC
#define BH_ATOMIC_32_IS_ATOMIC 1
Comment thread
wenyongh marked this conversation as resolved.
#else /* else of WASM_UINT32_IS_ATOMIC */
#define BH_ATOMIC_32_IS_ATOMIC 0
#endif /* WASM_UINT32_IS_ATOMIC */

#endif

#ifdef __cplusplus
}
#endif

#endif /* end of _BH_ATOMIC_H */
1 change: 0 additions & 1 deletion core/shared/utils/bh_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "bh_log.h"
#include "bh_queue.h"
#include "bh_vector.h"
#include "gnuc.h"
#include "runtime_timer.h"

/**
Expand Down