aboutsummaryrefslogtreecommitdiff
path: root/absl/synchronization/mutex_test.cc
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2023-10-27 00:37:27 -0700
committerCopybara-Service <copybara-worker@google.com>2023-10-27 00:38:09 -0700
commitcdb1e7f4a60a6d0ee038f733776356a50121265a (patch)
treec677456c22d00eff7fbf5876a4313f165002de4b /absl/synchronization/mutex_test.cc
parentc8087ae8bd1e8b25f4e2f38e903b1360005dc4e1 (diff)
downloadabseil-cdb1e7f4a60a6d0ee038f733776356a50121265a.tar.gz
abseil-cdb1e7f4a60a6d0ee038f733776356a50121265a.tar.bz2
abseil-cdb1e7f4a60a6d0ee038f733776356a50121265a.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: 577106805 Change-Id: Icaaf7166b99afcd5dce92b4acd1be661fb72f10b
Diffstat (limited to 'absl/synchronization/mutex_test.cc')
-rw-r--r--absl/synchronization/mutex_test.cc27
1 files changed, 27 insertions, 0 deletions
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc
index 6c38c07c..35b43334 100644
--- a/absl/synchronization/mutex_test.cc
+++ b/absl/synchronization/mutex_test.cc
@@ -1709,6 +1709,33 @@ TEST(Mutex, Logging) {
logged_cv.SignalAll();
}
+TEST(Mutex, LoggingAddressReuse) {
+ // Repeatedly re-create a Mutex with debug logging at the same address.
+ alignas(absl::Mutex) char storage[sizeof(absl::Mutex)];
+ auto invariant =
+ +[](void *alive) { EXPECT_TRUE(*static_cast<bool *>(alive)); };
+ constexpr size_t kIters = 10;
+ bool alive[kIters] = {};
+ for (size_t i = 0; i < kIters; ++i) {
+ absl::Mutex *mu = new (storage) absl::Mutex;
+ alive[i] = true;
+ mu->EnableDebugLog("Mutex");
+ mu->EnableInvariantDebugging(invariant, &alive[i]);
+ mu->Lock();
+ mu->Unlock();
+ mu->~Mutex();
+ alive[i] = false;
+ }
+}
+
+TEST(Mutex, LoggingBankrupcy) {
+ // Test the case with too many live Mutexes with debug logging.
+ std::vector<absl::Mutex> mus(1 << 20);
+ for (auto &mu : mus) {
+ mu.EnableDebugLog("Mutex");
+ }
+}
+
// --------------------------------------------------------
// Generate the vector of thread counts for tests parameterized on thread count.