summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristian Høgsberg <krh@bitplanet.net>2021-02-12 12:26:47 -0800
committerMarge Bot <eric+marge@anholt.net>2021-02-18 00:27:49 +0000
commit0d5fe24c9b7de15241727922e18c3ea08d11ef08 (patch)
tree190c5812b3eaecb0353c17ac0982f428858d3a75
parent1cef1a34bb89f1c9d111c3d5f941096b1a36e381 (diff)
macros: Add thread-safety annotation macros
Extracted from !7529 Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9061>
-rw-r--r--meson.build1
-rw-r--r--src/util/macros.h32
2 files changed, 33 insertions, 0 deletions
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 */