summaryrefslogtreecommitdiff
path: root/absl/synchronization
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-10-28 09:57:00 -0700
committerGravatar vslashg <gfalcon@google.com>2020-10-29 12:51:15 -0400
commite9b9e38f67a008d66133535a72ada843bd66013f (patch)
tree4ce2ce2042f7c1842e58088a7a47eed55e61bf1b /absl/synchronization
parent962b067540f511070b7afa4cda465233b42b560a (diff)
Export of internal Abseil changes
-- 0e3e8be75b3ab243991c9b28a27623d86e4511e6 by Abseil Team <absl-team@google.com>: Add constructor overloads with signature (Mutex*, const Condition&) to MutexLock, ReaderMutexLock, WriterMutexLock, ReleasableMutexLock, MaybeMutexLock. These overloads call Mutex::LockWhen, Mutex::ReaderLockWhen, Mutex::WriterLockWhen. Using the guard classes with these new constructors replaces both manual LockWhen/Unlock sequences and the less-efficient, but popular current pattern of "absl::MutexLock lock(&mu); mu.Await(cond);". PiperOrigin-RevId: 339480213 -- ff999bc08360f5bd95557147c97b0e7b200fe3a8 by Jorg Brown <jorg@google.com>: ConvertibleToStringView wastes a lot of cycles initializing members just to reset them immediately after. Only initialize the string storage when needed. This makes StrSplit() 0-30% faster depending on the use case. PiperOrigin-RevId: 339479046 -- 0a773bfb8bc141433a41388731357001fdb34881 by Derek Mauro <dmauro@google.com>: Remove the compiler upgrade fiasco inducing -Weverything -Werror. Switch to a curated set of warnings that may be expanded in the future. PiperOrigin-RevId: 339472677 -- eab54e3e11b126283d33f64c914b200038d215a4 by Abseil Team <absl-team@google.com>: Change execute permission to match presence of the shebang remove execute permission for cmake_common.sh add execute permission for conanfile.py PiperOrigin-RevId: 339453550 -- 7f9726fb605ed20f17f3e221dbce0df03d6904c6 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 339385761 -- f3210dbee3e8a719cf31706963721722203f90e0 by Derek Mauro <dmauro@google.com>: Switch clang compiler detection to use to the Bazel supported mechanism When Abseil launched, we relied on the compiler string "llvm", which we manually set when we used the automatic crosstool generation by using the environment variable BAZEL_COMPILER. Today, Bazel detects clang and automatically sets the compiler string to "clang". Fixes #732 PiperOrigin-RevId: 339360688 -- 413211f59e5e671bf5774efa63ab4df185c74248 by Abseil Team <absl-team@google.com>: Minor comment clarifications and cosmetic tweaks. PiperOrigin-RevId: 339344301 GitOrigin-RevId: 0e3e8be75b3ab243991c9b28a27623d86e4511e6 Change-Id: Ia5b7224cd3d274c79ec7f5514fef63014f458f0f
Diffstat (limited to 'absl/synchronization')
-rw-r--r--absl/synchronization/mutex.h83
-rw-r--r--absl/synchronization/mutex_test.cc34
2 files changed, 102 insertions, 15 deletions
diff --git a/absl/synchronization/mutex.h b/absl/synchronization/mutex.h
index f686c4df..598d1e06 100644
--- a/absl/synchronization/mutex.h
+++ b/absl/synchronization/mutex.h
@@ -31,22 +31,23 @@
//
// MutexLock - An RAII wrapper to acquire and release a `Mutex` for exclusive/
// write access within the current scope.
+//
// ReaderMutexLock
// - An RAII wrapper to acquire and release a `Mutex` for shared/read
// access within the current scope.
//
// WriterMutexLock
-// - Alias for `MutexLock` above, designed for use in distinguishing
-// reader and writer locks within code.
+// - Effectively an alias for `MutexLock` above, designed for use in
+// distinguishing reader and writer locks within code.
//
// In addition to simple mutex locks, this file also defines ways to perform
// locking under certain conditions.
//
-// Condition - (Preferred) Used to wait for a particular predicate that
-// depends on state protected by the `Mutex` to become true.
-// CondVar - A lower-level variant of `Condition` that relies on
-// application code to explicitly signal the `CondVar` when
-// a condition has been met.
+// Condition - (Preferred) Used to wait for a particular predicate that
+// depends on state protected by the `Mutex` to become true.
+// CondVar - A lower-level variant of `Condition` that relies on
+// application code to explicitly signal the `CondVar` when
+// a condition has been met.
//
// See below for more information on using `Condition` or `CondVar`.
//
@@ -506,22 +507,36 @@ class ABSL_LOCKABLE Mutex {
// Example:
//
// Class Foo {
-//
+// public:
// Foo::Bar* Baz() {
-// MutexLock l(&lock_);
+// MutexLock lock(&mu_);
// ...
// return bar;
// }
//
// private:
-// Mutex lock_;
+// Mutex mu_;
// };
class ABSL_SCOPED_LOCKABLE MutexLock {
public:
+ // Constructors
+
+ // Calls `mu->Lock()` and returns when that call returns. That is, `*mu` is
+ // guaranteed to be locked when this object is constructed. Requires that
+ // `mu` be dereferenceable.
explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
this->mu_->Lock();
}
+ // Like above, but calls `mu->LockWhen(cond)` instead. That is, in addition to
+ // the above, the condition given by `cond` is also guaranteed to hold when
+ // this object is constructed.
+ explicit MutexLock(Mutex *mu, const Condition &cond)
+ ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
+ : mu_(mu) {
+ this->mu_->LockWhen(cond);
+ }
+
MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
MutexLock& operator=(const MutexLock&) = delete;
@@ -543,6 +558,12 @@ class ABSL_SCOPED_LOCKABLE ReaderMutexLock {
mu->ReaderLock();
}
+ explicit ReaderMutexLock(Mutex *mu, const Condition &cond)
+ ABSL_SHARED_LOCK_FUNCTION(mu)
+ : mu_(mu) {
+ mu->ReaderLockWhen(cond);
+ }
+
ReaderMutexLock(const ReaderMutexLock&) = delete;
ReaderMutexLock(ReaderMutexLock&&) = delete;
ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
@@ -565,6 +586,12 @@ class ABSL_SCOPED_LOCKABLE WriterMutexLock {
mu->WriterLock();
}
+ explicit WriterMutexLock(Mutex *mu, const Condition &cond)
+ ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
+ : mu_(mu) {
+ mu->WriterLockWhen(cond);
+ }
+
WriterMutexLock(const WriterMutexLock&) = delete;
WriterMutexLock(WriterMutexLock&&) = delete;
WriterMutexLock& operator=(const WriterMutexLock&) = delete;
@@ -603,16 +630,26 @@ class ABSL_SCOPED_LOCKABLE WriterMutexLock {
// `noexcept`; until then this requirement cannot be enforced in the
// type system.)
//
-// Note: to use a `Condition`, you need only construct it and pass it within the
-// appropriate `Mutex' member function, such as `Mutex::Await()`.
+// Note: to use a `Condition`, you need only construct it and pass it to a
+// suitable `Mutex' member function, such as `Mutex::Await()`, or to the
+// constructor of one of the scope guard classes.
//
-// Example:
+// Example using LockWhen/Unlock:
//
// // assume count_ is not internal reference count
// int count_ ABSL_GUARDED_BY(mu_);
+// Condition count_is_zero(+[](int *count) { return *count == 0; }, &count_);
//
-// mu_.LockWhen(Condition(+[](int* count) { return *count == 0; },
-// &count_));
+// mu_.LockWhen(count_is_zero);
+// // ...
+// mu_.Unlock();
+//
+// Example using a scope guard:
+//
+// {
+// MutexLock lock(&mu_, count_is_zero);
+// // ...
+// }
//
// When multiple threads are waiting on exactly the same condition, make sure
// that they are constructed with the same parameters (same pointer to function
@@ -844,6 +881,15 @@ class ABSL_SCOPED_LOCKABLE MutexLockMaybe {
this->mu_->Lock();
}
}
+
+ explicit MutexLockMaybe(Mutex *mu, const Condition &cond)
+ ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
+ : mu_(mu) {
+ if (this->mu_ != nullptr) {
+ this->mu_->LockWhen(cond);
+ }
+ }
+
~MutexLockMaybe() ABSL_UNLOCK_FUNCTION() {
if (this->mu_ != nullptr) { this->mu_->Unlock(); }
}
@@ -866,6 +912,13 @@ class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
: mu_(mu) {
this->mu_->Lock();
}
+
+ explicit ReleasableMutexLock(Mutex *mu, const Condition &cond)
+ ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
+ : mu_(mu) {
+ this->mu_->LockWhen(cond);
+ }
+
~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
if (this->mu_ != nullptr) { this->mu_->Unlock(); }
}
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc
index 66d5f2aa..058f757b 100644
--- a/absl/synchronization/mutex_test.cc
+++ b/absl/synchronization/mutex_test.cc
@@ -707,6 +707,40 @@ TEST(Mutex, LockWhen) {
t.join();
}
+TEST(Mutex, LockWhenGuard) {
+ absl::Mutex mu;
+ int n = 30;
+ bool done = false;
+
+ // We don't inline the lambda because the conversion is ambiguous in MSVC.
+ bool (*cond_eq_10)(int *) = [](int *p) { return *p == 10; };
+ bool (*cond_lt_10)(int *) = [](int *p) { return *p < 10; };
+
+ std::thread t1([&mu, &n, &done, cond_eq_10]() {
+ absl::ReaderMutexLock lock(&mu, absl::Condition(cond_eq_10, &n));
+ done = true;
+ });
+
+ std::thread t2[10];
+ for (std::thread &t : t2) {
+ t = std::thread([&mu, &n, cond_lt_10]() {
+ absl::WriterMutexLock lock(&mu, absl::Condition(cond_lt_10, &n));
+ ++n;
+ });
+ }
+
+ {
+ absl::MutexLock lock(&mu);
+ n = 0;
+ }
+
+ for (std::thread &t : t2) t.join();
+ t1.join();
+
+ EXPECT_TRUE(done);
+ EXPECT_EQ(n, 10);
+}
+
// --------------------------------------------------------
// The following test requires Mutex::ReaderLock to be a real shared
// lock, which is not the case in all builds.