From 0d5fe24c9b7de15241727922e18c3ea08d11ef08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Fri, 12 Feb 2021 12:26:47 -0800 Subject: [PATCH] macros: Add thread-safety annotation macros Extracted from !7529 Part-of: --- meson.build | 1 + src/util/macros.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/meson.build b/meson.build index 827651f088f..18f96bc2635 100644 --- a/meson.build +++ b/meson.build @@ -1040,6 +1040,7 @@ else '-Werror=incompatible-pointer-types', '-Werror=int-conversion', '-Wimplicit-fallthrough', + '-Werror=thread-safety', '-Wno-missing-field-initializers', '-Wno-format-truncation', '-fno-math-errno', diff --git a/src/util/macros.h b/src/util/macros.h index d7fe24e5e9b..1fc9e23355b 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -40,6 +40,10 @@ # define __has_builtin(x) 0 #endif +#ifndef __has_attribute +# define __has_attribute(x) 0 +#endif + /** * __builtin_expect macros */ @@ -412,4 +416,32 @@ enum pipe_debug_type #endif #endif +/* Macros for static type-safety checking. + * + * https://clang.llvm.org/docs/ThreadSafetyAnalysis.html + */ + +#if __has_attribute(capability) +typedef int __attribute__((capability("mutex"))) lock_cap_t; + +#define guarded_by(l) __attribute__((guarded_by(l))) +#define acquire_cap(l) __attribute((acquire_capability(l), no_thread_safety_analysis)) +#define release_cap(l) __attribute((release_capability(l), no_thread_safety_analysis)) +#define assert_cap(l) __attribute((assert_capability(l), no_thread_safety_analysis)) +#define requires_cap(l) __attribute((requires_capability(l))) +#define disable_thread_safety_analysis __attribute((no_thread_safety_analysis)) + +#else + +typedef int lock_cap_t; + +#define guarded_by(l) +#define acquire_cap(l) +#define release_cap(l) +#define assert_cap(l) +#define requires_cap(l) +#define disable_thread_safety_analysis + +#endif + #endif /* UTIL_MACROS_H */