Repository navigation
[components][libc][pthreads] fix race in lazy mutex initialization - #12107
BernardXiong wants to merge 3 commits into
Conversation
Mutexes declared with PTHREAD_MUTEX_INITIALIZER are initialized lazily in pthread_mutex_lock, pthread_mutex_trylock and pthread_mutex_unlock. When several threads use such a mutex concurrently, they can all observe the uninitialized state and initialize it at the same time. They can also observe the attributes before the underlying rt_mutex is ready, which corrupts the mutex. Use an atomic state machine (-1 -> -2 -> attributes) so that only one thread performs the initialization while the others wait until the mutex is fully ready. Publish the attributes only after rt_mutex_init() succeeds, and make the mutex name counter atomic as well.
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_libcReviewers: @GorrayLi @mysterywolf Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-12-02 08:55 CST)
📝 Review Instructions
|
|
Apply the CI-pinned clang-format style to the changed lazy initialization lines, including braces around conditional bodies. This resolves the PR format check without changing mutex behavior.
| * initialized, so that a concurrent lazy initialization cannot observe | ||
| * a half-initialized mutex. | ||
| */ | ||
| rt_atomic_store(PTHREAD_MUTEX_ATTR(mutex), |
There was a problem hiding this comment.
[P1] 用 release/acquire 发布初始化结果
ARM SMP 使用硬件原子后端时,rt_atomic_store/load 的 LDREX/STREX 没有内存屏障。另一核可能先看到 attr 已就绪,却读到尚未可见的 rt_mutex 初始化字段。请用 release 发布 attr,用 acquire 读取就绪状态,确保硬件后端也有对应屏障。
There was a problem hiding this comment.
已在 7bdeefa 按建议处理:SMP 构建下,发布 attr 前调用 rt_hw_dmb() 作为 release 屏障,读取到就绪状态后再调用 rt_hw_dmb() 作为 acquire 屏障,不再依赖缺少内存序的硬件原子后端;单核构建不引入屏障。
验证:qemu-vexpress-a9 开启 RT_USING_SMP(2 核)和 RT_USING_PTHREADS,4 个线程各执行 10000 次 lock/unlock,计数为 40000,自测输出 PASS。
| return EINVAL; | ||
| if (mutex->attr == -1) | ||
| /* lazy initialization for PTHREAD_MUTEX_INITIALIZER */ | ||
| result = pthread_mutex_lazy_init(mutex); |
There was a problem hiding this comment.
[P2] 保持 pthread_mutex_trylock() 非阻塞
另一线程正在初始化静态锁时,这里会进入 rt_thread_delay(1) 循环。请给初始化函数加非等待路径,遇到 INITIALIZING 立即返回 EBUSY,并在这里原样返回。
There was a problem hiding this comment.
已在 7bdeefa 处理:pthread_mutex_lazy_init() 增加 wait 参数,pthread_mutex_trylock() 传入 RT_FALSE;遇到 INITIALIZING 时立即返回 EBUSY,不再进入 rt_thread_delay() 等待。
同一次 QEMU 自测中,持锁期间另一线程 trylock 返回 EBUSY,输出 PASS。
静态初始化的互斥锁在首次使用时惰性初始化。当前用 rt_atomic_store/load 发布和读取初始化状态,但部分硬件原子后端(如 ARM 的 LDREX/STREX)不提供 内存序,SMP 下另一核可能先看到状态就绪、却读到尚未初始化的 rt_mutex 字段。 - SMP 下在发布 attr 前加 release 屏障,读取到就绪状态后加 acquire 屏障; 单核构建不加屏障; - 为惰性初始化增加非等待路径,pthread_mutex_trylock() 遇到其他线程正在 初始化时直接返回 EBUSY,不再进入 rt_thread_delay() 循环。 验证:qemu-vexpress-a9 开启 RT_USING_SMP(2 核)和 RT_USING_PTHREADS, 4 个线程各执行 10000 次 lock/unlock 后计数为 40000,trylock 返回 EBUSY, 自测输出 PASS。
拉取/合并请求描述:(PR description)
为什么提交这份PR (why to submit this PR)
Mutexes declared with
PTHREAD_MUTEX_INITIALIZERare initialized lazily bypthread_mutex_lock(),pthread_mutex_trylock()andpthread_mutex_unlock().Concurrent first use can cause multiple threads to initialize the same mutex,
or allow another thread to observe its attributes before the underlying
rt_mutexis ready. This can corrupt the mutex.静态互斥锁在多线程首次并发使用时,可能重复初始化,或在底层锁尚未就绪时被访问。本 PR 修复这一延迟初始化竞态。
你的解决方案是什么 (what is your solution)
components/libc/posix/pthreads/pthread_mutex.conly.-1) to initializing(
-2), so that exactly one thread performs lazy initialization.other threads wait until initialization finishes.
通过原子状态机保证只有一个线程执行初始化,初始化成功后再发布属性;失败时恢复可重试状态,同时将锁名称计数器改为原子操作。
请提供验证的bsp和config (provide the config and bsp)
bsp/qemu-vexpress-a9.CONFIG_RT_USING_PTHREADS=yon top of the BSP defaultconfiguration, then generate
rtconfig.hwithscons --pyconfig-silent.scons -j8passed with the pthreadsources compiled. QEMU vexpress-a9 booted to the shell and successfully ran
version,freeandps. This was a boot/shell smoke test, not a dedicatedconcurrent lazy-initialization stress test. No SD image was attached, so the
BSP reported the expected SD mount warning during boot.
tools/ci/clang_format_check.pyhelpers and CI-pinned clang-format 22.1.8;the check and
git diff --checkpassed.with
CONFIG_RT_USING_PTHREADS=y(passed on commitd017c692d3), andPR CI checks.
d017c692d3completed successfully on December 2,2026: 115 check runs passed, 3 were skipped as expected, and the CLA status
passed. There are no failed or pending checks.
format check,
BSP builds,
automated test matrix,
static analysis.
does not apply.
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up