diff --git a/ecmascript/ecma_macros.h b/ecmascript/ecma_macros.h index ecf7fc9620..90175957de 100644 --- a/ecmascript/ecma_macros.h +++ b/ecmascript/ecma_macros.h @@ -184,6 +184,24 @@ Set##bitFieldName(name##Bits::Update(Get##bitFieldName(), t)); \ } +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define ACCESSORS_PRIMITIVE_FIELD_ATOMIC(name, type, offset) \ + inline void AtomicSet##name(type value) \ + { \ + volatile auto *atomicField = reinterpret_cast *>(ToUintPtr(this) + (offset)); \ + atomicField->store(value, std::memory_order_release); \ + } \ + inline type AtomicGet##name() const \ + { \ + volatile auto *atomicField = reinterpret_cast *>(ToUintPtr(this) + (offset)); \ + return atomicField->load(std::memory_order_acquire); \ + } + +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) +#define ACCESSORS_PRIMITIVE_FIELD_HAS_ATOMIC_INTERFACE(name, type, offset, endOffset) \ + ACCESSORS_PRIMITIVE_FIELD(name, type, offset, endOffset) \ + ACCESSORS_PRIMITIVE_FIELD_ATOMIC(name, type, offset) + // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define FIRST_BIT_FIELD(bitFieldName, name, type, bits) \ using name##Bits = BitField; \ diff --git a/ecmascript/mem/work_space_chunk.cpp b/ecmascript/mem/work_space_chunk.cpp index edb0c2f7ec..61027da72c 100644 --- a/ecmascript/mem/work_space_chunk.cpp +++ b/ecmascript/mem/work_space_chunk.cpp @@ -28,7 +28,6 @@ uintptr_t WorkSpaceChunk::NewArea(size_t size) LOG_ECMA_MEM(FATAL) << "OOM WorkSpaceChunk : NewArea area is nullptr"; UNREACHABLE(); } - allocator_->IncreaseNativeSizeStats(size, NativeFlag::CHUNK_MEM); areaList_.emplace(area, area); return area; } diff --git a/ecmascript/method.h b/ecmascript/method.h index 7d225c3844..71d6ed4583 100644 --- a/ecmascript/method.h +++ b/ecmascript/method.h @@ -130,9 +130,9 @@ public: void SetAotCodeBit(bool isCompiled) { - uint64_t callField = GetCallField(); + uint64_t callField = AtomicGetCallField(); uint64_t newValue = SetAotCodeBit(callField, isCompiled); - SetCallField(newValue); + AtomicSetCallField(newValue); } void SetFastBuiltinBit(bool isFastBuiltin) @@ -174,7 +174,7 @@ public: bool IsAotWithCallField() const { - uint64_t callField = GetCallField(); + uint64_t callField = AtomicGetCallField(); return MethodLiteral::IsAotWithCallField(callField); } @@ -320,14 +320,14 @@ public: void SetFunctionKind(FunctionKind kind) { - uint64_t extraLiteralInfo = GetExtraLiteralInfo(); + uint64_t extraLiteralInfo = AtomicGetExtraLiteralInfo(); uint64_t newValue = SetFunctionKind(extraLiteralInfo, kind); - SetExtraLiteralInfo(newValue); + AtomicSetExtraLiteralInfo(newValue); } FunctionKind GetFunctionKind() const { - uint64_t extraLiteralInfo = GetExtraLiteralInfo(); + uint64_t extraLiteralInfo = AtomicGetExtraLiteralInfo(); return GetFunctionKind(extraLiteralInfo); } @@ -484,14 +484,15 @@ public: static constexpr size_t CONSTANT_POOL_OFFSET = TaggedObjectSize(); ACCESSORS(ConstantPool, CONSTANT_POOL_OFFSET, CALL_FIELD_OFFSET) - ACCESSORS_PRIMITIVE_FIELD(CallField, uint64_t, CALL_FIELD_OFFSET, NATIVE_POINTER_OR_BYTECODE_ARRAY_OFFSET) + ACCESSORS_PRIMITIVE_FIELD_HAS_ATOMIC_INTERFACE(CallField, uint64_t, CALL_FIELD_OFFSET, + NATIVE_POINTER_OR_BYTECODE_ARRAY_OFFSET) // Native method decides this filed is NativePointer or BytecodeArray pointer. ACCESSORS_NATIVE_FIELD( NativePointerOrBytecodeArray, void, NATIVE_POINTER_OR_BYTECODE_ARRAY_OFFSET, CODEENTRY_LITERAL_OFFSET) ACCESSORS_PRIMITIVE_FIELD(CodeEntryOrLiteral, uintptr_t, CODEENTRY_LITERAL_OFFSET, LITERAL_INFO_OFFSET) // hotness counter is encoded in a js method field, the first uint16_t in a uint64_t. ACCESSORS_PRIMITIVE_FIELD(LiteralInfo, uint64_t, LITERAL_INFO_OFFSET, EXTRA_LITERAL_INFO_OFFSET) - ACCESSORS_PRIMITIVE_FIELD(ExtraLiteralInfo, uint64_t, EXTRA_LITERAL_INFO_OFFSET, LAST_OFFSET) + ACCESSORS_PRIMITIVE_FIELD_HAS_ATOMIC_INTERFACE(ExtraLiteralInfo, uint64_t, EXTRA_LITERAL_INFO_OFFSET, LAST_OFFSET) DEFINE_ALIGN_SIZE(LAST_OFFSET); DECL_VISIT_OBJECT(CONSTANT_POOL_OFFSET, CALL_FIELD_OFFSET); diff --git a/ecmascript/napi/jsnapi_expo.cpp b/ecmascript/napi/jsnapi_expo.cpp index 675cadd24f..7bc96f2a49 100644 --- a/ecmascript/napi/jsnapi_expo.cpp +++ b/ecmascript/napi/jsnapi_expo.cpp @@ -4246,6 +4246,7 @@ void JSNApi::SetEnv(EcmaVM *vm, void *env) void JSNApi::SynchronizVMInfo(EcmaVM *vm, const EcmaVM *hostVM) { + std::atomic_thread_fence(std::memory_order_seq_cst); vm->SetBundleName(hostVM->GetBundleName()); vm->SetModuleName(hostVM->GetModuleName()); vm->SetAssetPath(hostVM->GetAssetPath());