C++ 静态线程安全标注
cpp-static-thread-safety
强制执行 C++ 静态线程安全标注,并正确使用同步原语。当设计多线程类或修改受保护(guarded)的成员字段时,使用此技能。
- License 许可
- Apache-2.0
- 所属仓库
- google/filament
- 最近更新
- 2026-11-14
- Stars
- ★ 2.1w
- Forks
- 2.3k
01 前言 Front Matter
- name
- cpp-static-thread-safety
- description
- Enforce C++ static thread safety annotations and correct synchronization primitives. Use this skill when designing multi-threaded classes or editing guarded member fields.
03 SKILL.md 正文
C++ Static Thread Safety & Synchronization Guidelines
Filament leverages Clang's static thread safety analysis to verify lock holding requirements at compile-time. All multi-threaded classes must use explicit capability annotations to guarantee race-free state access.
1. Selecting the Correct Lock Primitive
- Filament Primitives (
utils::Mutex&utils::Condition) — MANDATORY:- Rule: All engine, backend, and utility code under
filament/must useutils::Mutexandutils::Conditionexclusively. Do not usestd::mutexorstd::condition_variable. - Deadlock & Order Debugging:
utils::Mutextransparently integrates with Filament's compile-time lock debugging facility (FILAMENT_DEBUG_MUTEX/-u). When enabled, it maintains a global cycle dependency graph via BFS duringlock()andtry_lock(), immediately trapping lock-order inversions and self-deadlocks with exactCallStacktraces. Any locks defined viastd::mutexbypass this tracker entirely and remain invisible to deadlock diagnostics. - Memory & Cache Hygiene: On Android and Linux (
linuxutil::Mutex),utils::Mutexis only 4 bytes (a single atomic futex word) versus 40 bytes forstd::mutex. For structures allocated in large volumes or embedded in handles/fences, this 10x size reduction prevents struct bloat and maintains cache-line density. - Condition Variable Support:
utils::Condition(Condition::wait/wait_until) is explicitly templated (template <typename M>) to work seamlessly withUniqueLock<utils::Mutex>for producer-consumer queues and blocking wait loops. - Priority Inversion Reality: C++ standard
std::mutex(pthread_mutex_t) does not provide priority inheritance out of the box (PTHREAD_PRIO_NONE). Therefore,std::mutexoffers zero priority inversion protection overutils::Mutex.
- Rule: All engine, backend, and utility code under
2. Lock Guard & RAII Lifetime Conventions
- Immutability Invariant: Use
LockGuard constas the default for all standard synchronized blocks to guarantee scope-bound read-only lock scopes.// Correct utils::LockGuard const lock(mLock); - Condition Variables Overloads: Use
UniqueLock(non-const) strictly when passing locks to condition variables (wait(lock)) or when explicit.unlock()/.lock()boundaries are required for performance or deadlock prevention. - Lock Dependency: Any source file (
.cpp) that instantiatesLockGuardorUniqueLockmust explicitly include the matching utility header:#include <utils/Mutex.h>
3. Resolving Clang's Lambda Closure Limitations
Clang evaluates C++ anonymous closures (lambdas) as separate context boundaries. Because lambdas lack capability attributes, standard condition variable waits passing local predicates (e.g., using std::ranges::all_of) will trigger false-positive thread safety errors.
To resolve this, use one of the following approved patterns:
Pattern A: Inner Lambda Bypass (Recommended)
Decorate only the CV wait predicate lambda operator with UTILS_NO_THREAD_SAFETY_ANALYSIS to ignore nested boundaries while preserving outer compile-time checks:
UniqueLock lock(mQueueLock);
mQueueCondition.wait(lock, [this]() UTILS_NO_THREAD_SAFETY_ANALYSIS {
return mExitRequested ||
(!std::ranges::all_of(mQueues, [](auto&& q) { return q.empty(); }));
});
Pattern B: Manual Loop Inlining
If the check is flat, completely inline the CV predicate as a standard while loop to bring the member variables directly into the parent function's locked scope:
UniqueLock lock(mLock);
while (mFreeSpace < requiredSize) {
mCondition.wait(lock);
}
4. Dynamic Threading & Preprocessor Safety
- Single-Threaded Parity: All thread safety annotations (
UTILS_GUARDED_BY) are conditionally compiled out on single-threaded configurations. To prevent compile crashes whenFILAMENT_SINGLE_THREADEDis defined, standard annotations are gated byUTILS_HAS_THREADINGincompiler.h.
04 同仓库更多 Skills
Filament 数学运算与坐标变换
filament-math
确保 Filament 代码中数学原语和命名约定的正确性。在执行坐标变换、向量运算或投影设置时使用此技能。
Filament 对象生命周期管理
filament-lifetime
确保 Filament 对象创建与销毁生命周期的正确性。在初始化渲染管线或分配/释放 GPU 缓冲区和实体(entity)时使用此技能。
Git 提交 cherry-pick 与回填
cherry-pick
将某个提交 cherry-pick 到另一个分支并创建 PR。当用户要求将某个提交或 PR cherry-pick 或回填(backport)到发布分支时使用。
C++ 代码验证流程
verification-protocols
修改 C++ 源文件或头文件后需执行的标准验证流程。使用此技能格式化 include 语句、构建引擎并运行核心测试。
C++ 预处理保护规范检查
preprocessor-guard-hygiene
在 C++ 中强制实施预处理保护(preprocessor guard)规则以及依赖宏的包含卫生规范。在修改条件导入或预处理指令时使用此技能。
C++ 头文件自包含检查
header-self-containment
强制实施严格的头文件自包含规范,确保每个头文件都能独立编译。在创建或修改 C++ 头文件时使用此技能。