From 9827a4daf496bcc9b1639225fd357d1d23d37b80 Mon Sep 17 00:00:00 2001 From: jdymitarai Date: Thu, 10 Sep 2026 12:22:55 +0800 Subject: [PATCH] fix(mutex): auto-detect thread safety attributes and annotate lock/unlock (fixes #2263) --- src/mutex.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/mutex.h b/src/mutex.h index bec78d9e5f..8d2928d227 100644 --- a/src/mutex.h +++ b/src/mutex.h @@ -8,7 +8,16 @@ // Enable thread safety attributes only with clang. // The attributes can be safely erased when compiling with other compilers. -#if defined(HAVE_THREAD_SAFETY_ATTRIBUTES) +#if !defined(HAVE_THREAD_SAFETY_ATTRIBUTES) +#if defined(__clang__) && defined(__has_attribute) +#if __has_attribute(capability) && __has_attribute(scoped_lockable) && \ + __has_attribute(guarded_by) +#define HAVE_THREAD_SAFETY_ATTRIBUTES 1 +#endif +#endif +#endif + +#if defined(HAVE_THREAD_SAFETY_ATTRIBUTES) && HAVE_THREAD_SAFETY_ATTRIBUTES #define THREAD_ANNOTATION_ATTRIBUTE_(x) __attribute__((x)) #else #define THREAD_ANNOTATION_ATTRIBUTE_(x) // no-op @@ -76,8 +85,8 @@ class CAPABILITY("mutex") Mutex { public: Mutex() {} - void lock() ACQUIRE() { mut_.lock(); } - void unlock() RELEASE() { mut_.unlock(); } + void lock() ACQUIRE() NO_THREAD_SAFETY_ANALYSIS { mut_.lock(); } + void unlock() RELEASE() NO_THREAD_SAFETY_ANALYSIS { mut_.unlock(); } std::mutex& native_handle() { return mut_; } private: