Skip to content

[BUG] nxmutex_trylock 在ARM平台上的语意错误 #267

@qingquanzhang151

Description

@qingquanzhang151

Description / Steps to reproduce the issue

您好,
在使用小米 Vela OS 的 nxmutex_trylock 接口时,发现 ARM 平台下存在语义不符合预期的情况:由于该接口底层依赖atomic_compare_exchange_weak_explicit实现,而 ARM 架构的 LL/SC 指令特性会导致atomic_compare_exchange_weak出现 “虚假失败”(即使锁未被占用也可能返回失败),最终造成nxmutex_trylock在无真实竞争时误判失败,违背了nxmutex_trylock接口 “仅当锁被实际占用时返回失败” 的语义约定,且该问题具有明显的平台相关性。
nxmutex_trylock作为通用同步接口,应保证跨平台的语义一致性,而当前实现未屏蔽原子操作的底层平台特性,可能给上层应用带来兼容性问题。建议可通过以下方式优化:

  1. 替换为atomic_compare_exchange_strong_explicit,消除虚假失败;
  2. 若保留weak版本以追求性能,可增加循环逻辑过滤虚假失败。

希望该问题能得到关注,感谢 Vela 团队的开源贡献!

Hello,
When using the nxmutex_trylock interface of Xiaomi Vela OS, we encountered a semantic inconsistency issue on ARM platforms. The root cause is that this interface is implemented based on atomic_compare_exchange_weak_explicit under the hood. Due to the LL/SC (Load-Linked/Store-Conditional) instruction characteristics of the ARM architecture, atomic_compare_exchange_weak may produce "spurious failures" — it might return failure even when the mutex is not occupied. This ultimately leads to nxmutex_trylock incorrectly reporting failure in the absence of real contention, which violates the core semantic contract of a trylock interface: failure should only occur when the mutex is actually held by another thread. Additionally, this issue is explicitly platform-dependent.
As a general-purpose synchronization interface, nxmutex_trylock should guarantee cross-platform semantic consistency. The current implementation fails to abstract underlying platform-specific behaviors of atomic operations, potentially introducing compatibility issues for upper-layer applications. We propose the following optimization approaches:

  1. Replace atomic_compare_exchange_weak_explicit with atomic_compare_exchange_strong_explicit to eliminate spurious failures entirely;
  2. If retaining the weak variant for performance optimization (e.g., reducing bus traffic under high contention), add a loop to filter out spurious failures.

We hope this issue receives your attention. Thank you for the excellent open-source work of the Vela team!

Issue Architecture

[Arch: arm]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions