From 4131ddabd827726e6fadbc4d5ffe492936cbeb36 Mon Sep 17 00:00:00 2001 From: yuanjay07 Date: Tue, 14 Jul 2026 00:04:00 +0800 Subject: [PATCH 1/2] fix(framework): fix potential race condition and false alarm in ObjectIDAllocator --- .../function/framework/object/object_id_allocator.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/engine/source/runtime/function/framework/object/object_id_allocator.cpp b/engine/source/runtime/function/framework/object/object_id_allocator.cpp index 9d5b80b0d..d8e856d57 100644 --- a/engine/source/runtime/function/framework/object/object_id_allocator.cpp +++ b/engine/source/runtime/function/framework/object/object_id_allocator.cpp @@ -8,9 +8,8 @@ namespace Piccolo GObjectID ObjectIDAllocator::alloc() { - std::atomic new_object_ret = m_next_id.load(); - m_next_id++; - if (m_next_id >= k_invalid_gobject_id) + GObjectID new_object_ret = m_next_id++; + if (new_object_ret >= k_invalid_gobject_id - 1) { LOG_FATAL("gobject id overflow"); } @@ -19,3 +18,8 @@ namespace Piccolo } } // namespace Piccolo + + + + + From 268052370ff78b9caf3795108fa532abce90491c Mon Sep 17 00:00:00 2001 From: yuanjay07 Date: Tue, 14 Jul 2026 01:44:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9D=BE=E6=95=A3=E5=86=85=E5=AD=98=E5=BA=8F=E8=87=AA?= =?UTF-8?q?=E5=A2=9E=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=BA=A2=E5=87=BA=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E7=9A=84=E4=B8=AD=E6=96=87=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../function/framework/object/object_id_allocator.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/engine/source/runtime/function/framework/object/object_id_allocator.cpp b/engine/source/runtime/function/framework/object/object_id_allocator.cpp index d8e856d57..74df7de5c 100644 --- a/engine/source/runtime/function/framework/object/object_id_allocator.cpp +++ b/engine/source/runtime/function/framework/object/object_id_allocator.cpp @@ -8,7 +8,11 @@ namespace Piccolo GObjectID ObjectIDAllocator::alloc() { - GObjectID new_object_ret = m_next_id++; + // 使用松散内存顺序(std::memory_order_relaxed)执行原子自增并返回旧值,性能更优 + GObjectID new_object_ret = m_next_id.fetch_add(1, std::memory_order_relaxed); + + // 由于 new_object_ret 是自增前的值,判断自增后的值(new_object_ret + 1)是否溢出, + // 等价于判断 new_object_ret 是否大于等于 k_invalid_gobject_id - 1 if (new_object_ret >= k_invalid_gobject_id - 1) { LOG_FATAL("gobject id overflow"); @@ -23,3 +27,4 @@ namespace Piccolo +