[libcxx-commits] [libcxx] [libcxx] Shared Mutex no longer holds the lock when calling notify_* on gates. (PR #107876)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 9 08:20:27 PDT 2024
https://github.com/Brotcrunsher created https://github.com/llvm/llvm-project/pull/107876
Holding the associated lock while calling notify_* on a condition_variable is generally considered a pessimization, as the notified thread might "instantly" wake up, notice that it can't acquire the lock, and then goes back to sleep.
**Note:** This is my very first PR to the LLVM-Project. As such, it's possible that some kind of compiler extensions exist that render this unnecessary.
>From 10f7359197f84b796ca7765c1874eb73d2b5bfbb Mon Sep 17 00:00:00 2001
From: Brotcrunsher <Brotcrunsher at hotmail.de>
Date: Mon, 9 Sep 2024 17:16:59 +0200
Subject: [PATCH] [libcxx] Shared Mutex no longer holds the lock when calling
notify_* on gates.
Holding the associated lock while calling notify_* on a condition_variable is generally considered a pessimization, as the notified thread might "instantly" wake up, notice that it can't acquire the lock, and then goes back to sleep.
---
libcxx/src/shared_mutex.cpp | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/libcxx/src/shared_mutex.cpp b/libcxx/src/shared_mutex.cpp
index 1a346dda027f8e..61808337369561 100644
--- a/libcxx/src/shared_mutex.cpp
+++ b/libcxx/src/shared_mutex.cpp
@@ -38,8 +38,10 @@ bool __shared_mutex_base::try_lock() {
}
void __shared_mutex_base::unlock() {
- lock_guard<mutex> _(__mut_);
- __state_ = 0;
+ {
+ lock_guard<mutex> _(__mut_);
+ __state_ = 0;
+ }
__gate1_.notify_all();
}
@@ -67,16 +69,20 @@ bool __shared_mutex_base::try_lock_shared() {
}
void __shared_mutex_base::unlock_shared() {
- lock_guard<mutex> _(__mut_);
+ unique_lock<mutex> lk(__mut_);
unsigned num_readers = (__state_ & __n_readers_) - 1;
__state_ &= ~__n_readers_;
__state_ |= num_readers;
if (__state_ & __write_entered_) {
- if (num_readers == 0)
+ if (num_readers == 0) {
+ lk.unlock();
__gate2_.notify_one();
+ }
} else {
- if (num_readers == __n_readers_ - 1)
+ if (num_readers == __n_readers_ - 1) {
+ lk.unlock();
__gate1_.notify_one();
+ }
}
}
More information about the libcxx-commits
mailing list