Skip to content

Commit

Permalink
common: Implement bthread rwlock try rdlock
Browse files Browse the repository at this point in the history
Signed-off-by: Hanqing Wu <wuhanqing@corp.netease.com>
  • Loading branch information
wu-hanqing committed Dec 12, 2023
1 parent 921a7ba commit f233132
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ git_repository(
"//:thirdparties/brpc/brpc.patch",
"//:thirdparties/brpc/fix-gcc11.patch",
"//:thirdparties/brpc/0001-bvar-warning-on-conflict-bvar-name.patch",
"//:thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch",
],
patch_args = ["-p1"],
)
Expand Down
2 changes: 1 addition & 1 deletion curvefs/src/metaserver/mds/fsinfo_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace curvefs {
namespace metaserver {
bool FsInfoManager::GetFsInfo(uint32_t fsId, FsInfo *fsInfo) {
std::lock_guard<std::mutex> lock(mtx_);
std::lock_guard<bthread::Mutex> lock(mtx_);
auto iter = fsInfoMap_.find(fsId);
if (iter == fsInfoMap_.end()) {
auto ret = mdsClient_->GetFsInfo(fsId, fsInfo);
Expand Down
4 changes: 3 additions & 1 deletion curvefs/src/metaserver/mds/fsinfo_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <map>
#include <memory>
#include <mutex>

#include "bthread/mutex.h"
#include "curvefs/src/client/rpcclient/mds_client.h"

namespace curvefs {
Expand All @@ -50,7 +52,7 @@ class FsInfoManager {
std::shared_ptr<MdsClient> mdsClient_;
std::map<uint32_t, FsInfo> fsInfoMap_;

std::mutex mtx_;
bthread::Mutex mtx_;
};
} // namespace metaserver
} // namespace curvefs
Expand Down
2 changes: 1 addition & 1 deletion curvefs/src/metaserver/metastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class MetaStoreImpl : public MetaStore {
bool ClearInternal();

private:
RWLock rwLock_; // protect partitionMap_
curve::common::BthreadRWLock rwLock_; // protect partitionMap_
std::shared_ptr<KVStorage> kvStorage_;
std::map<uint32_t, std::shared_ptr<Partition>> partitionMap_;
std::list<uint32_t> partitionIds_;
Expand Down
3 changes: 1 addition & 2 deletions src/common/concurrent/rw_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ class BthreadRWLock : public RWLockBase {
}

int TryRDLock() override {
LOG(WARNING) << "TryRDLock not support yet";
return EINVAL;
return bthread_rwlock_tryrdlock(&rwlock_);
}

void Unlock() override {
Expand Down
84 changes: 84 additions & 0 deletions thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
From 044f2fce36404727110cd5fd5bab563d76fba71a Mon Sep 17 00:00:00 2001
From: Hanqing Wu <wuhanqing@corp.netease.com>
Date: Mon, 11 Dec 2023 18:12:24 +0800
Subject: [PATCH] Add bthread rwlock try rdlock

---
src/bthread/rwlock.cpp | 28 +++++++++++++++++++++++++++
test/bthread_brpc_rwlock_unittest.cpp | 18 +++++++++++++++++
2 files changed, 46 insertions(+)

diff --git a/src/bthread/rwlock.cpp b/src/bthread/rwlock.cpp
index 418f4ad0..ca0d49b0 100644
--- a/src/bthread/rwlock.cpp
+++ b/src/bthread/rwlock.cpp
@@ -106,6 +106,29 @@ inline int rwlock_rlock(bthread_rwlock_t* rwlock) {

}

+inline int rwlock_tryrdlock(bthread_rwlock_t* rwlock) {
+ butil::atomic<unsigned>* whole =
+ (butil::atomic<unsigned>*)rwlock->lock_flag;
+ butil::atomic<unsigned>* w_wait_count =
+ (butil::atomic<unsigned>*)rwlock->w_wait_count;
+
+ while (1) {
+ unsigned w = w_wait_count->load();
+ if (w > 0) {
+ return EBUSY;
+ }
+ // FIXME!! we don't consider read_wait_count overflow yet,2^31 should be enough here
+ unsigned r = whole->load();
+ if ((r >> 31) == 0) {
+ if (whole->compare_exchange_weak(r, r + 1)) {
+ return 0;
+ }
+ } else {
+ return EBUSY;
+ }
+ }
+}
+
inline int rwlock_wlock(bthread_rwlock_t* rwlock) {
butil::atomic<unsigned>* w_wait_count = (butil::atomic<unsigned>*)rwlock->w_wait_count;
butil::atomic<unsigned>* whole = (butil::atomic<unsigned>*)rwlock->lock_flag;
@@ -160,4 +183,9 @@ int bthread_rwlock_unrlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_un
int bthread_rwlock_unwlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_unwlock(rwlock); }

int bthread_rwlock_unlock(bthread_rwlock_t* rwlock) { return bthread::rwlock_unlock(rwlock); }
+
+int bthread_rwlock_tryrdlock(bthread_rwlock_t* rwlock) {
+ return bthread::rwlock_tryrdlock(rwlock);
+}
+
}
diff --git a/test/bthread_brpc_rwlock_unittest.cpp b/test/bthread_brpc_rwlock_unittest.cpp
index f5ce3fb0..345eb322 100644
--- a/test/bthread_brpc_rwlock_unittest.cpp
+++ b/test/bthread_brpc_rwlock_unittest.cpp
@@ -195,4 +195,22 @@ TEST(RwlockTest, mix_thread_types) {
pthread_join(pthreads[i], NULL);
}
}
+
+TEST(RWLockTest, try_rdlock_test) {
+ bthread_rwlock_t rwlock;
+ bthread_rwlock_init(&rwlock, NULL);
+
+ ASSERT_EQ(0, bthread_rwlock_rdlock(&rwlock));
+ ASSERT_EQ(0, bthread_rwlock_tryrdlock(&rwlock));
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock));
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock));
+
+ ASSERT_EQ(0, bthread_rwlock_wrlock(&rwlock));
+ ASSERT_EQ(EBUSY, bthread_rwlock_tryrdlock(&rwlock));
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock));
+
+ ASSERT_EQ(0, bthread_rwlock_tryrdlock(&rwlock));
+ ASSERT_EQ(0, bthread_rwlock_unlock(&rwlock));
+}
+
} // namespace
--
2.37.2

0 comments on commit f233132

Please sign in to comment.