diff --git a/WORKSPACE b/WORKSPACE index a423f1c46a..4200320eee 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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"], ) diff --git a/curvefs/src/metaserver/mds/fsinfo_manager.cpp b/curvefs/src/metaserver/mds/fsinfo_manager.cpp index 25b2c5ca9d..6797668d0a 100644 --- a/curvefs/src/metaserver/mds/fsinfo_manager.cpp +++ b/curvefs/src/metaserver/mds/fsinfo_manager.cpp @@ -25,7 +25,7 @@ namespace curvefs { namespace metaserver { bool FsInfoManager::GetFsInfo(uint32_t fsId, FsInfo *fsInfo) { - std::lock_guard lock(mtx_); + std::lock_guard lock(mtx_); auto iter = fsInfoMap_.find(fsId); if (iter == fsInfoMap_.end()) { auto ret = mdsClient_->GetFsInfo(fsId, fsInfo); diff --git a/curvefs/src/metaserver/mds/fsinfo_manager.h b/curvefs/src/metaserver/mds/fsinfo_manager.h index 0cf6fb84e8..5b11000a33 100644 --- a/curvefs/src/metaserver/mds/fsinfo_manager.h +++ b/curvefs/src/metaserver/mds/fsinfo_manager.h @@ -26,6 +26,8 @@ #include #include #include + +#include "bthread/mutex.h" #include "curvefs/src/client/rpcclient/mds_client.h" namespace curvefs { @@ -50,7 +52,7 @@ class FsInfoManager { std::shared_ptr mdsClient_; std::map fsInfoMap_; - std::mutex mtx_; + bthread::Mutex mtx_; }; } // namespace metaserver } // namespace curvefs diff --git a/curvefs/src/metaserver/metastore.h b/curvefs/src/metaserver/metastore.h index a13c0a4980..63480fcc48 100644 --- a/curvefs/src/metaserver/metastore.h +++ b/curvefs/src/metaserver/metastore.h @@ -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_; std::map> partitionMap_; std::list partitionIds_; diff --git a/src/common/concurrent/rw_lock.h b/src/common/concurrent/rw_lock.h index 807afb3b8c..f602a9ba57 100644 --- a/src/common/concurrent/rw_lock.h +++ b/src/common/concurrent/rw_lock.h @@ -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 { diff --git a/thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch b/thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch new file mode 100644 index 0000000000..09b33fd749 --- /dev/null +++ b/thirdparties/brpc/0002-Add-bthread-rwlock-try-rdlock.patch @@ -0,0 +1,84 @@ +From 044f2fce36404727110cd5fd5bab563d76fba71a Mon Sep 17 00:00:00 2001 +From: Hanqing Wu +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* whole = ++ (butil::atomic*)rwlock->lock_flag; ++ butil::atomic* w_wait_count = ++ (butil::atomic*)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* w_wait_count = (butil::atomic*)rwlock->w_wait_count; + butil::atomic* whole = (butil::atomic*)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 +