summaryrefslogtreecommitdiff
path: root/absl
diff options
context:
space:
mode:
Diffstat (limited to 'absl')
-rw-r--r--absl/synchronization/mutex_test.cc25
1 files changed, 25 insertions, 0 deletions
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc
index 35802b2e..b585c342 100644
--- a/absl/synchronization/mutex_test.cc
+++ b/absl/synchronization/mutex_test.cc
@@ -1868,4 +1868,29 @@ TEST(Mutex, WriterPriority) {
EXPECT_TRUE(saw_wrote.load());
}
+TEST(Mutex, LockWhenWithTimeoutResult) {
+ // Check various corner cases for Await/LockWhen return value
+ // with always true/always false conditions.
+ absl::Mutex mu;
+ const bool kAlwaysTrue = true, kAlwaysFalse = false;
+ const absl::Condition kTrueCond(&kAlwaysTrue), kFalseCond(&kAlwaysFalse);
+ EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
+ mu.Unlock();
+ EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
+ EXPECT_TRUE(mu.AwaitWithTimeout(kTrueCond, absl::Milliseconds(1)));
+ EXPECT_FALSE(mu.AwaitWithTimeout(kFalseCond, absl::Milliseconds(1)));
+ std::thread th1([&]() {
+ EXPECT_TRUE(mu.LockWhenWithTimeout(kTrueCond, absl::Milliseconds(1)));
+ mu.Unlock();
+ });
+ std::thread th2([&]() {
+ EXPECT_FALSE(mu.LockWhenWithTimeout(kFalseCond, absl::Milliseconds(1)));
+ mu.Unlock();
+ });
+ absl::SleepFor(absl::Milliseconds(100));
+ mu.Unlock();
+ th1.join();
+ th2.join();
+}
+
} // namespace