Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ namespace Piccolo

GObjectID ObjectIDAllocator::alloc()
{
std::atomic<GObjectID> new_object_ret = m_next_id.load();
m_next_id++;
if (m_next_id >= k_invalid_gobject_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");
}
Expand All @@ -19,3 +22,9 @@ namespace Piccolo
}

} // namespace Piccolo