aboutsummaryrefslogtreecommitdiff
path: root/absl/synchronization/mutex.h
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2023-10-31 03:34:00 -0700
committerCopybara-Service <copybara-worker@google.com>2023-10-31 03:34:46 -0700
commitf3760b4d3b2773d1cb8e9ddda29dc9bce386d540 (patch)
tree11f466cef17290c4f550637b6a5a9ab18dffdb80 /absl/synchronization/mutex.h
parent6c8338c252116e429307361ff4fdc1fd0532902d (diff)
downloadabseil-f3760b4d3b2773d1cb8e9ddda29dc9bce386d540.tar.gz
abseil-f3760b4d3b2773d1cb8e9ddda29dc9bce386d540.tar.bz2
abseil-f3760b4d3b2773d1cb8e9ddda29dc9bce386d540.zip
Mutex: Remove destructor in release build
The Mutex destructor is needed only to clean up debug logging and invariant checking synch events. These are not supposed to be used in production, but the non-empty destructor has costs for production builds. Instead of removing synch events in destructor, drop all of them if we accumulated too many. For tests is should not matter (we maybe only consume a bit more memory). Production builds should be either unaffected (if don't use debug logging), or use periodic reset of all synch events. PiperOrigin-RevId: 578123259 Change-Id: I0ec59183a5f63ea0a6b7fc50f0a77974e7f677be
Diffstat (limited to 'absl/synchronization/mutex.h')
-rw-r--r--absl/synchronization/mutex.h18
1 files changed, 17 insertions, 1 deletions
diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h
index 95726f6b..157b7050 100644
--- a/absl/synchronization/mutex.h
+++ b/absl/synchronization/mutex.h
@@ -64,6 +64,7 @@
#include <iterator>
#include <string>
+#include "absl/base/attributes.h"
#include "absl/base/const_init.h"
#include "absl/base/internal/identity.h"
#include "absl/base/internal/low_level_alloc.h"
@@ -536,6 +537,7 @@ class ABSL_LOCKABLE Mutex {
void Block(base_internal::PerThreadSynch* s);
// Wake a thread; return successor.
base_internal::PerThreadSynch* Wakeup(base_internal::PerThreadSynch* w);
+ void Dtor();
friend class CondVar; // for access to Trans()/Fer().
void Trans(MuHow how); // used for CondVar->Mutex transfer
@@ -909,7 +911,6 @@ class CondVar {
// A `CondVar` allocated on the heap or on the stack can use the this
// constructor.
CondVar();
- ~CondVar();
// CondVar::Wait()
//
@@ -1061,6 +1062,21 @@ inline Mutex::Mutex() : mu_(0) {
inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {}
+#if !defined(__APPLE__) && !defined(ABSL_BUILD_DLL)
+ABSL_ATTRIBUTE_ALWAYS_INLINE
+inline Mutex::~Mutex() { Dtor(); }
+#endif
+
+#if defined(NDEBUG) && !defined(ABSL_HAVE_THREAD_SANITIZER)
+// Use default (empty) destructor in release build for performance reasons.
+// We need to mark both Dtor and ~Mutex as always inline for inconsistent
+// builds that use both NDEBUG and !NDEBUG with dynamic libraries. In these
+// cases we want the empty functions to dissolve entirely rather than being
+// exported from dynamic libraries and potentially override the non-empty ones.
+ABSL_ATTRIBUTE_ALWAYS_INLINE
+inline void Mutex::Dtor() {}
+#endif
+
inline CondVar::CondVar() : cv_(0) {}
// static