[PATCH 01/14] include: sbi: add thread safety analysis macros

Carlos López carlos.lopezr4096 at gmail.com
Wed Jul 29 09:35:28 PDT 2026


Add macros to properly guard clang's thread safety analysis attributes
keeping compatibility with GCC, on top of making things more readable.

Signed-off-by: Carlos López <carlos.lopezr4096 at gmail.com>
---
 include/sbi/sbi_visibility.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/sbi/sbi_visibility.h b/include/sbi/sbi_visibility.h
index f900c44565a4..56ebb83855d7 100644
--- a/include/sbi/sbi_visibility.h
+++ b/include/sbi/sbi_visibility.h
@@ -15,4 +15,21 @@
 #pragma GCC visibility push(hidden)
 #endif
 
+/* Thread Safety Analysis (TSA) annotations for use with clang -Wthread-safety */
+#if defined(__clang__)
+#define CLANG_ATTR(x) __attribute__((x))
+#else
+#define CLANG_ATTR(x)
+#endif
+
+#define CAPABILITY(x) CLANG_ATTR(capability(x))
+#define GUARDED_BY(x) CLANG_ATTR(guarded_by(x))
+#define PT_GUARDED_BY(x) CLANG_ATTR(pt_guarded_by(x))
+#define ACQUIRE(...) CLANG_ATTR(acquire_capability(__VA_ARGS__))
+#define RELEASE(...) CLANG_ATTR(release_capability(__VA_ARGS__))
+#define MUST_HOLD(...) CLANG_ATTR(requires_capability(__VA_ARGS__))
+#define MUST_NOT_HOLD(...) CLANG_ATTR(locks_excluded(__VA_ARGS__))
+#define TRY_ACQUIRE(b, ...) CLANG_ATTR(try_acquire_capability(b, __VA_ARGS__))
+#define NO_THREAD_SAFETY_ANALYSIS CLANG_ATTR(no_thread_safety_analysis)
+
 #endif
-- 
2.51.0




More information about the opensbi mailing list