From adc408698a60a165f38bd66221a1a9ca46ad0c46 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Wed, 30 May 2018 14:06:37 -0700 Subject: Added Clang thread-safety annotations in mutex.h For Clang's thread safety analysis (-Wthread-safety) to pass without warnings, we need to annotate WrappedMutex to indicate that Lock() acquires the lock and Unlock() releases it. This CL adds the annotations and guards them with an ifdef to make sure they're a no-op on compilers other than Clang. This is a cherry-pick of a change I already made to the Google-internal codebase. --- src/google/protobuf/stubs/mutex.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/stubs/mutex.h b/src/google/protobuf/stubs/mutex.h index b9b7d2e1..47edb7a3 100644 --- a/src/google/protobuf/stubs/mutex.h +++ b/src/google/protobuf/stubs/mutex.h @@ -34,6 +34,18 @@ #include +// Define thread-safety annotations for use below, if we are building with +// Clang. +#if defined(__clang__) && !defined(SWIG) +#define GOOGLE_PROTOBUF_ACQUIRE(...) \ + __attribute__((acquire_capability(__VA_ARGS__))) +#define GOOGLE_PROTOBUF_RELEASE(...) \ + __attribute__((release_capability(__VA_ARGS__))) +#else +#define GOOGLE_PROTOBUF_ACQUIRE(...) +#define GOOGLE_PROTOBUF_RELEASE(...) +#endif + // =================================================================== // emulates google3/base/mutex.h namespace google { @@ -48,8 +60,8 @@ namespace internal { class LIBPROTOBUF_EXPORT WrappedMutex { public: WrappedMutex() = default; - void Lock() { mu_.lock(); } - void Unlock() { mu_.unlock(); } + void Lock() GOOGLE_PROTOBUF_ACQUIRE() { mu_.lock(); } + void Unlock() GOOGLE_PROTOBUF_RELEASE() { mu_.unlock(); } // Crash if this Mutex is not held exclusively by this thread. // May fail to crash when it should; will never crash when it should not. void AssertHeld() const {} @@ -123,8 +135,10 @@ using internal::ReaderMutexLock; using internal::WriterMutexLock; using internal::MutexLockMaybe; - } // namespace protobuf } // namespace google +#undef GOOGLE_PROTOBUF_ACQUIRE +#undef GOOGLE_PROTOBUF_RELEASE + #endif // GOOGLE_PROTOBUF_STUBS_MUTEX_H_ -- cgit v1.2.3