aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/stubs/mutex.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/stubs/mutex.h')
-rw-r--r--src/google/protobuf/stubs/mutex.h54
1 files changed, 29 insertions, 25 deletions
diff --git a/src/google/protobuf/stubs/mutex.h b/src/google/protobuf/stubs/mutex.h
index 174290f6..47edb7a3 100644
--- a/src/google/protobuf/stubs/mutex.h
+++ b/src/google/protobuf/stubs/mutex.h
@@ -30,46 +30,48 @@
#ifndef GOOGLE_PROTOBUF_STUBS_MUTEX_H_
#define GOOGLE_PROTOBUF_STUBS_MUTEX_H_
-#ifdef GOOGLE_PROTOBUF_NO_THREADLOCAL
-#include <pthread.h>
-#endif
+#include <mutex>
#include <google/protobuf/stubs/macros.h>
+// 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 {
namespace protobuf {
namespace internal {
-// A Mutex is a non-reentrant (aka non-recursive) mutex. At most one thread T
-// may hold a mutex at a given time. If T attempts to Lock() the same Mutex
-// while holding it, T will deadlock.
-class LIBPROTOBUF_EXPORT Mutex {
- public:
- // Create a Mutex that is not held by anybody.
- Mutex();
-
- // Destructor
- ~Mutex();
-
- // Block if necessary until this Mutex is free, then acquire it exclusively.
- void Lock();
-
- // Release this Mutex. Caller must hold it exclusively.
- void Unlock();
+#define GOOGLE_PROTOBUF_LINKER_INITIALIZED
+// Mutex is a natural type to wrap. As both google and other organization have
+// specialized mutexes. gRPC also provides an injection mechanism for custom
+// mutexes.
+class LIBPROTOBUF_EXPORT WrappedMutex {
+ public:
+ WrappedMutex() = default;
+ 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();
+ void AssertHeld() const {}
private:
- struct Internal;
- Internal* mInternal;
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
+ std::mutex mu_;
};
+using Mutex = WrappedMutex;
+
// MutexLock(mu) acquires mu when constructed and releases it when destroyed.
class LIBPROTOBUF_EXPORT MutexLock {
public:
@@ -133,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_